Reputation: 111
I'm trying to populate a dropdown box that retrieve database from MySQL. Here are my form and php files below. It looks alright to me, but I don't get why it doesn't show up any thing when I click on submit button. Is there anything I can do to fix this error? Any correction and suggestion would be appreciated.
CS_JOBS
table consists of: job_title
, category_code
(17, 27, 37....), ...etc.
connect.php works fine in this case.
MY FORM
<!DOCTYPE html>
<html>
<head></head>
<body>
<form action = "csjob.php" method = "POST" name = "jobsearch">
<select name = "category_code[]">
<option value = "17">Architecture and Engineering</option>
<option value = "27">Arts, Design, Entertainment, Sports, and Media</option>
<option value = "37">Building and Grounds Cleaning and Maintenance</option>
<option value = "13">Business and Financial Operations</option>
<option value = "21">Community and Social Services</option>
<option value = "15">Computer and Mathematical</option>
<option value = "47">Construction and Extraction</option>
<option value = "25">Education, Training, and Library</option>
<option value = "45">Farming, Fishing, and Forestry</option>
<option value = "35">Food Preparation and Serving Related</option>
<option value = "29">Healthcare Practitioner and Technical</option>
<option value = "31">Healthcare support</option>
<option value = "49">Installation, Maintenance, and Repair</option>
<option value = "23">Legal</option>
<option value = "19">Life, Physical, and Social Science</option>
<option value = "11">Management</option>
<option value = "43">Office and Administrative Support</option>
<option value = "39">Personal Care and Service</option>
<option value = "51">Production</option>
<option value = "33">Protective Service</option>
<option value = "41">Sales and Related</option>
<option value = "53">Transportation and Material Moving</option>
</select>
<input type="submit" value="Submit">
</form>
</body>
</html>
csjob.php
<?php
require("connect.php");
if(isset($_POST['submit'])){
$sql = "SELECT * FROM CS_JOBS WHERE category_code=".$_POST['category_code'];
$result = mysql_query($sql);
echo "<select name='category_code'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row['job_title']"'>".$row['job_title']"</option>";
}
echo "</select>";
}
?>
So many typos in this question, Here is my actual code that I upload: a link!
Upvotes: 0
Views: 525
Reputation: 11832
You have 3 small problems. First one is in the form:
<select name = "category_code[]">
should be
<select name="category_code">
So without the brackets []
in the name and no need for the extra spaces...
The brackets are only needed if you have an option that is has multiple select, and will return an array (hence the brackets). That isn't happening in your case.
And the second problem is what is said before: in the PHP:
$row['job_title'}
should be
$row['job_title']
So change }
with ]
Your third problem has to do with checking the submit:
if(isset($_POST['submit'])){
should be
if(isset($_POST['category_code'])){
Because you don't have a field with the name 'submit'. You have a field with id submit and value submit. But no name submit. You can add name='submit'
to the submit button. Or just go with my proposed change above...
Upvotes: 2
Reputation: 524
<?php
require("connect.php");
if(isset($_POST['submit']))
{
$sql = "SELECT * FROM CS_JOBS WHERE category_code=".$_POST['category_code'];
$result = mysql_query($sql);
if( mysql_num_rows( $result ) > 0 ){
echo "<select name='category_code'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='".$row['job_title']"'>".$row['job_title']"</option>";
}
echo "</select>";
}
}
?>
Upvotes: 1
Reputation: 360602
Your code makes no sense. You run the query if a particular form field is present, but only run the "display results" code when that same form field is NOT present.
Easiest fix: REMOVE the } else {
...
Upvotes: 0