drutjkpsr67393592 2015-04-01 19:20
浏览 56

将下拉列表值保存到php会话

I have created a drop down list that is populated by data from mysql db, im trying to save the selected value in a session variable but im not sure how to write this (where the *** are)? And does it need to be posted to server side eve though its all in php?

    <?php
$servername1 = "localhost";
$username1 = "root";
$password1 = "";
$dbname1 = "gpdb1";
$conn1 = new mysqli($servername1, $username1, $password1, $dbname1);
if ($conn1->connect_error) {
die("connection failed: " . $conn1->connect_error);
}
$sql2 = "SELECT DoctorID, Title, Surname FROM doctors";
$result2 = $conn1->query($sql2);
echo "<select name='doctor' value=''><option>Select a Doctor</option>";
if ($result2->num_rows > 0) {
foreach($result2 as $row2) 
{echo "<option value=".$row2['DoctorID'].">".$row2['Title']."     ".$row2['Surname']."</option>"; 
}
}echo "</select>";
$_SESSION['selected']=***;
if (isset($_SESSION['selected'])){
echo $SESSION['selected'];}
?>

Thanks :)

  • 写回答

2条回答 默认 最新

  • dongmeirang4679 2015-04-01 19:28
    关注

    First, I advise you to indent your code, for better readability !

    The HTML attribute "value" in your tag isn't useful, you can remove it.

    And then, it is not useful to control your session field with isset() since you're setting it just above.

    I think that you just need to control if the given value in the post field is correct.

    The line you are looking for is :

    $_SESSION['selected'] = $_POST['doctor'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备