Cetanu Posted August 26, 2009 Share Posted August 26, 2009 I am trying to make a page where someone selects an option on one form, a code is executed depending on the selection, and then a new form appears for another option. Could it be that my forms are clashing into each other because when I have them both on one page AND them action="" set to the same page, they don't work. If I have them on the same page but they have different actions, it works. Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/ Share on other sites More sharing options...
mikesta707 Posted August 26, 2009 Share Posted August 26, 2009 without seeing any code I'm forced to say yes they seem to be clashing into each other. Whats wrong with them having different actions? Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/#findComment-907093 Share on other sites More sharing options...
Cetanu Posted August 26, 2009 Author Share Posted August 26, 2009 I do not want to redirect my users to another page and then back (because the one with the different action redirects them to its action). Also, when it redirects them I cannot execute code based on whether or not the second form was completed AFTER they were sent away and back. here: FORM 1 <form action='fcenter.php?page=planet1' method='post'> <select size='4' name='fight'> <option value='Aliens'>Aliens</option> <option value='Marines'>Marines</option> <option value='Native Animals'>Native Species</option> <option value='Totally Random'>Randomize</option> </select> <input type='submit' name='choose' value='Search'/> </form> FORM 2 <form action='fcenter.php?page=planet1' method='post'> <input type='radio' name='attack' value='Attack'/>Attack<br/> <input type='radio' name='attack' value='Flee'/> Flee <br/> <input type='submit' name='confirm' id='confirm' value='Choose'/> </form> Ideal situation: Member answers form 1 --> Form 2 comes up (answers form 2) --> Code is executed based on what they chose on form 2 (this code involves $_SESSIONs) What it is doing is this: Member answers form 1 --> Form 2 comes up (with different action) --> Send to enemy.php --> Redirect back to original page --> Now the original page is reloaded so I cannot execute the code based on form 2!!!! What if form 1 was a $_GET form? >.> <.< then I could redirect them to...the page they chose? :shrug: Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/#findComment-907100 Share on other sites More sharing options...
bluebyyou Posted August 26, 2009 Share Posted August 26, 2009 What you are hoping for will require some javascript. PHP is executed on the server, javascript is executed in the browser. Or you can have the form's action point the the same page...then have a hidden input on each form. the hidden input will give an if statement or a switch to choose a peice of code to execute Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/#findComment-907107 Share on other sites More sharing options...
Cetanu Posted August 26, 2009 Author Share Posted August 26, 2009 What you are hoping for will require some javascript. PHP is executed on the server, javascript is executed in the browser. Like what sort of JavaScript? Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/#findComment-907109 Share on other sites More sharing options...
bluebyyou Posted August 26, 2009 Share Posted August 26, 2009 The php way would be something like this <?php if ($_POST['action'] == 1) { /* DO SOMETHING */} else { /* DO SOMETHING ELSE */ } ?> <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input name="action" type="hidden" value = "1"> <input="submit" name="submit" value="submit"> </form> <form name="form2" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input name="action" type="hidden" value = "2"> <input="submit" name="submit" value="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/#findComment-907113 Share on other sites More sharing options...
Cetanu Posted August 27, 2009 Author Share Posted August 27, 2009 The php way would be something like this <?php if ($_POST['action'] == 1) { /* DO SOMETHING */} else { /* DO SOMETHING ELSE */ } ?> <form name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input name="action" type="hidden" value = "1"> <input="submit" name="submit" value="submit"> </form> <form name="form2" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input name="action" type="hidden" value = "2"> <input="submit" name="submit" value="submit"> </form> I don't get it, that's supposed to do what I want? I tried $_SERVER but it doesn't work because the page it is supposed to execute on is a switch (?page=planet1), when I tried it it redirected me to the main page with out the switch. Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/#findComment-907183 Share on other sites More sharing options...
Cetanu Posted August 27, 2009 Author Share Posted August 27, 2009 When it gets to the second form of my script and the user presses "Confirm" it does not move on to execute the rest of the PHP, it reloads the page. Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/#findComment-907673 Share on other sites More sharing options...
bluebyyou Posted August 27, 2009 Share Posted August 27, 2009 please post your code Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/#findComment-907826 Share on other sites More sharing options...
akitchin Posted August 27, 2009 Share Posted August 27, 2009 Ideal situation: Member answers form 1 --> Form 2 comes up (answers form 2) --> Code is executed based on what they chose on form 2 (this code involves $_SESSIONs) What it is doing is this: Member answers form 1 --> Form 2 comes up (with different action) --> Send to enemy.php --> Redirect back to original page --> Now the original page is reloaded so I cannot execute the code based on form 2!!!! What if form 1 was a $_GET form? >.> <.< then I could redirect them to...the page they chose? :shrug: why exactly is the form sending to enemy.php and redirecting? how are you trying to process the forms on fcenter.php? if the first form is sent, then $_POST['choose'] will be set. to execute the code for form1 on submission, use: if (isset($_POST['choose'])) { // form1 execution } if the second form is sent, then $_POST['confirm'] will be set, but not $_POST['choose']. for the form2 code execution, you can use: if (isset($_POST['confirm'])) { // form2 execution } these can easily go on the same page in the header, and only one statement will process on any given page submission. in fact, to make it all streamlined, you could go: if (isset($_POST['choose'])) { // form1 was submitted } elseif (isset($_POST['confirm'])) { // form2 was submitted } else { // nothing was submitted } perhaps i'm missing something here.. EDIT: note that if you want the information from form1 to post along with the information selected/posted from form2, you will need to add hidden inputs into form2's code to plug that information in. Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/#findComment-907835 Share on other sites More sharing options...
Cetanu Posted August 27, 2009 Author Share Posted August 27, 2009 Actually, I still haven't found the problem, but I've worked around it. I may never know why it refused to work. o_o Quote Link to comment https://forums.phpfreaks.com/topic/172024-solved-two-forms-on-one-page/#findComment-907945 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.