vendredi 27 mars 2015

Selected value in dropdown does not seem to be posted

If this problem/question has already been answered then my apologies. I was not able to find an answer for my problem with an HTML dropdown.


I have a form with multiple input fields and text areas and whenever I post this form all these values will get posted. However, I also have a dropdown that I create from my database. This dropdown shows up properly but when I post the form this is the only $_POST variable in the form that does not seem to get posted. This form is used to edit an existing object and make sure the category that object has is automatically selected.


Below is the part of my form with the dropdown:



<?php if (isset($_SESSION['categoryselect'])) {

$allcategory = $_SESSION['categoryselect'] ?>
<select name="selectedcategory" required="required">

<?php foreach($allcategory as $category) {

if ($category->sub_categorie == $selectedproduct->sub_category) {?>

<option value="<?php $category->sub_category?>"selected="selected">
<?php echo $category->sub_category?>
</option>

<?php } else {?>

<option value="<?php $category->sub_category?>">
<?php echo $category->sub_category?>
</option>

<?php }
} ?>
</select>
<?php } ?>


Below is a part of the code that handles the posted form:



$name= $_POST['name'];
$name= mysql_escape_string($name);

$sub_category = $_POST['selectedcategory'];
$sub_category = mysql_escape_string($sub_category);

$price= $_POST['price'];
$price= mysql_escape_string($price);


The posts for 'name' and 'price' are also in the same form with my 'category' These 2 fields do get posted. My 'category' however gives an undefined index error. As far as I know this means that the 'category' does not get posted. But this made me wondering why the other values do get posted and this particular value from a dropdown does not.


Aucun commentaire:

Enregistrer un commentaire