Page 1 of 1
Why won't php print my drop down menu?
Posted: Wed Jul 08, 2009 6:15 pm
by NewWebDesigner
Hello all,
I have been trying to get PHP to print this drop down box and I keep getting an error message that reads:
Parse error: parse error, expecting `','' or `';'' in C:\wamp\www\expirement.php on line 12
Can anybody point me in the right direction as to what the problem is?
here is my code:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo "<form >";
echo "<select action="">";
echo "<option value="volvo">Volvo</option>";
echo "<option value="saab">Saab</option>";
echo "<option value="fiat" selected="selected">Fiat</option>";
echo "<option value="audi">Audi</option>";
echo "</select>";
echo "</form>";
?>
</body>
</html>
Re: Why won't php print my drop down menu?
Posted: Wed Jul 08, 2009 6:52 pm
by califdon
You can't use doublequotes within doublequotes like that. It will need to be like:
Code: Select all
echo "<select action=''>";
echo "<option value='volvo'>Volvo</option>";
or
Code: Select all
echo "<select action=\"\">";
echo "<option value=\"volvo\">Volvo</option>";
I recommend the former.
Re: Why won't php print my drop down menu?
Posted: Wed Jul 08, 2009 7:20 pm
by NewWebDesigner
Thank you for helping me. My code works perfectly now.
Re: Why won't php print my drop down menu?
Posted: Thu Jul 09, 2009 11:13 pm
by NewWebDesigner
Thanks guys.
I changed my code around a bit. now I have a database of States and the the colleges in them. I want a user to select the state via a drop down menu. Then when the user goes to select a college, they are presented with only the colleges that correspond to that state.
My problem is that I can't figure out the syntax to get the colleges to appear in a drop down menu without getting errors.
Here is the error message I get:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\Drop.php on line 49
(The error refers to the PHP code)
This is my HTML code:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
select your state:
<form action="Drop.php" method="post">
<select name="State">
<option value="NY">NY</option>
<option value="ND">ND</option>
</select>
<input type="submit" />
</form>
<form action="" method="">
<select name="College">
<option value="Select College">Select College</option>
</select>
<input type="submit" />
</form>
</body>
</html>
Here is my php code:
Code: Select all
Welcome <?php echo $_POST["State"]; ?>!<br />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
select your state:
<form action="Drop.php" method="post">
<select name="State">
<option value="NY">NY</option>
<option value="ND">ND</option>
</select>
<input type="submit" />
</form>
<?php if (isset($_POST['State'])){
$state = $_POST['State'];
echo "Country Selected: ". $state . "<br />";
}?>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("books", $con);
$result = mysql_query("SELECT * FROM schools WHERE State='$state'");
echo "<form >
<select action=''>";
while($row = mysql_fetch_array($result))
{
echo "<option value='$row['School']'>" . $row['School'] . "</option";
}
echo " </select>";
echo " </form>";
?>
</body>
</html>
Re: Why won't php print my drop down menu?
Posted: Fri Jul 10, 2009 4:28 am
by mattpointblank
Line 49 in your code above is this:
Code: Select all
echo "<option value='$row['School']'>" . $row['School'] . "</option";
You can't use array references (eg the $row variable) within an echo, you have to concatenate them - this just means mixing 'normal' output with variable output, using periods to concatenate. Like this:
Code: Select all
echo "<option value='" . $row['School'] . "'> " . $row['School'] . "</option>";
You managed it on the the second one, but not the first - look at the syntax highlighting! It does get a little confusing outputting HTML in this way, the mixture of double/single quotes etc can be quite tricky, but you'll get it. Also, you missed the closing < off your option tag, so I added that too.
Try reading the error messages php is giving you a little more closely. They tell you pretty much what character(s) it expects and where you went wrong.