ajax sql和PHP查询数据库并返回结果

时间:2015-01-25 04:53:14

标签: javascript php jquery sql ajax

我目前正在尝试使用Ajax查询数据库。我的Ajax如下

function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Not working");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.returnhere.value = ajaxRequest.responseText;
        }
    }
    var datepicker = document.getElementById('datepicker').value;
    var datepicker1 = document.getElementById('datepicker1').value;
    var queryString = "?datepicker=" + datepicker + "&datepicker1=" + datepicker1;
    ajaxRequest.open("GET", "detengde.php" + queryString, true);
    ajaxRequest.send(null); 
}

//-->
</script>



<form name='myForm'>
From: <input  id='datepicker' /> <br />
To: <input  id='datepicker1' />
<br />
    <input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
<div id=returnhere></div>

我的PHP看起来像这样:

    include 'config.php'
$startd = ($_GET['datepicker']);
$endd = ($_GET['datepicker1']);


$sql = "SELECT * FROM delays WHERE Delaytype >= date('".$startd."') AND Delaydate < ADDATE(date('".$endd."'), INTERVAL 1 DAY)";


$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) {

do something . ..  ..  . .

此查询适用于PHP,并将返回位于两个日期范围之间的记录。

我很难将php输出恢复到我的页面。说实话,当我点击按钮时,很少发生。我很熟悉PHP数据库交互AJAX是我刚开始学习的东西。

请注意没有关于安全性的消息我知道这是非常不安全的。

我在这里缺少一些非常基本的东西。经过许多教程,搜索堆叠溢出它只是没有点击(没有双关语)

2 个答案:

答案 0 :(得分:0)

在HTML页面中添加名称为“returnhere”的字段。

<input type="text" name="returnhere">

如果我正确读取你的代码,ajax函数的回调应该填写它的值。

答案 1 :(得分:0)

<?php
include 'config.php';
header('Content-Type: application/json; charset="utf-8"');
$startd = ($_GET['datepicker']);
$endd = ($_GET['datepicker1']);

$sql = "SELECT * FROM delays WHERE Delaytype >= date('".$startd."') AND Delaydate < ADDATE(date('".$endd."'), INTERVAL 1 DAY)";


$result = mysqli_query($con,$sql);

//just an empty array here

$final_array=array();

while($row = mysqli_fetch_array($result)) {

// I am not aware of what is being returned. So I am going to assume its a 'value'
// you can just store the values returned, in an array and echo the array or
// json_encode($array) and echo that

// this will just echo the value for time the loop encounters this statement
echo $row['value'];

//push the element into the array.Beware of overhead caused by array_push()
//if it is a `key-value` pair, its better just to use $final_array[$key] = $value;

array_push($final_array,$row['value']);

}
// Don't forget to set the header before echoing the json
echo json_encode($final_array);
?>

我希望这在一定程度上有所帮助。或者如果您需要详细信息,请随时在下面发表评论

在你的JS中 使用

document.getElementById("returnhere").value=object.responseText;

document.getElementById("returnhere").innerHTML=object.responseText;

适合您的需要