您好我正在尝试使用PHP脚本将三个下拉列表中的'post'传递给数据库,其中我使用三个列表的值将值插入数据库
之前我使用jquery ajax调用将另一个页面的列表检索到div time.php
代码如下: -
//This script uses jquery and ajax it is used to set the values in
// the time field whenever a day is selected.
$(document).ready(function(){
$("#day").change(function(){
var day=$("#day").val();
var doctor=$("#doctor").val();
$.ajax({
type:"post",
url:"time.php",
data:"day="+day+"&doctor="+doctor,
success:function(data){
$("#time").html(data);
}
});
});
});
html部分的代码如下所示: -
<select id="doctor">some options</select>
<select id="day">some options</select>
<div id="time"> </div>
现在两个列表中的值都很好。但我检索到div的那个人没有通过。你能指出我正确的方向吗? 提前谢谢。
//The php script for insertion
<?php
session_start(); //Use this to include session variables
$con=mysqli_connect("localhost","clinic","myclinic","myclinic");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "INSERT INTO appointment(username, doctor, day, time) VALUES('$_SESSION[username]', '$_POST[doctor]', '$_POST[day]', '$_POST[time]')";
if (!mysqli_query($con,$query))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
header("location:login_success.php");
?>
答案 0 :(得分:0)
如果您想要像输入那样检索div内容,请选择等
你必须得到它
var time = $("#time").html();
您的代码将是
在页面上添加一个按钮
<input type='button' value='Send' id='send'>
然后添加
$(document).ready(function(){
$("#send").click(function(){
var day=$("#day").val();
var doctor=$("#doctor").val();
var time = $("#time").find("select").val();
$.ajax({
type:"post",
url:"time.php",
data:"day="+day+"&doctor="+doctor+"&time="+time, //add param
success:function(data){
}
});
});
});