gpt4 book ai didi

javascript - 使用 AJAX 和 PHP 将值插入 MYSQL db OnChange 的下拉框

转载 作者:行者123 更新时间:2023-11-29 03:25:40 25 4
gpt4 key购买 nike

当下拉框发生变化时,我在通过 AJAX 执行 SQL 时遇到问题,如果可能,我希望获得一些帮助。

背景信息我的任务是创建一个每日日历,显示在健身房运行的所有类(class),最多是每小时 5 节课,每节课 6 (30) 人,持续 14 小时。我不是专业人士,我可能已经创建了一个关于这个问题的复杂方法,如果我有,请告诉我。

我已经设法创建了一个 View ,该 View 由 14 列 30 个下拉框组成(每小时 5 节课,每节 6 节课,持续 14 小时)。每个下拉框都会轮询数据库,如果有条目,它将使用预订用户的名称填充该框。如果没有找到任何预订,它将创建一个下拉框,用于轮询成员(member)表并显示健身房的所有成员(member),当更改时,希望可以预订该人。-这是我当前的问题!

每个下拉框的名称对应于我打算传递给 javascript 函数并最终传递给 SQL 语句的时间、组和人数。每个选项的值都与 memberid 相对应,该 memberid 也将传递给构造 SQL 所需的所有信息。

我目前的代码

HTML - 从 php 循环生成的片段

    <div id="results">
<div id="07" class="column">07:00<br/>
<div id="group1">
<select name="07:00-1-0" onchange="getda(this.value,this)">
<option value="none">---------------</option>
<option value="2">John Doe</option>
<option value="1">Joe Bloggs</option>
</select>
<select name="07:00-1-1" onchange="getda(this.value,this)">
<option value="none">---------------</option>
<option value="2">John Doe</option>
<option value="1">Joe Bloggs</option>
</select>

PHP

<?php
$mysqli = new mysqli("localhost", "root", "", "gym");

/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
function hyphenate($str) {
return implode("-", str_split($str, 2));
}
function getmembers($time,$group,$iteration)
{
$date=$_GET["date"];

$date=hyphenate($date);
$date = explode('-', $date);
$new_date = $date[2].'-'.$date[1].'-'.$date[0];

$mysqli = new mysqli("localhost", "root", "", "gym");
if ($iteration == 0){
$result = $mysqli->query("select members.memberid, members.firstname, members.lastname from bookings inner join members on bookings.memberid = members.memberid where bookings.date = '$new_date' and time = '$time' and bookings.groupnumber = '$group' order by bookings.bookingid ASC limit 1");
}
else {$result = $mysqli->query("select members.memberid, members.firstname, members.lastname from bookings inner join members on bookings.memberid = members.memberid where bookings.date = '$new_date' and time = '$time' and bookings.groupnumber = '$group' order by bookings.bookingid ASC limit 1,$iteration");
}
$rowcount=mysqli_num_rows($result);
if ($rowcount==$iteration && $iteration == 0)
{
$result = $mysqli->query("select firstname, lastname,memberid from members order by firstname ASC");


echo '<select name="'.$time.'-'.$group.'-'.$iteration.'" onchange="getda(this.value,this)"><option value="---------------">---------------</option>';

while ($row = $result->fetch_assoc()) {

unset($firstname, $lastname);
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$memberid = $row['memberid'];
echo '<option value="'.$memberid.'">'.$firstname . ' ' . $lastname .'</option>';

}

echo "</select>";
}
else if ($rowcount>=$iteration){
echo '<select name="'.$time.'-'.$group.'-'.$iteration.'" onchange="getda(this.value,this)">';

while ($row = $result->fetch_assoc()) {

unset($firstname, $lastname);
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$memberid = $row['memberid'];
echo '<option value="'.$memberid.'">'.$firstname . ' ' . $lastname .'</option><option value="cancel">Cancel</option>';
}
echo "</select>";
}
else{
$result = $mysqli->query("select firstname, lastname, memberid from members order by firstname ASC");


echo '<select name="'.$time.'-'.$group.'-'.$iteration.'" onchange="getda(this.value,this)"><option value="---------------">---------------</option>';

while ($row = $result->fetch_assoc()) {

unset($firstname, $lastname);
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$memberid = $row['memberid'];
echo '<option value="'.$memberid.'">'.$firstname . ' ' . $lastname .'</option>';

}

echo "</select>";

}
}

?>

JS

function getda(id,booking){
$.ajax({
type: 'post',
url: 'samefile.php',
data: {
get_option:id
},
success: function (response) {
document.getElementById("result").innerHTML=response;
}
});
}

相同文件.php

<?php 
if(isset($_POST['get_option']))
{
inlude 'config/config.php';

$name=$_POST["get_option"];
echo "<SCRIPT>
alert('$name');
</SCRIPT>";

$sql = "insert into bookings (memberid,date,time,groupnumber) values (1,'2016-04-14','09:00',3)";
$query = mysqli_query($sql);



$mysqli->close();


?>

chrome 中的控制台看起来不错(下图),但没有插入任何记录,也没有显示 php 警报。我没有将任何变量传递给 SQL,因为我第一次测试查询是否正确执行

jquery.min.js:4 XHR 完成加载:POST "http://localhost/gym/samefile.php ".send @ jquery.min.js:4n.extend.ajax @ jquery.min.js:4getda @ cal.php?date=140416 :42onchange @ cal.php?date=140416:36ListPicker._handleMouseUp @ about:blank:535

最佳答案

可能需要查看 jQuery 的 .change() .我认为它可以与下面的代码一起使用。你也可以让它调用你的函数,其中也有 ajax

    $( ".class" ).change(function() { //can use #id here too
$.ajax({
type: 'post',
url: 'samefile.php',
data: {
get_option:this.value
},
success: function (response) {
document.getElementById("result").innerHTML=response;
}
});
});

关于javascript - 使用 AJAX 和 PHP 将值插入 MYSQL db OnChange 的下拉框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36632354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com