LowEndTheory Posted January 6, 2010 Share Posted January 6, 2010 I'm pretty new with php. Generally only done some mailform scripts and Wordpress tweaks. I'm trying to create a form that will allow a user to input a value into a text field then pass that value to a textbox on another page. I have been able to accomplish this. However, I want to give them two choices on the second page using two buttons and have the values for those buttons passed to a third page which will then display info corresponding to which choice they picked using an if/else statement. My code for the 3 pages are as follows: Page 1: <form id="ask-me" action="ask-me-pg2.php" method="post"> <p>What's your question? <input type="text" name="Question" /></p> <p><input type="submit" name="Form_Submit" value="Send" /></p> </form> Page 2: <?php $QUESTION=$_REQUEST['Question']; echo<<<TEXT <form id="ask-me2" action="ask-me-pg3.php" method="post"> <p><textarea name="question-box" id="question-box" cols="45" rows="5">$QUESTION</textarea></p> <p>How would you prefer to be contacted? <input type="submit" name="Contact_Choice" value="Email"/> <input type="submit" name="Contact_Choice" value="Phone"/></p> </form> TEXT; ?> Page 3: <?php $QUESTION2=$_REQUEST['question-box']; $CONTACT=$_REQUEST['Contact_Choice']; if($CONTACT == "Email"){ echo<<<TEXT <form id="ask-me3" actions="ask-me-pg3.php" method="post"> <p><textarea name="question-box" id="question-box" cols="45" rows="5">$QUESTION2</textarea></p> <p><input type="submit" name="Email" value="Email"/></p> <p>EMAIL BUTTON WORKS!</p> </form> TEXT; } else if($CONTACT == "Phone"){ echo<<<TEXT <form id="ask-me3" actions="ask-me-pg3.php" method="post"> <p><textarea name="question-box" id="question-box" cols="45" rows="5">$QUESTION2</textarea></p> <p><input type="submit" name="Phone" value="Phone"/></p> <p>PHONE BUTTON WORKS!</p> </form> TEXT; } ?> Many thanks for taking a look at this. Link to comment https://forums.phpfreaks.com/topic/187444-need-some-help-passing-values/ Share on other sites More sharing options...
PHP Monkeh Posted January 6, 2010 Share Posted January 6, 2010 I'd suggest using radio buttons instead of submit buttons for users to make a choice between things like this. However, to answer your question. You nearly have it, except a few things I'd like to point out. 1. $_REQUEST Although valid, it isn't recommended. You know the form method is "post", so use $_POST instead. 2. Avoid using echo<<< as it really doesn't read that well. Try and get in to the habbit of ending php tags and then beginning them again once you need them. For example, I'd write your page 2 like this: <?php $question = $_POST['Question']; ?> <form id="ask-me2" action="ask-me-pg3.php" method="post"> <p><textarea name="question-box" id="question-box" cols="45" rows="5"><?php echo $question; ?></textarea></p> <p>How would you prefer to be contacted? <input type="submit" name="Contact_Choice" value="Email"/> <input type="submit" name="Contact_Choice" value="Phone"/></p> </form> 3. The overall structure of the process isn't that great, all of this could be rolled in to one page rather than having it in 3 different files. Look in to submitting forms to the same page (using $_SERVER['PHP_SELF'] as the action) and then by using isset($_POST['submitButtonName']) to see whether the form has been submitted. 4. Try changing your page 3 code to this and see whether it works: <?php $question = $_POST['question-box']; $contact_choice = $_POST['Contact_Choice']; ?> <form id="ask-me3" actions="ask-me-pg3.php" method="post"> <p><textarea name="question-box" id="question-box" cols="45" rows="5"><?php echo $question; ?></textarea></p> <?php if($contact_choice == "Email") { ?> <p><input type="submit" name="Email" value="Email"/></p> <p>EMAIL BUTTON WORKS!</p> <?php } elseif($contact_choice == "Phone") { ?> <p><input type="submit" name="Phone" value="Phone"/></p> <p>PHONE BUTTON WORKS!</p> <?php } ?> </form> Link to comment https://forums.phpfreaks.com/topic/187444-need-some-help-passing-values/#findComment-989810 Share on other sites More sharing options...
LowEndTheory Posted January 6, 2010 Author Share Posted January 6, 2010 Monkeh, Thanks so much for this. I definately have to look into rolling it into one page. I know it's not the prettiest code by a long shot. Just trying to make it functional at this point. Thanks again for helping a noob like me! Link to comment https://forums.phpfreaks.com/topic/187444-need-some-help-passing-values/#findComment-989820 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.