Connecting Tech Pros Worldwide Help | Site Map

Problems submitting a form via Javascript

  #1  
Old September 1st, 2008, 03:26 PM
Newbie
 
Join Date: Sep 2008
Posts: 5
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
  #2  
Old September 1st, 2008, 04:54 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,528
Provided Answers: 12

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.
  #3  
Old September 2nd, 2008, 08:16 AM
Newbie
 
Join Date: Sep 2008
Posts: 5

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 :(
  #4  
Old September 2nd, 2008, 09:08 AM
Newbie
 
Join Date: Sep 2008
Posts: 5

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 :(
  #5  
Old September 2nd, 2008, 01:01 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,528
Provided Answers: 12

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.
  #6  
Old September 2nd, 2008, 01:56 PM
Newbie
 
Join Date: Sep 2008
Posts: 5

re: Problems submitting a form via Javascript


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

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?
  #8  
Old September 2nd, 2008, 05:02 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,528
Provided Answers: 12

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Javascript array to Java Oleg Konovalov answers 7 December 1st, 2005 08:55 PM
problems submitting form from one domain to another Tyler Style answers 5 July 23rd, 2005 06:39 PM
REQUEST: Help With Transferring to New Page After Submitting Form (Viae-mail) Matthew Lybanon answers 2 July 23rd, 2005 12:21 PM
Problem with Opera and JavaScript Shaun Fleming answers 6 July 20th, 2005 03:15 PM