Jump to content

[SOLVED] submit() vs. clicking Submit button?


galvin

Recommended Posts

All on test.php, I have the following...

 

I have this form...


<form method="post" id='updatecount' action="test.php" class="">
<input type='submit' name='submit'  value='Update Game Count' class='' style="display:none">
</form>

 

I use the following javascript to submit the form (so that a user doesn't have to click a Submit button)...

 

document.getElementById('updatecount').submit();

 

My question is this.  I have this PHP code to process this form when it is submitted....

 

if (isset($_POST['submit'])) {
//code here
}

 

I tested the code first WITH a "Submit" button and it ran fine that way.  But when I remove the submit button and submit the form using submit(), it is NOT running the code.  So it seems like using submit() via Javascript is not the same as actually clicking a Submit button.  Is that right?  Regardless, how can I recognize the submit() function using PHP?

Link to comment
Share on other sites

What's the problem?

 

The form will submit back to itself(test.php) and you can catch it..  put php code at the top of the page:

<?php

if(isset($_POST['submit'])){
  //process form
}

?>
<!-- Form code -->

 

 

Also, why are you using javascript to submit the form?  Perhaps there is an alternative to triggering submit(), and you can rather capture the event, do validation, and return true?

Link to comment
Share on other sites

The button, at least in HTML, will only register that its been clicked, if it has been clicked.  Thus, if you hit ENTER, it wont register as having been clicked.  Could be different in JS tho, I dont play much with that...  Or try going back and forth, see if there is a typo, spelling difference or something too

 

foreach ($_POST as $key => $val)  {

  echo "Key: $key  Val: $val<br />\n";

  }

 

Take a look at all your POST VARS and see if there is a difference in whats coming thru and what youre checking for...

Link to comment
Share on other sites

xtopolis, that's how I have it currently.  The gist is that if I add an actual Submit button to the form and click that, the PHP code at the top of the page runs fine.  But if I hide the Submit button (using CSS, display: none) and submit the form by using Javascript (via submit()), the PHP code doesn't process.  So it seems like this PHP code ...

if(isset($_POST['submit'])){
  //process form
}

 

...isn't recognizing the Javascript submit(), only an actual clicked Submit button. I was thinking maybe that's just that way it is (i.e. there needs to be an actual CLICK for PHP to process it) but from the replies in this post, it sounds like it should work just fine either way.  I'll check to see if maybe there is just a typo somewhere.

 

 

Link to comment
Share on other sites

if you're using javascript to submit a form, whether it's by ID or NAME, you must include a hidden field to pass so you can check for form submission on the other side, ie.

 

<form method="post" name="updatecount" action="test.php" class="">
     <input name="submit" type="hidden" />
     <a href="javascript:void(0);" onmouseup="javascript:updatecount.submit();">Update</a>
</form>

your 'type="submit"' button value will not be passed if it's not clicked .. therefore, you must use a hidden field to get that value to pass through.

Link to comment
Share on other sites

This still won't work.  Ken, there is not a form for anyone to fill out.  I just want to run some PHP/MySQL when a certain javascript function runs, so I figured I'd have a little form and submit it "behind the scenes" using submit() and then use PHP code to recognize that submit and make other stuff happen.  I just can't get it to recognize the submit without having someone actually click a Submit button.

 

Maybe I'm going about it the wrong way, but this seems like something simple that should work.

 

mrMarcus wrote the following. Is that third line necessary?  Can someone explain what that line is doing? 

This code below makes a link for "Update" show up, but I don't want anything to show up. I want this entire form to be hidden, and just make it submit using submit() and then recognize the submit via PHP..

<form method="post" name="updatecount" action="test.php" class="">
     <input name="submit" type="hidden" />
     <a href="javascript:void(0);" onmouseup="javascript:updatecount.submit();">Update</a>
</form>

Link to comment
Share on other sites

<form method="post" name='updatecount' action="test.php" class="">
<input type='submit' name='submit'  value='Update Game Count' class='' style="display:none">
</form>
<script type="text/javascript">document.forms.updatecount.submit();</script>

Link to comment
Share on other sites

Exactly what I ended up doing (i.e. using AJAX) because I finally got it working the original way, but it refreshed the entire page and that wasn't gonna work.  So AJAX is the way to go.  Thanks everyone!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.