Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ajax和php的空响应_Javascript_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Javascript 使用ajax和php的空响应

Javascript 使用ajax和php的空响应,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我对这里的JS和PHP很陌生。我在努力自学 我一直在遵循这个原则,但是我无法让它在我的项目中发挥作用。也许有人可以看看我的代码,给我一些提示 我有两页 php(包含我的日期选择器) eventsIn.php(包含我的查询) 在chart.php中有一个日期选择器,带有startDate和endDate。这些值应该填充myeventsIn.php页面中的查询,该页面将返回一些结果 但是,当我在chart.php中使用console.log(response)和在eventsIn.php中使用v

我对这里的JS和PHP很陌生。我在努力自学

我一直在遵循这个原则,但是我无法让它在我的项目中发挥作用。也许有人可以看看我的代码,给我一些提示

我有两页

  • php(包含我的日期选择器)
  • eventsIn.php(包含我的查询)
在chart.php中有一个日期选择器,带有
startDate
endDate
。这些值应该填充my
eventsIn.php
页面中的查询,该页面将返回一些结果

但是,当我在chart.php中使用
console.log(response)
和在eventsIn.php中使用
var\u dump($\u POST)
时,我可以在控制台中看到以下代码:

Hooray, it worked!
ChartJson.php:119 
array(2) {
  ["startDate"]=>
  string(10) "2015-01-01"
  ["endDate"]=>
  string(10) "2015-03-01"
}
[] 
看起来数据是从第一个页面(chart.php)发送的,但是从第二个页面(eventsIn.php)没有接收到任何数据-这是正确的吗

我假设错误在我的代码中(可能是数组?),我已经在下面介绍了它。我非常感谢你的帮助

chart.php

<form method="post" id="search">
    <input type="text" placeholder="start" id="startDate" name="startDate">
    <input type="text" placeholder="end" id="endDate" name="endDate">
    <input type="submit" value="submit">
</form>

<script> 
$(document).ready(function () {

    var request;

    $("#search").submit(function (event) {

        // Abort any pending request  
        if (request) {
            request.abort();
        }
        // setup some local variables
        var $form = $(this);

        // Let's select and cache all the fields
        var $inputs = $form.find("input, select, button, textarea");

        // Serialize the data in the form
        var serializedData = $form.serialize();

        // Let's disable the inputs for the duration of the Ajax request.
        // Note: we disable elements AFTER the form data has been serialized.
        // Disabled form elements will not be serialized.
        $inputs.prop("disabled", true);

        request = $.ajax({
            url: "eventsIn.php",
            type: "post",
            data: serializedData
        });

        // Callback handler that will be called on success
        request.done(function (response, textStatus, jqXHR) {
            // Log a message to the console
            console.log("Hooray, it worked!");
            console.log(response);

            // Callback handler that will be called on failure
            request.fail(function (jqXHR, textStatus, errorThrown) {
                // Log the error to the console
                console.error(
                    "The following error occurred: " + textStatus, errorThrown);

                // Callback handler that will be called regardless
                // if the request failed or succeeded
                request.always(function () {
                    // Reenable the inputs
                    $inputs.prop("disabled", false);
                });

                // Prevent default posting of form
                event.preventDefault();
            });
        });
    });
}); //end dom ready
</script>
<?php 

var_dump($_POST);// for testing

if (isset($_POST['startDate'])) {
    $startDate = $_POST['startDate'];
    $endDate = $_POST['endDate'];
}

$stmt = $conn -> prepare("
    SELECT 
        MONTHNAME(TimeStamp), COUNT(*)
    FROM
        transactions
    WHERE
        TimeStamp BETWEEN (?) AND (?) 
    GROUP BY EXTRACT(MONTH FROM TIMESTAMP)");

$stmt->bind_param('ii', $startDate, $endDate);

$stmt -> execute();

$stmt -> store_result();

$stmt -> bind_result($month, $number);

$data = array();

while ($stmt -> fetch()) {
       $data[] = array(
        'month'  => $month,
        'number'  => $number
    );
}

echo json_encode($data);

$stmt->close();

?> 
这是否表明它与准备好的语句有关?

您应该使用

var input = $("#search").serialize();
而不是

 var $inputs = $form.find("input, select, button, textarea");
 var serializedData = $form.serialize();

嘿@johnny_'s你的代码看起来不错,工作也很好,我得到了以下回复

Hooray, it worked!
chart.php (line 42)

<pre>Array
(
    [startDate] => 2015-01-01
    [endDate] => 2015-03-01
)
</pre>
万岁,成功了! chart.php(第42行) 排列 ( [startDate]=>2015-01-01 [结束日期]=>2015-03-01 ) 您的代码似乎没有任何问题,请尝试通过错误报告检查php代码,并检查控制台日志错误,这样我可以帮助您。检查url和其他变量。

编写表单

var generateReport = function(dom)
{
    var formData = $("#myform").serialize();
    $.ajax({
        url:'your_url',
        type:'post',
        data:formData,
        dataType:'json',
        success:function(data){

        },
        error:function(data){
        }
    });
    return false;
};

如果在json编码之前
var\u dump($data)
?你们确定并没有任何sql错误吗?嗨,最好的方法是使用好的firebug:UseNet选项卡,这样就可以准确地看到你们得到的响应。使用脚本选项卡一步一步地跟踪你,看看填充了哪些变量。只是一个问题我不确定这是否也是答案,你不是在传递一个带“-”的时间戳作为整数吗?发布的数据是否需要转换为unix时间或其他格式才能成为整数?您确定没有任何sql错误吗?您的查询是否有效?您必须调试
eventsIn.php
。问题来自此部分。警告来自
$stmt->bind_param(…)。由于直接在查询中写入日期,因此没有任何参数可绑定。但它并不影响原始代码。但在测试中,您使用了以下日期:
'2014-01-01'和'2014-12-01'
,这些日期与您邮寄的日期不同。您应该使用相同的数据进行测试。感谢Drop Shadow提供的提示,但不幸的是,这并不能解决问题。我的方法行得通吗?请提供降低答案等级的原因(无论是谁做的),否则,如果你不知道答案,请不要评分,这是一种让人泄气的方式。谢谢Ashish,我也能看到这一点。但是,我需要将这些值传递到我的查询中,并将它们回送到
echo json_encode($data)零件。我不同意投反对票的理由。
<form name="myform" id="myform" method="get" action="">
 <input type="submit" onclick="return generateReport(this);" value="Submitt" />
var generateReport = function(dom)
{
    var formData = $("#myform").serialize();
    $.ajax({
        url:'your_url',
        type:'post',
        data:formData,
        dataType:'json',
        success:function(data){

        },
        error:function(data){
        }
    });
    return false;
};