I have a category list as a PHP function. I called this function in a form to display the options. However I cannot get the form to post the selected option to my database. I have searched SO and online but most questions relate to the values being a select option, which would be easier. However my list is quite long and may change. The other values from the form are sent to the database correctly, apart form the pictures which I am working on, however I cannot find a way to insert the selected value from the drop down list within the form.
This is my simple sql query to send information from the form to the database:
if (isset($_POST['submit']))
{
$title = $_POST['title'];
$subtitle = $_POST['subtitle'];
$description = $_POST['description'];
$category= $_POST['category'];
$pic1 = $_POST['pic1'];
$pic2 = $_POST['pic2'];
$pic3 = $_POST['pic3'];
$pic4 = $_POST['pic4'];
$pic5 = $_POST['pic5'];
$item_location = $_POST['location'];
$items_wanted = $_POST['wanted'];
mysql_query("INSERT INTO items_available (title, subtitle, description, category, image_path1, image_path2, image_path3, image_path4, image_path5, item_location, items_wanted) VALUES ('$title', '$subtitle', '$description', '$category', '$pic1', '$pic2', '$pic3', '$pic4', '$pic5', '$item_location', '$items_wanted')")or die(mysql_error());
header('Location: prompt.php?x=8');
}
This is the code for the field for category selection from my form which is a drop down list:
<div class="field">
<label for="category">Category:</label>
<select id=\"category\" name=\"category\" class=\"searchBox\">
<?php createCategoryList(); ?>
</select>
</div>
This is the code for the category list function:
//Creates Category <option>'s for search bar
function createCategoryList(){
if( ctype_digit($_GET['category']) ){ $x = $_GET['category']; }else{ $x = 999; }
echo "<option>All Categories</option>";
$i=0;
while(1){
if(numberToCategory($i)=="Category Does Not Exist"){
break;
}else{
echo " <option value=\"$i\" ";
if($i==$x){echo ' SELECTED ';}
echo " > ";
echo numberToCategory($i);
echo "</option>";
}
$i++;
}
}
//Category Number to String
function numberToCategory($n){
switch($n){
case 0:
$cat = "Antiques";
break;
case 1:
$cat = "Art";
break;
case 2:
$cat = "Automotive";
break;
case 3:
$cat = "Baby";
break;
case 4:
$cat = "Books";
break;
case 5:
$cat = "Business & Industrial";
break;
case 6:
$cat = "Cameras & Photo";
break;
case 7:
$cat = "Clothing & Accessories";
break;
case 8:
$cat = "Collectibles";
break;
case 9:
$cat = "Computers";
break;
case 10:
$cat = "Crafts";
break;
case 11:
$cat = "DVD's & Movies";
break;
case 12:
$cat = "Electronics";
break;
case 13:
$cat = "Health & Beauty";
break;
case 14:
$cat = "Home & Garden";
break;
case 15:
$cat = "Jewelry & Watches";
break;
case 16:
$cat = "Music";
break;
case 17:
$cat = "Pet Supplies";
break;
case 18:
$cat = "Services";
break;
case 19:
$cat = "Sports & Outdoors";
break;
case 20:
$cat = "Sports Memorabilia & Cards";
break;
case 21:
$cat = "Tools & Home Improvement";
break;
case 22:
$cat = "Toys & Hobbies";
break;
case 23:
$cat = "Video Games";
break;
case 24:
$cat = "Other";
break;
default:
$cat = "Category Does Not Exist";
}
Currently the numerical value of the selected option is passed i.e if I select the baby option from the drop down list, then the number 3 is entered into my database. How do I replace this numerical value with it's corresponding value as it appears on the drop down list?
All and any answers are gratefully appreciated.