Connecting Tech Pros Worldwide Help | Site Map

Problems submitting a form via Javascript

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 1st, 2008, 02:26 PM
Newbie
 
Join Date: Sep 2008
Posts: 5
Default Problems submitting a form via Javascript

Hi guys,

I've got a very basic form, like so:

Expand|Select|Wrap|Line Numbers
  1.     <form method="post" name="shra" action="http://url.com">
  2.  
  3.         <input type="submit" name="Submit" value="Submit">
  4.  
  5.     </form>
Then a basic check for the POST in PHP, again it works fine:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     if(isset($_POST['Submit'])) {
  3.         echo "POST!"; die;
  4.     }
  5. ?>
Again, no problem. Hit the submit button and the magic happens.

Now, I'm trying to achieve a similar thing though JavaScript since I didn't want to use an actual button for another implementation:

Expand|Select|Wrap|Line Numbers
  1. <span onclick="document.shra.submit();">Submit</span>
And it doesn't submit, but it does seem to refresh the page.

Any suggestions please chaps?

Many thanks
Reply
  #2  
Old September 1st, 2008, 03:54 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,236
Default

Why can't you use a button?

From what you've posted, it should work. Can you post the rest of the relevant code.
Reply
  #3  
Old September 2nd, 2008, 07:16 AM
Newbie
 
Join Date: Sep 2008
Posts: 5
Default

A button seems to have weird styling issues, basically the page is quite long, so I wanted to have an inline submit button at the very top of the page, and a span with javascript (and styled appropriately with a cursor etc) seemed to be the best way around this.

Just wondering, what additional code would you need? I think it does something at the moment, but it doesn't trigger the if statement in the PHP to say POST has been declared :(
Reply
  #4  
Old September 2nd, 2008, 08:08 AM
Newbie
 
Join Date: Sep 2008
Posts: 5
Default

Just stripped it down to the bare bones:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.     if(isset($_POST['Submit'])) {
  3.         echo "POST!";
  4.         die;
  5.     }
  6. ?>
  7.  
  8.     <form method="post" name="shra" action="http://url.com">
  9.  
  10.         <input type="submit" name="Submit" value="Submit">
  11.  
  12.         <span onclick="document.shra.submit();">Submit</span>
  13.  
  14.     </form>
Clicking on the input button submits fine, but clicking on the span doesn't :(
Reply
  #5  
Old September 2nd, 2008, 12:01 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,236
Default

Of course it's not going to work. It's a problem with your PHP code. It's testing for a posted submit button which is not set when using a JavaScript submit. So the submit is working, but you'll need to modify your server-side code.
Reply
  #6  
Old September 2nd, 2008, 12:56 PM
Newbie
 
Join Date: Sep 2008
Posts: 5
Default

What a moron, thank you for the tip and sorry for wasting your time :(
Reply
  #7  
Old September 2nd, 2008, 12:57 PM
Newbie
 
Join Date: Sep 2008
Posts: 5
Default

Apologies for another question, but is there a way to check for a POST in general?

I've just created a hidden field for now to check for, but there could be a better way?
Reply
  #8  
Old September 2nd, 2008, 04:02 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,236
Default

Well, you're going to post something, so you don't even need a hidden field for that purpose. Just check if one of the fields is defined in the $_POST array. By the way, this is more of a PHP question, so if you have problems with it, ask in the PHP forum.
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.