Help - Search - Members - Calendar
Full Version: option boxes (drop down boxes)
HTMLHelp Forums > Programming > Server-side Scripting
joeysleeper
I'm doing a very simple search form where someone would select a state from a drop down box , click 'submit', and see info from a MySQL table. Can I (should I?) assign a variable to the 'select' tag that can be sent to the PHP query? If so, what is the proper syntax?

lines from HTML file:

<form action="searchMyTable.php" method="post">

<select name="state" id="state">
<option value=" AL "> AL </option>
<option value=" AK "> AK </option>
<option value=" AZ "> AZ </option>
<option value=" AR "> AR </option>
<input type="submit" value="Search!"/>

</form>


Lines from PHP file:

<?php

$con = mysql_connect("localhost","userid","name");
if (!$con)
{
die('Could not connect to it: ' . mysql_error());
}
mysql_select_db("MyDatabase", $con);

$result = mysql_query("SELECT * From MyTable Where ColumnState = "state");

while ($row = mysql_fetch_array($result))

mysql_close($con);

?>

Brian Chandler
QUOTE(joeysleeper @ Mar 21 2007, 12:33 AM) *

I'm doing a very simple search form where someone would select a state from a drop down box , click 'submit', and see info from a MySQL table. Can I (should I?) assign a variable to the 'select' tag that can be sent to the PHP query? If so, what is the proper syntax?

lines from HTML file:

<form action="searchMyTable.php" method="post">

<select name="state" id="state">
<option value=" AL "> AL </option>
<option value=" AK "> AK </option>
<option value=" AZ "> AZ </option>
<option value=" AR "> AR </option>
<input type="submit" value="Search!"/>

</form>



Right - this will send POST data including a name-value pair (e.g. state=AZ)


QUOTE


Lines from PHP file:

<?php

$con = mysql_connect("localhost","userid","name");
if (!$con)
{
die('Could not connect to it: ' . mysql_error());
}
mysql_select_db("MyDatabase", $con);

$result = mysql_query("SELECT * From MyTable Where ColumnState = "state");

while ($row = mysql_fetch_array($result))

mysql_close($con);

?>


But this has not accessed the POST data at all. When you correct the syntax error (odd number of "), you will get the database info where the value of ColumnState is 'state' - and there aren't any states called 'state'. You need to refer to $_POST['state']. (Please look for a tutorial or the manual for details.)





joeysleeper
Man this is confusing! I've changed my PHP script to add $_POST, but I'm still not getting any output. Have I styled the $_POST and SELECT lines correctly?

<?php

$con = mysql_connect("localhost","userid","name");
if (!$con)
{
die('Could not connect to it: ' . mysql_error());
}
mysql_select_db("MyDatabase", $con);

$state=$_POST['state']

$result = mysql_query("SELECT * From MyTable Where CompanyState = $state ");

while ($row = mysql_fetch_array($result))

$CompanyName = $row["CompanyName"];
$ContactFirstName = $row["ContactFirstName"];
$ContactLastName = $row["ContactLastName"];
$CompanyStreetAddress = $row["CompanyStreetAddress"];
$CompanyCity = $row["CompanyCity"];
$CompantState = $row["CompanyState"];
$CompanyZip = $row["CompanyZip"];
$CompanyPhone = $row["CompanyPhone"];
$ComapnyFax = $row["CompanyFax"];
$CompanyEmail = $row["CompanyEmail"];
$CompanyWebsite = $row["CompanyWebsite"];
$CompanyAreaServed = $row["CompanyAreaServed"];
echo "$CompanyName<br>$ContactFirstName<br>$ContactLastName <br>$CompanyStreetAddress <br>$CompanyCity <br>$CompantState <br>$CompanyZip <br>$CompanyPhone <br>$ComapnyFax <br>$CompanyEmail <br>$CompanyWebsite <br>$CompanyAreaServed ";

mysql_close($con);

?>
Brian Chandler
Excuse me skipping the 'proper' quoting... (I'm *** )


Man this is confusing! I've changed my PHP script to add $_POST, but I'm still not getting any output. Have I styled the $_POST and SELECT lines correctly?

*** Firstly, "style" isn't the right word. You don't 'style' bits of program - this is not the half-baked world of CSS. You have to write a bit of php to be syntactically correct, and with the semantics specified in the manual (which is very good, btw: http://www.php.net/manual/en/ )

<?php

$con = mysql_connect("localhost","userid","name");
if (!$con)
{
die('Could not connect to it: ' . mysql_error());
}
mysql_select_db("MyDatabase", $con);

$state=$_POST['state']

*** Missing a semicolon. But this is the point to find out what's going on by _debugging_. Add echo "<br>State: $state\n"; to see what value is being passed. Then you know whether the problem is upstream or downstream of this point.


$result = mysql_query("SELECT * From MyTable Where CompanyState = $state ");

while ($row = mysql_fetch_array($result))

$CompanyName = $row["CompanyName"];
$ContactFirstName = $row["ContactFirstName"];
$ContactLastName = $row["ContactLastName"];
$CompanyStreetAddress = $row["CompanyStreetAddress"];
$CompanyCity = $row["CompanyCity"];
$CompantState = $row["CompanyState"];
$CompanyZip = $row["CompanyZip"];
$CompanyPhone = $row["CompanyPhone"];
$ComapnyFax = $row["CompanyFax"];
$CompanyEmail = $row["CompanyEmail"];
$CompanyWebsite = $row["CompanyWebsite"];
$CompanyAreaServed = $row["CompanyAreaServed"];
echo "$CompanyName<br>$ContactFirstName<br>$ContactLastName <br>$CompanyStreetAddress <br>$CompanyCity <br>$CompantState <br>$CompanyZip <br>$CompanyPhone <br>$ComapnyFax <br>$CompanyEmail <br>$CompanyWebsite <br>$CompanyAreaServed ";

mysql_close($con);

*** Offhand I can't see the problem. What actually happens?
joeysleeper
What it was doing was that when I selected a state from the drop down, no data was returned. What I discovered was that in my 'options' I had spaces in the option value, so I took them out. Now it retrieves data, but I'm getting some error messages I need to work through.

with spaces:
<option value=" AL "> AL </option>

without spaces:
<option value="AL"> AL </option>

thx a bunch! I love this place.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.