【问题标题】:ajax sql and PHP query database and return resultajax sql 和 PHP 查询数据库并返回结果
【发布时间】:2015-01-25 04:53:13
【问题描述】:

我目前正在尝试使用 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 是我刚开始学习的东西。

请不要发送有关安全性的消息,我知道这是非常不安全的。

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

【问题讨论】:

  • Im struggling to get the php output back to my page. 。您的意思是您只想将结果echo 返回到您的页面,或者您想以json 之类的格式组织数据?
  • 老实说,这两种解决方案都会很有用,因为有些我会在 JSON 中回显我想要的其他数据。我想我在 AJAX 和 json_encode 中指定了 PHP 的数据类型,但不熟悉该方法。我更新了代码,这里有一个名为 return 的 div 格式不正确

标签: javascript php jquery sql ajax


【解决方案1】:

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

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

如果我没看错,ajax 函数的回调应该填写它的值。

【讨论】:

  • 您好,请查看修正问题是否存在,但未格式化为代码。它是一个名为 returnhere 的 div。当我访问错误的元素时
【解决方案2】:
<?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;

哪个适合你的需要

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多