The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> option boxes (drop down boxes), can a variable be assigned to an option box?
joeysleeper
post Mar 20 2007, 10:33 AM
Post #1





Group: Members
Posts: 7
Joined: 12-March 07
Member No.: 2,178



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

?>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 20 2007, 10:55 PM
Post #2


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



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





User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
joeysleeper
post Mar 21 2007, 11:24 PM
Post #3





Group: Members
Posts: 7
Joined: 12-March 07
Member No.: 2,178



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

?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Mar 22 2007, 06:04 AM
Post #4


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
joeysleeper
post Mar 22 2007, 08:40 AM
Post #5





Group: Members
Posts: 7
Joined: 12-March 07
Member No.: 2,178



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 27th April 2024 - 03:14 AM