By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

Problems submitting a form via Javascript

P: 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
Sep 1 '08 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
Why can't you use a button?

From what you've posted, it should work. Can you post the rest of the relevant code.
Sep 1 '08 #2

P: 5
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 :(
Sep 2 '08 #3

P: 5
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 :(
Sep 2 '08 #4

acoder
Expert Mod 15k+
P: 16,027
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.
Sep 2 '08 #5

P: 5
What a moron, thank you for the tip and sorry for wasting your time :(
Sep 2 '08 #6

P: 5
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?
Sep 2 '08 #7

acoder
Expert Mod 15k+
P: 16,027
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.
Sep 2 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.