可能重复:
Populating a dynamic drop down menu from a MySQL database
我的表单成功更新了mysql数据库,但是当用户重新登录时,我希望在下拉框中将数据库值指定为选定值,以便用户在之前选择的内容不起作用。
PHP:
<?php
session_start();
require_once("config.php");
if(!isset($_SESSION['username'])){
header('Location: login.php'); /
}
}
if(isset($_POST['submit'])){
$sql = "UPDATE user SET attendance1 = '" . mysql_real_escape_string($_POST['attendance1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET food1 = '" . mysql_real_escape_string($_POST['food1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET drink1 = '" . mysql_real_escape_string($_POST['drink1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
header("Location: thanks.html", true, 303);
}
$row2 = "SELECT * FROM user WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
$result = mysql_query($row2) or die("Error in SQL: " . mysql_error());
$row3 = mysql_fetch_array($result);
echo $row3['shade1'];
?>
FORM:
<form>
<input name="attendance1" type="radio" id="Yes" value="Yes" checked="checked"/>Yes
<br />
<input name="attendance1" type="radio" id="No" value="No" />No
</h3></td>
<td>
<select name="colour1" id="colour1" >
<option selected="selected">Please Select</option>
<option>Red</option>
<option>White</option>
<option>Green</option>
</select>
</td>
<td><h3>
<select name="shade1" id="shade1" >
<option selected="selected">Please Select</option>
<option value="Light" <?php if($row2['shade1']=="Light") { echo "selected"; }?>>Light</option>
<option value="Heavy" <?php if($row2['shade1']=="Heavy") { echo "selected"; }?>>Heavy</option>
</select>
<td> </td>
<td><label>
<input type="submit" name="submit" id="button" value="Submit" />
</label></td>
</tr>
</table>
</form>
答案 0 :(得分:0)
您可以使用JQuery:
1)使用$.ajax
中的$(document).ready()
电话获取您想要的值
2)将它们设置为:
假设您的<select></select>
元素的ID为“options”,请将其设置为当前选择的值,如下所示:
$("#options").val(optionToBeSelected);
其中optionToBeSelected
是您要加入的value
元素的<option></option>
属性。
此代码应该适合您。 如果有人在我的回答中看到主要/次要缺陷,请告诉我!