Jump to content


Photo

Creating a PHP drop down menu

php menu drop down

  • Please log in to reply
10 replies to this topic

#1 Movies32

Movies32

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 03 April 2013 - 03:04 PM

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/...signment5-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 by Movies32, 03 April 2013 - 03:13 PM.


#2 JaysonDotPH

JaysonDotPH

    Advanced Member

  • Members
  • PipPipPip
  • 178 posts
  • LocationPhilippines

Posted 03 April 2013 - 07:30 PM

use 

foreach statement/tag

Edited by jayson_ph, 03 April 2013 - 07:30 PM.


#3 exeTrix

exeTrix

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts
  • LocationUK
  • Age:27

Posted 04 April 2013 - 01:40 AM

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 :)



#4 Movies32

Movies32

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 04 April 2013 - 01:26 PM

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>


#5 Movies32

Movies32

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 06 April 2013 - 11:25 PM

Please can anyone help?  I'm stuck with nothing showing up on the webpage.  Please read my previous post above.



#6 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 07 April 2013 - 07:53 AM

Yes, your syntax is way off.
http://php.net/manua...tive-syntax.php

Also, if you enable error reporting you would see the errors.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#7 Movies32

Movies32

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 07 April 2013 - 01:55 PM

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/manua...tive-syntax.php

Also, if you enable error reporting you would see the errors.

 



#8 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 07 April 2013 - 01:59 PM

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.


My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#9 Movies32

Movies32

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 07 April 2013 - 02:38 PM

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.



#10 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 07 April 2013 - 02:59 PM

http://us.php.net/ma...r-reporting.php

http://us.php.net/ma....display-errors


Edited by Jessica, 07 April 2013 - 02:59 PM.

My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#11 Movies32

Movies32

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 07 April 2013 - 05:37 PM

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 by Movies32, 07 April 2013 - 05:45 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com