galvin Posted May 6, 2009 Share Posted May 6, 2009 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? Quote Link to comment Share on other sites More sharing options...
taith Posted May 6, 2009 Share Posted May 6, 2009 javascript formsubmits are wierd... document.updatecount.submit(); Quote Link to comment Share on other sites More sharing options...
galvin Posted May 6, 2009 Author Share Posted May 6, 2009 I changed it to what you wrote but it didn't work. Is it just not possible? Quote Link to comment Share on other sites More sharing options...
xtopolis Posted May 6, 2009 Share Posted May 6, 2009 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? Quote Link to comment Share on other sites More sharing options...
BioBob Posted May 6, 2009 Share Posted May 6, 2009 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... Quote Link to comment Share on other sites More sharing options...
galvin Posted May 6, 2009 Author Share Posted May 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted May 6, 2009 Share Posted May 6, 2009 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. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 There's no difference between JavaScript submit() and the button submit. Perhaps your JavaScript submits the form before it gets filled out? Quote Link to comment Share on other sites More sharing options...
galvin Posted May 6, 2009 Author Share Posted May 6, 2009 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> Quote Link to comment Share on other sites More sharing options...
galvin Posted May 6, 2009 Author Share Posted May 6, 2009 Also, what is the proper way to call the submit() using Javascript... document.updatecount.submit(); OR document.getElementById('updatecount').submit(); Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 <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> Quote Link to comment Share on other sites More sharing options...
xtopolis Posted May 7, 2009 Share Posted May 7, 2009 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. Use Ajax instead? Quote Link to comment Share on other sites More sharing options...
galvin Posted May 7, 2009 Author Share Posted May 7, 2009 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.