A JM Posted June 25, 2009 Share Posted June 25, 2009 I want to ask the user a question and receive an answer before SUBMITTING my form how would I go about achieving that? For example: Before saving my forms information to the DB I want to ask the user with a pop-up if they are ready to finalize the document, wait for their answer, yes/no and save their choice as a variable. Can this be done via PHP or is this more of a javascript issue? Thanks. A JM, Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/ Share on other sites More sharing options...
celsoendo Posted June 25, 2009 Share Posted June 25, 2009 Javascript: document.getElementById("form_id").submit(); Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863049 Share on other sites More sharing options...
flyhoney Posted June 25, 2009 Share Posted June 25, 2009 It can be done either way. In javascript you need to hook the form's submit event. You can then display a dialog popup like this: <script> function onSubmit() { if (confirm("Do you really want to do that?")) { // submit the form } else { // don't submit the form } } </script> Alternatively, you can store the user's answers to the form in the $_SESSION, that way you can display an intermediary page for them to confirm. Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863051 Share on other sites More sharing options...
A JM Posted June 25, 2009 Author Share Posted June 25, 2009 Thanks for the posts. I'm still a bit green with working with forms, php,etc. From an implementation standpoint, I think the the full script makes more sense. The button on my form should still be a SUBMIT correct? How do I exit the script without submitting the form just don't process a SUBMIT? Will look something like this? <script> function onSubmit() { if (confirm("Do you really want to do that?")) { document.getElementById("form_id").submit(); } else { // don't submit the form } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863059 Share on other sites More sharing options...
joel24 Posted June 25, 2009 Share Posted June 25, 2009 your button would be like <button onclick="onSubmit()">Submit Form</button> or <input type="submit" onclick="onSubmit()" value="Submit Form" /> then put that js up in the head Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863061 Share on other sites More sharing options...
Alex Posted June 25, 2009 Share Posted June 25, 2009 You do something like.. onclick="if(confirm('Are you sure? This is irreversible!')){return true;}else{return false;}" Makes this easier. Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863063 Share on other sites More sharing options...
A JM Posted June 25, 2009 Author Share Posted June 25, 2009 You do something like.. onclick="if(confirm('Are you sure? This is irreversible!')){return true;}else{return false;}" Makes this easier. Makes sense, this runs from the button as opposed to the head and is more compact. How do I capture the value from return whether it be TRUE/FALSE or is 'return' the variable that I can use? A JM, Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863068 Share on other sites More sharing options...
Alex Posted June 25, 2009 Share Posted June 25, 2009 You do something like.. onclick="if(confirm('Are you sure? This is irreversible!')){return true;}else{return false;}" Makes this easier. Makes sense, this runs from the button as opposed to the head and is more compact. How do I capture the value from return whether it be TRUE/FALSE or is 'return' the variable that I can use? A JM, That's all you need. Put that inside your Submit button element. Once submit is clicked you'll be asked that question, if you hit "Ok" It'll submit the form. If you hit "Cancel" It won't. Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863071 Share on other sites More sharing options...
A JM Posted June 25, 2009 Author Share Posted June 25, 2009 OK, gotcha. This doesn't return a variable of TRUE/FALSE only waits for the user to click YES/NO. To return a variable with TRUE/FALSE I need to go with the script in the head section. <button onclick="onSubmit()">Submit Form</button> <script> function onSubmit() { if (confirm("Do you really want to do that?")) { $myYESvariable = 'TRUE'; document.getElementById("form_id").submit(); } else { $myNOvariable = 'FALSE'; // don't submit the form } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863075 Share on other sites More sharing options...
chmpdog Posted June 25, 2009 Share Posted June 25, 2009 yeah I dont think php is capable of doing that without javascript Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863076 Share on other sites More sharing options...
Alex Posted June 25, 2009 Share Posted June 25, 2009 OK, gotcha. This doesn't return a variable of TRUE/FALSE only waits for the user to click YES/NO. To return a variable with TRUE/FALSE I need to go with the script in the head section. <button onclick="onSubmit()">Submit Form</button> <script> function onSubmit() { if (confirm("Do you really want to do that?")) { $myYESvariable = 'TRUE'; document.getElementById("form_id").submit(); } else { $myNOvariable = 'FALSE'; // don't submit the form } } </script> Why do you need to set anything to true or false?.. All you need to do is put the code I gave you inside your submit button: eg. <input type="submit" onclick="what I gave you..." /> And it'll do exactly what you want. Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863094 Share on other sites More sharing options...
A JM Posted June 25, 2009 Author Share Posted June 25, 2009 I'm trying to set a value for a variable for TRUE $myvariable =1 and for FALSE $myvariable=0. The script worked perfectly - last question on this... My form has the following action: <form action="<?php echo $editFormAction2; ?>" method="post" id="form1" enctype="multipart/form-data"> When the form is submitted by document.getElementById("form_id").submit(); will it run the action part of the form or do I need to kick that off in he script somehow <?php echo $editFormAction2; ?>? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863258 Share on other sites More sharing options...
flyhoney Posted June 25, 2009 Share Posted June 25, 2009 It will submit the form to the script located at $editFormAction2. So if $editFormAction2 = 'something.php', the form will be sent to something.php Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-863405 Share on other sites More sharing options...
A JM Posted June 29, 2009 Author Share Posted June 29, 2009 This doesn't seem to be working the way that I think it should so I must not be doing this correctly, hopefully someone can straighten me out... My button on my form has the following: <input type="button" name="submit_revision" value="Save Changes" onClick="onSubmit()"/> my form: <form action="<?php echo $editFormAction2; ?>" method="post" id="form1" enctype="multipart/form-data"> ....other PHP stuff.... <script> function onSubmit() { if (confirm("Finalize for Management review?")) { $submit_for_review = 1; document.getElementById("form1").submit(); //confirm($submit_for_review); //exit(); } else { $submit_for_review = 0; // don't submit the form //confirm($submit_for_review); //exit(); } } </script> <?php if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $editFormAction2 = $_SERVER['PHP_SELF']; print_r("it submitted"); exit; } ?> My interpretation of the above is that when the form is submitted it would run $editFormAction2 but it isn't....???? not sure why though. Also, the confirm() can it be a "YES"/"NO" as opposed to an "OK"/"CANCEL"??? Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-865266 Share on other sites More sharing options...
A JM Posted June 29, 2009 Author Share Posted June 29, 2009 I'm also noticing that the variable that is being set in the <SCRIPT> is not available to PHP, is that correct? Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-865285 Share on other sites More sharing options...
haku Posted June 29, 2009 Share Posted June 29, 2009 Variables set by javascript are not directly accessible by PHP. You need to use an AJAX call in the background to be able to do that. Another point is that this popup isn't going to work for any users who don't have javascript enabled, so you may want to build a php fallback if it is that important. Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-865389 Share on other sites More sharing options...
A JM Posted June 29, 2009 Author Share Posted June 29, 2009 OK, makes sense. So, if I use a hidden textbox and set the variable from Javascript that should work correct? <script> function onSubmit() { if (confirm("Finalize for Management review?")) { document.getElementById("form1").elements["submit_for_review"].value = '1'; document.getElementById("form1").submit(); } else { document.getElementById("form1").elements["submit_for_review"].value = '0'; } } </script> Any ideas about Also, the confirm() can it be a "YES"/"NO" as opposed to an "OK"/"CANCEL"??? Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-865529 Share on other sites More sharing options...
A JM Posted June 30, 2009 Author Share Posted June 30, 2009 K, finally figured this out using javascript. Thanks to all that helped. <script> function onSubmit() { if (confirm("Finalize for Management review?")) { document.getElementById("form1").elements["submit_for_review"].value = '1'; document.getElementById("form1").submit(); } else { document.getElementById("form1").elements["submit_for_review"].value = '0'; document.getElementById("form1").submit(); } } </script> I also realized that PHP script is read from top down (duh!) so if I wanted a script to run prior to another I just had to load it at the top of the page... A JM, Quote Link to comment https://forums.phpfreaks.com/topic/163577-solved-how-do-i-submit-a-form/#findComment-866395 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.