下拉框,使用AJAX和PHP将值插入MYSQL db OnChange

时间:2016-04-14 19:34:12

标签: javascript php mysql ajax

我在更改下拉框时通过AJAX执行SQL时遇到问题,如果可能的话,我希望得到一些帮助。

背景信息 我的任务是创建一个日历,显示在健身房运行的所有课程,最多为5小时,每小时6(30)人,持续14小时。我不是专业人员,我可能创建了一个如果我有这个问题,请告诉我。

我设法创建了包含14列30个下拉框的视图(5个类,每小时6个,每个小时14个小时)。每个下拉框都会轮询数据库,如果有一个条目,它将使用bookinguser的名称填充该框。如果没有找到预订,它将创建一个下拉框,用于轮询会员桌并呈现健身房的所有成员,这些成员在更改后,将有希望预订该人。 - 这是我当前的问题!

每个下拉框的名称对应于我打算传递给javascript函数并最终传递给SQL语句的时间,组和人数。每个选项的值对应于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; 
 }
   });
}

samefile.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”。发送@jquery.min.js:4n.extend.ajax @jquery.min.js:4getda @ cal.php?date = 140416:42onchange @ cal.php?date = 140416:36ListPicker._handleMouseUp @ about:blank:535

2 个答案:

答案 0 :(得分:0)

可能想要查看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; 
         }
         });
   });

答案 1 :(得分:0)

我在samefile.php中看到三个问题 - include拼写不正确,一个额外的分号和一个缺少的右括号:

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

      $name = $_POST["get_option"];

      //this should be converted to parameterized queries
      $sql = "insert into bookings (memberid,date,time,groupnumber) values (1,'2016-04-14','09:00',3)";
      $query = mysqli_query($sql); 

      if(is_object($query)){
          echo 'successfully inserted';
      } else {
         echo 'insert failed';
      }

      $mysqli->close();

   } else {
       echo 'no data to process!';
   }

?>