Movies32 Posted April 3, 2013 Share Posted April 3, 2013 (edited) I'm taking this class and we've just started to learn PHP so I am really new to the subject. I have done some coding and have been emailing my instructor (he has not responded yet) so please do not just click the back button thinking all I want is the answer. Here is what I need to accomplish: Create a "self-processing" PHP page that will initially just display a simple form with a drop-down select box that lets the user choose between the feedback and help request forms. When the user makes a selection, instead of using JavaScript to dynamically build or reveal the appropriate submission form, the form will be submitted back to the same page on the server for further processing. The PHP page will use a PHP script to check if the user has submitted a choice, and if so it will dynamically display the appropriate form. This is what my assignment looks like so far: http://www2.esc.edu/jvooris/assignment5-2.php As you can see, the forms are already displayed even without selecting an option from the drop down menu. What I need to have happen is have that drop down menu appear and then have the appropriate form display after the user clicks the submit button. So if the user clicks the submit button when the option is still on "Please select the form you want to fill out" then nothing should be displayed. However, if the user clicks the submit button when they select the option "Feedback Form" then that form will appear on the page and the same goes for the "Help Form". When I emailed my instructor he said that I need to: "Create a PHP page with a form with a drop down menu and a submit button that has a action to post to itself" and "Add PHP code at the top using the isset method to test to see if the page was submitted to itself and if your drop down menu variable was sent." Like I said, I do have some code already written. My problem is trying to figure out how to make it work correctly and I can't tell if I am way off base with the code I already have or if I just need to tweak a few things. I would appreciate any help in this matter and I hope I did not do too much explaining to deter anyone. Thank you. I will post my code below (I have cut out the code for the forms to shorten the code). <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>PHP Form</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <link rel="stylesheet" href="styles.css" type="text/css" /> <!--This links the entire page to the CSS style sheet. The font sizes, colors, colors on the page, etc. are all created on the attached style sheet--> </head> <body> <div id="wrapper"> <!--This statement makes the entire page align to a specific setting as set in the CSS.--> <!--This code first appears on the page to allow the user to select an option--> <form action="assignment5-2.php" name="select"> <p><select name="formChoice" size="1"> <option value="0">Please select the form you want to fill out</option> <?php $formChoice=$_GET["formChoice"]; echo $formChoice; if($formChoice==1)echo "<option value=1 selected='selected'>Feedback Form</option>"; else echo "<option value=1>Feedback Form</option>"; if($formChoice==2)echo "<option value=2> selected='selected'>Help Form</option>"; else echo "<option value=2>Help Form</option>"; ?> <input type="submit" value="Submit"/> </select> </form> <form id="feedbackForm" action="assignment5-2.php" method="GET"> <h1>User Feedback</h1> Edited April 3, 2013 by Movies32 Quote Link to comment Share on other sites More sharing options...
Yohanne Posted April 4, 2013 Share Posted April 4, 2013 (edited) use foreach statement/tag Edited April 4, 2013 by jayson_ph Quote Link to comment Share on other sites More sharing options...
exeTrix Posted April 4, 2013 Share Posted April 4, 2013 Hi and welcome to the forum. It's good to hear that you've decided to learn PHP! Looks like you're missing an if statement. I won't do it for you because it may hamper the learning process but I'll give you n example of what need to happen server side and I'm sure you'lll be able to fill in the blanks //first we'll test to see if the form has not been submitted if( !isset( $_GET['formChoice'] ) ){ //display the message echo 'Please make a selection'; }else{ //add the selection to a variable with type int $formChoice = (int) $_GET['formChoice']; if( $formChoice === 1 ){ ?> <!-- the HTML code for feedback form goes here --> <?php }elseif( $formChoice === 2 ){ ?> <!-- the HTML code for help form goes here --> <?php }else{ ?> <!-- HTML messge to say invalid selection goes here --> <?php } } All we're doing here is first making sure the form has been submitted, then we're testing the value submitted by the user to identify which form should be displayed. Hope that helps Quote Link to comment Share on other sites More sharing options...
Movies32 Posted April 4, 2013 Author Share Posted April 4, 2013 Hi and welcome to the forum. It's good to hear that you've decided to learn PHP! Looks like you're missing an if statement. I won't do it for you because it may hamper the learning process but I'll give you n example of what need to happen server side and I'm sure you'lll be able to fill in the blanks //first we'll test to see if the form has not been submitted if( !isset( $_GET['formChoice'] ) ){ //display the message echo 'Please make a selection'; }else{ //add the selection to a variable with type int $formChoice = (int) $_GET['formChoice']; if( $formChoice === 1 ){ ?> <!-- the HTML code for feedback form goes here --> <?php }elseif( $formChoice === 2 ){ ?> <!-- the HTML code for help form goes here --> <?php }else{ ?> <!-- HTML messge to say invalid selection goes here --> <?php } } All we're doing here is first making sure the form has been submitted, then we're testing the value submitted by the user to identify which form should be displayed. Hope that helps I just made some changes and now nothing is appearing at all on the webpage. Am I nesting the PHP statements the wrong way or what is going on? Here is my code (edited to reduce the length): !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>PHP Form</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <link rel="stylesheet" href="styles.css" type="text/css" /> <!--This links the entire page to the CSS style sheet. The font sizes, colors, colors on the page, etc. are all created on the attached style sheet--> </head> <body> <div id="wrapper"> <!--This statement makes the entire page align to a specific setting as set in the CSS.--> <!--This code first appears on the page to allow the user to select an option--> <form action="assignment5-2.php" name="select"> <p><select name="formChoice" size="1"> <option value="0">Please select the form you want to fill out</option> <?php $formChoice=$_GET["formChoice"]; echo $formChoice; if($formChoice==1)echo "<option value=1 selected='selected'>Feedback Form</option>"; else echo "<option value=1>Feedback Form</option>"; if($formChoice==2)echo "<option value=2 selected='selected'>Help Form</option>"; else echo "<option value=2>Help Form</option>"; ?> <input type="submit" value="Submit"/> </select> </form> <?php if( !isset( $_GET['formChoice'] ) ) if $formChoice==1 ?> <form id="feedbackForm" action="assignment5-2.php" method="GET"> <h1>User Feedback</h1> <p>Please provide feedback about my website</p> <div id="content"> <?php end if ?> <?php if $formChoice==2 ?> <!--This code below will be displayed when a user selects the help option on the website--> <form id="supportForm" action="assignment5-2.php" method="GET"> <h1>Help Request</h1> <p>Enter your help request and a representative will contact you</p> <?php end if ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Movies32 Posted April 7, 2013 Author Share Posted April 7, 2013 Please can anyone help? I'm stuck with nothing showing up on the webpage. Please read my previous post above. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 7, 2013 Share Posted April 7, 2013 Yes, your syntax is way off. http://php.net/manual/en/control-structures.alternative-syntax.php Also, if you enable error reporting you would see the errors. Quote Link to comment Share on other sites More sharing options...
Movies32 Posted April 7, 2013 Author Share Posted April 7, 2013 So I just tried to change the PHP statements and there is still nothing showing up on the webpage. Does my very first PHP code need to be changed or is it the other two PHP statements that display the forms? I even tried to put in an "elseif" for the other two PHP statements but that did not work. Which part needs to be changed? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>PHP Form</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <link rel="stylesheet" href="styles.css" type="text/css" /> <!--This links the entire page to the CSS style sheet. The font sizes, colors, colors on the page, etc. are all created on the attached style sheet--> </head> <body> <!--This code first appears on the page to allow the user to select an option--> <form action="assignment5-2.php" name="select"> <p><select name="formChoice" size="1"> <option value="0">Please select the form you want to fill out</option> <?php $formChoice=$_GET["formChoice"]; echo $formChoice; if($formChoice==1)echo "<option value=1 selected='selected'>Feedback Form</option>"; else echo "<option value=1>Feedback Form</option>"; if($formChoice==2)echo "<option value=2 selected='selected'>Help Form</option>"; else echo "<option value=2>Help Form</option>"; ?> <input type="submit" value="Submit"/> </select> </form> <?php if ($formChoice == 1): ?> <form id="feedbackForm" action="assignment5-2.php" method="GET"> <h1>User Feedback</h1> <p>Please provide feedback about my website</p> <?php end if; ?> <?php if ($formChoice == 2): ?> <!--This code below will be displayed when a user selects the help option on the website--> <form id="supportForm" action="assignment5-2.php" method="GET"> <h1>Help Request</h1> <?php end if; ?> </body> </html> Yes, your syntax is way off.http://php.net/manual/en/control-structures.alternative-syntax.phpAlso, if you enable error reporting you would see the errors. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 7, 2013 Share Posted April 7, 2013 You NEED to enable error reporting. It's rude to ask us to keep reading all your code to find your syntax errors when you could get the actual error yourself. Quote Link to comment Share on other sites More sharing options...
Movies32 Posted April 7, 2013 Author Share Posted April 7, 2013 Enable error reporting in what?? I am using an HTML editor not a PHP editor and any errors that do come up are only HTML errors not PHP errors. So I really do have no idea if my PHP is correct or not without some help. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 7, 2013 Share Posted April 7, 2013 (edited) http://us.php.net/manual/en/function.error-reporting.php http://us.php.net/manual/en/errorfunc.configuration.php#ini.display-errors Edited April 7, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
Movies32 Posted April 7, 2013 Author Share Posted April 7, 2013 (edited) I have now gotten the page to work like it's supposed to. However, the next part of my assignment requires me to validate my form. For example, if someone does not enter their name when they click submit, then a message should show up saying that they need to provide one. The error I am getting is: Undefined index: UserName Do I need to link my two pages together somehow in order to stop this error or do I need to change how I am validating the form? Here is my code where the user can enter their name: <div class="myRow"> <label class="labelCol" for="UserName">Name*:</label> <input type="text" name="UserName" id="UserName" size="30" /> </div> Here is my PHP code for validating their entry: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Form To Email</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <?php $name = $_POST['UserName']; $tried = ($_POST['tried'] == 'yes'); if ($tried) { $validated = (!empty($name)); if (!$validated) { ?> <p>Name field cannot be empty.</p> <?php } } ?> </body> </html> Edited April 7, 2013 by Movies32 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.