如何为变量分配ajax响应

时间:2015-06-11 10:52:25

标签: php jquery ajax

我有下拉选择框,如下所示

<?php
$sql = "SELECT scheduleName FROM schedule";
$result = mysqli_query($link,$sql);
echo "<select name='schedule' id='schedule'>";
echo "<option value=''>-- Select Schedule --</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['scheduleName'] . "'>" . $row['scheduleName'] . "</option>";
}
echo "</select>";
?>

我有一个名为 processClg.php 的文件,如下所示

<?php
include "config.php";
if ($_POST['type']=='POST')
{
    $qry = "SELECT * FROM schedule WHERE scheduleName LIKE 'Row id of drop down selection'";
    $res = mysqli_query($link,$qry);
}
?>

如何通过将下拉选择的行ID指定为where条件来调用$("#schedule").change(function ());上的processClg.php文件。

更新

我从processClg.Php获得响应如下

[{"id":"2","scheduleName":"shanth","subject":"Patho","university":"Dali","facultyName":"Dr","scheduleStartDate":"2015-06-05","scheduleEndDate":"2015-06-09"}]

如何将ajax调用的响应值分配给以下Php变量

  <?php
$scheduleStartDate = '';
$scheduleEndDate = '';
?>

我非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

$("#schedule").change(function() {
    var value = $('#schedule option:selected').text();
    var ajaxCheck = $.ajax({
        url: 'processClg.php',
        type: 'POST', // had mention post bcoz u mention in processClg.php
        dataType: 'json', // processClg.php will return string means change to text
        data: { id: value },
        success: function(data){
            console.log('success');
            itrToRead(data);
        }
    });
});

function itrToRead(data) {
    $(data).each(function(key, value){
        console.log('key is: '+key+' and value is: '+value);
    });
}

<强> processClg.php

<?php
    include "config.php";
    if ($_POST['type']=='POST') {
        $qry = "SELECT * FROM schedule WHERE scheduleName LIKE '".$_POST['id']."'";
        $res = mysqli_query($link,$qry);
        echo $res;
    }
?>

答案 1 :(得分:0)


您可以按如下方式调用ajax:

    var RowId;

    $.ajax({
        type: "POST",
        async: false,
        url: url,
        data: postdata,
        //dataType: "json",
        success: function (data) {
            RowId = data;
        }
    });

事实是,为变量分配响应是通过参数async: false,

然后它的工作。

答案 2 :(得分:0)

我猜您在渲染页面中有下拉列表,并且您想将所选值发送到php页面:

$("#schedule").change(function(){
    var val2pass = $(this).find(':selected').val(); // get the value
    $.ajax({
      url: 'processClg.php',
      type:'post', // <-----you need to use post as you are using $_POST[]
      data: { rowid : val2pass }, //<---pass the value
      success: function(data){
          itrToRead(data);
      }
   });
});

所以现在在php方面你需要这样做:

<?php
include "config.php";
if ($_POST['type']=='POST')
{
    $qry = "SELECT * FROM schedule WHERE scheduleName LIKE '".$_POST['rowid']."'";
    $res = mysqli_query($link,$qry);
}
?>

答案 3 :(得分:0)

你的procellClg.php将是

<?php
include "config.php";
if (isset($_REQUEST['qid']))
{
    $qry = "SELECT * FROM schedule WHERE scheduleName LIKE '".$_REQUEST['qid']."'";
    $result = mysqli_query($link,$qry);
echo "<select name='schedule' id='schedule'>";
echo "<option value=''>-- Select Schedule --</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['scheduleName'] . "'>" . $row['scheduleName'] . "</option>";
}
echo "</select>";
}
?>

然后进行Ajax函数调用

$("#schedule").change(function() {  
val = $(this).val();
    $.ajax({
        type: "POST",
        async: false,
        url: 'processClg.php',
        data: {qid:val},
        success: function(data){
            $(this).html(data);
        }
    });
});