PDA

View Full Version : Problem receiving variable from drop down box


whizard
07-06-2006, 04:44 PM
I am having two problems with what should be a simple script I wrote. Its purpose is to receive a POST variable (an id number) from a drop down box and then match that number against a database and retrive the data that relates to the id number. [code below]

The first problem is this: For some reason, the form processing page isn't receiving the variable when the form is submitted. Secondly, (and this may be caused by the first problem), I get this error message whenevr I run the script:

Parse error: syntax error, unexpected T_STRING in C:\WEB_FILES\trip_log\includes\place_info.inc on line 37

Form (pulls values from DB and puts them in drop down box):

<form name="FindPlace" action="?page=place_info" method="post">
<label>
Place Name:
<select name="place">
<option value=""></option>
<?php
require("includes/db/dbconnection.inc");
$connect = mysql_connect($dbserver,$dbuser,$dbpass) or die("Could not connect to MySQL");
$db = mysql_select_db($dbselect,$connect) or die("Could not connect to database \"<strong>$dbselect</strong>\"");
$query = ("SELECT `id`, `place_name` FROM `places` ORDER BY `place_name` ASC");
$results = mysql_query($query);
echo mysql_error();

if (mysql_Numrows($results)>0)
{
$numrows=mysql_NumRows($results);
$x=0;
while ($x<$numrows)
{
$id=mysql_result($results,$x,id);
$name=mysql_result($results,$x,place_name);
echo "<option value=\"$id\">$name</option>\n";
$x++;
}
}
?>
</select>
</label>
<input class="homeholderbox" name="FindPlaceSubmit" type="submit" value="Go" />


Data Processing Code (Receives variable from form and pulls related info from DB):
<?php
$id = $_POST['place'];
if($id == "")
{
?>
<script type="text/javascript">
alert("You must select a place");
history.go(-1);
</script>
<?php
}
else
{
require("includes/db/dbconnection.inc");
$connect = mysql_connect($dbserver,$dbuser,$dbpass) or die("Could not connect to MySQL");
$db = mysql_select_db($dbselect,$connect) or die("Could not connect to database \"<strong>$dbselect</strong>\"");
$query = ("SELECT * FROM `places` WHERE `id` = $id);
$results = mysql_query($query);
echo mysql_error();
if (mysql_Numrows($results)>0)
{
$numrows=mysql_NumRows($results);
$x=0;
while ($x<$numrows)
{
$place = mysql_result($results,$x,place_name);
$address = mysql_result($results,$x,place_address);
$city = mysql_result($results,$x,place_city);
$state = mysql_result($results,$x,place_state);
$zip = mysql_result($results,$x,place_zip);
$phone = mysql_result($results,$x,place_phone);
$x++;
}
}
}
?>
<div class="homeholder">
<h2 class="homeholdertitle">
Place Information: <?php print $place; ?>
</h2>
<p><?php print $phone; ?></p>
</div>

(Line 37 is this line: <div class="homeholder">)

Thanks in advance
Dan

whizard
07-06-2006, 04:48 PM
Wait.... now I see why the second error occurred. I forgot a " in my SQL query.

But the script still refuses to accept the variable from the drop-down box, and I get my javascript error box popping up telling me to select a place.

Dan

kehers
07-06-2006, 06:32 PM
Try this:

<form name="FindPlace" action="?page=place_info" method="post">
<label>
Place Name:
<select name="place">
<option value=""></option>
<?php
require("includes/db/dbconnection.inc");
$connect = mysql_connect($dbserver,$dbuser,$dbpass) or die("Could not connect to MySQL");
$db = mysql_select_db($dbselect,$connect) or die("Could not connect to database \"<strong>$dbselect</strong>\"");
$query = "SELECT id, place_name FROM places ORDER BY `place_name` ASC";//why the ()?
$results = mysql_query($query);
if(!$results)
echo mysql_error();

echo "The query returned".mysql_num_rows($results)."rows";
if (mysql_num_rows($results)>0){
while(list($id, $name) = mysql_fetch_row($results)){
echo "<option value=\"$id\">$name</option>\n";
}
}
?>
</select>
</label>
<input class="homeholderbox" name="FindPlaceSubmit" type="submit" value="Go" />

And let's see what happens.... :cool:
If the returned number of rows from the database is greater than 0 and you still get the JS alert, then check the values in tnhe id column of your table

whizard
07-06-2006, 06:39 PM
Thanks for replying! I was able to get it to work somehow, by messing around with the form and its variables.

Dan