我有下面的代码,我可以从mysql db的下拉列表中获取动态值,但单击提交按钮时无法打印所选值。
谁能紧急帮助我?
<?php
include("includes/config.inc.php");
$query = "SELECT * FROM category";
$result = mysql_query ($query);
echo "<select class='turnintodropdown' name='CategoryID' ><option value=''>All</option>";
while($r = mysql_fetch_array($result)) {
echo "<option value=".$r['CategoryID'].">".$r['CategoryName']."</option>";
}
echo "</select>";
if (isset($_POST['submit'])) {
$selected_val = $_POST['CategoryID']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
?>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
最佳答案
如果要预选的选定值,则可以使用以下代码。还要检查您的表单方法attuteute,看看它是否设置为post(注意,不建议使用mysql_*
函数,最好使用预处理语句使用PDO)。
while($r = mysql_fetch_array($result)) {
if (!empty($_POST['CategoryID']) && $_POST['CategoryID'] == $r['CategoryID']) {
$selected = 'selected="selected"';
} else {
$selected = '';
}
echo "<option ".$selected." value=".$r['CategoryID'].">".$r['CategoryName']."</option>";
}
关于php - 从动态下拉列表PHP MySQL中打印选定的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33680831/