使用数据库

时间:2016-06-17 10:35:51

标签: php ajax

例如我有摄入量和课程数据库。在摄入量中我有A和B数据。 在课程中,我有一个带有进口A作为外键的PHP,以及带有进口B作为外键的Jquery。

所以在形式上,如果我选择" A"作为一个摄取,后来当然选项或下拉将有" PHP"值。

*编辑

使用AJAX从另一个下拉值设置下拉选项。

这是Ajax函数

    <script type="text/JavaScript">
        function Intake()
        {
            $.ajax({
            type: "POST",
            url: "IntakeDropdown.php",
            //data: { id_option: $(object).val(), operation: 'get_subjects' },
            contentType ="application/json; charset=utf-8",
            dataType: "json",
            success: function(data)
            {
                //Clear options corresponding to earlier option of first dropdown
                  //$('#Iname').empty();
                  $('#Iname').html("<option> value='0'>--Select Intake--</option>");
                       //Populate options of the second dropdown
               $.each(data, function(i,item)
               {
                   $('#Iname').html('<option value="'+ data[i].id+'">'+ data[i].name+'</option>');
               });
               //$('#Iname').focus();
            },
                complete: function(){

                }
            });
         }

          function Courses(sid)
        {
            $.ajax({
            type: "POST",
            url: "CoursesDropdown.php?sid="+sid,
            //data: { id_option: $(object).val(), operation: 'get_subjects' },
            contentType ="application/json; charset=utf-8",
            dataType: "json",
            success: function(data)
            {
                //Clear options corresponding to earlier option of first dropdown
                  //$('#Courses').empty();
                  $('#Courses').html("<option> value='0'>--Select Courses--</option>");
                       //Populate options of the second dropdown
               $.each(data, function(i,item)
               {
                   $('#Courses').html('<option value="'+ data[i].id+'">'+ data[i].name+'</option>');
               });
               //$('#Courses').focus();
            },
                complete: function(){

                }
            });
         }

         $(document).ready(function(){
          Intake();
          $("#Iname").change(function(){
            var intakeid = $("#Iname").val();
            Courses(intakeid);
          });
         });
    </script>

      </head>

IntakeDropdown.php

<?php
            include "connect.php";

            $checkuser = "Select * from Intake";
            $result2 = mysqli_query($con, $checkuser);
            if (mysqli_num_rows($results2)) {
                $data = array();
                while ($row = mysqli_fetch_array($results2)) {
                    $data[] = array(
                        'id' => $row['IntakeID'],
                        'name' => $row['IntakeName'],
                    );
                }
                header('Content-type: application/json');
                echo json_encode($data);
            }
?>

CoursesDropdown

<?php
            include "connect.php";

            $checkuser = "Select * from courses Where IntakeID ='" . $_GET("sid") . "'";
            $result2 = mysqli_query($con, $checkuser);
            if (mysqli_num_rows($results2)) {
                $data = array();
                while ($row = mysqli_fetch_array($results2)) {
                    $data[] = array(
                        'id' => $row['CoursesID'],
                        'sid' => $row['IntakeID'],
                        'name' => $row['CoursesName'],
                    );
                }
                header('Content-type: application/json');
                echo json_encode($data);
            }
            ?>

1 个答案:

答案 0 :(得分:0)

您需要使用mysqli_fetch_array()函数将结果行提取为关联数组,数字数组或两者。

参考:http://www.w3schools.com/php/func_mysqli_fetch_array.asp

    <?php  
    $sql = "SELECT * FROM courses where CoursesIntake ='$ScheduleIntake'";
    $result = mysqli_query($con, $sql);

    while($coursesintake=mysqli_fetch_array($result)){ ?>
         <option value="<?php echo $coursesintake["CoursesName"]; ?>"> <?php echo $coursesintake["CoursesName"]; ?></option>
   <?php  } ?>