Jump to content

I'm having such a problem with validation and arrays. :(


Dtp1337
 Share

Recommended Posts

Create an associative array that will be used in a form for a dropdown box for user to pick their favorite color. The entries should be:RedBlueGreenCyanMagentaYellowCreate a second associative array that will populate a set of radio buttons. The buttons should ask the user to choose their favorite type of food from following list.BarbequeChineseJapaneseThaiSteakVeganMexicanThe form should also ask for first and last name. Data should be validated. If invalid data is entered, and error should be displayed, otherwise, output the data. I have to create a form that sends the answers to the php. I don't understand where to put the arrays/ I have this but it don't seem right for the dropdown box.Even so after getting it to work how do I valid the information of a dropbox or radio button. I'm so confused. <body> <?php$color = array( 0=>"Red", 1=>"Blue", 2=>"Green", 3=>"Cyan", 4=>"Magenta", 5=>"Yellow");$food = array(0=>"Barbeque", 1>="Chinese", 2="Japanese", 3=>"Thai", 4=>"Steak", 5=>"Vegan", 6=>"Mexican"); $color = str_repalce(" "," ",$color);$food = str_replace(" "," ", $food);echo '<SELECT name=color>';echo '<SELECT name=food>';foreach ($color as $pick => $value){echo '<OPTION value='.$value.'>'.$value.;}echo '</select>'; ?> <form action="CISP1720-dpatter3-Lab7.php" method="post"> <p> Please provide your First Name. <input name="Fname" type="text" /><p/> <p> Please provide your Last Name. <input name="Lname" type="text" /><p/> <input type="submit" name="Submit" id="Submit" value="Submit"></body></html>

Link to comment
Share on other sites

check using in_array() that submite field value is in array or not, if not show the errors. drop down menu should be inside the form tag. you need two loop to show two drop down for color and food. when form get submited the submited values are available inrto $_POST and $_GET array depending upon the method form submited or page requested.

  • Like 1
Link to comment
Share on other sites

? This is what i've come up so far for the names and first array. It gives me a unexpected end error... !DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title> Lab 8 </title></head><body><form action =" " method="post"><h1> Enter your First and Last name, then select your favorite color and food.</h1> <p> Please provide your First Name. <input name="Fname" type="text" /><p/> <p> Please provide your Last Name. <input name="Lname" type="text" /><p/> <h1> Choose your favorite color. </h1><?php $color["R"]="Red"; $color["B"]="Blue"; $color["G"]="Green"; $color["C"]="Cyan"; $color["M"]="Magenta"; $color["Y"]="Yellow"; ?> <select name = "color" id = "color"><?php foreach($color as $pick => $favorite) { echo "<option value=\"$favorite\"> $favorite </option> /n";?></select> <input type="submit" name="Submit" id="Submit" value="Submit"></form></body></html>

Edited by Dtp1337
Link to comment
Share on other sites

lol found that already lol thanks now I need to validate that the color and the food is selected when submitted. How and where would i palce that in my php form. This is what I have for it. Also something I found that may conflict with validing the use of the drop list, would i need to make an empty entry into the list before "Red" so that it will force the user to select one unless they over look it completely. edit#1-5 : Also I have an extra empty drop box between my droplist and buttons. Why is it there. <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title> Lab 8-F </title></head><body><form action =" " method="post"><h1> Enter your First and Last name then select your favorite color and food.</h1> <p> First Name: <input name="Fname" type="text" /><p/> <p> Last Name: <input name="Lname" type="text" /><p/><?php $color["R"]="Red"; $color["B"]="Blue"; $color["G"]="Green"; $color["C"]="Cyan"; $color["M"]="Magenta"; $color["Y"]="Yellow"; ?> <select name = "color" id = "color"><?php foreach($color as $pick => $favorite) { echo "<option value=\"$favorite\"> $favorite </option> \n"; }?></select> <?php $food["B"]="Barbeque"; $food["C"]="Chinese"; $food["J"]="Japanese"; $food["T"]="Thai"; $food["S"]="Steak"; $food["V"]="Vegan"; $food["M"]="Mexican"; ?> <select name = "food" id = "food"><?php foreach($food as $pick1 => $favorite1) { echo "<input type=\"radio\" name=\"favorite1\" value=\"favorite1\"> $favorite1\n"; }?> </select> <input type="submit" name="Submit" id="Submit" value="Submit"></form></body></html> ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- <html><head><meta a http-equiv="Content-Type" content="text/html; charset=utf-8"><title> CISP1720 Lab 8</title></head> <h1> Lab 8 </h1><h3> </h3><body> <?php$Fname = $_POST['Fname'];$Lname = $_POST['Lname'];$color = $_POST['color'];$food = $_POST['food'];if(empty($Fname) || empty($Lname)) {echo <p> Please do not leave fields blank. </p>; } else if (is_numeric($Fname) || is_numeric(Lname)) { echo <p> Please do not use numbers in your name. </p>; } else if ( ?> </body></html>

Edited by Dtp1337
Link to comment
Share on other sites

name=\"favorite1\"should bename=\"food\" andvalue=\"favorite1\"should bevalue=\"$favorite1\" in:

echo "<input type=\"radio\" name=\"favorite1\" value=\"favorite1\"> $favorite1\n";

the empty <select> is because of:

<select name = "food" id = "food">

and

</select>

for validating the color and food you can use in_array() as birbal said.http://php.net/manua...on.in-array.php you could put the $color and $food arrays in 1 file, then include that file from your current 2 files (to save duplicating them)http://php.net/manua...ion.include.php

Edited by JamesB
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...