Connecting Tech Pros Worldwide Forums | Help | Site Map

Problems submitting a form via Javascript

Newbie
 
Join Date: Sep 2008
Posts: 5
#1: Sep 1 '08
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

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Sep 1 '08

re: Problems submitting a form via Javascript


Why can't you use a button?

From what you've posted, it should work. Can you post the rest of the relevant code.
Newbie
 
Join Date: Sep 2008
Posts: 5
#3: Sep 2 '08

re: Problems submitting a form via Javascript


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 :(
Newbie
 
Join Date: Sep 2008
Posts: 5
#4: Sep 2 '08

re: Problems submitting a form via Javascript


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 :(
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Sep 2 '08

re: Problems submitting a form via Javascript


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.
Newbie
 
Join Date: Sep 2008
Posts: 5
#6: Sep 2 '08

re: Problems submitting a form via Javascript


What a moron, thank you for the tip and sorry for wasting your time :(
Newbie
 
Join Date: Sep 2008
Posts: 5
#7: Sep 2 '08

re: Problems submitting a form via Javascript


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?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Sep 2 '08

re: Problems submitting a form via Javascript


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