简体   繁体   English

如何在jquery ajax调用期间调试php

[英]how to debug php during jquery ajax call

I have an html form that I submit to a php server. 我有提交给php服务器的html表单。 If I submit the form the standard way with a submit button and an action="screen_custom.php", then it works fine. 如果我以带有提交按钮和action =“ screen_custom.php”的标准方式提交表单,则可以正常工作。 I see a page full of valid JSON data. 我看到一个充满有效JSON数据的页面。

But if I submit the form using a jquery ajax call, then an empty json array is returned. 但是,如果我使用jquery ajax调用提交表单,则会返回一个空的json数组。 I believe the php is unable to find the form values for some unknown reason. 我相信php由于某些未知原因无法找到表单值。 Anyway, how do I debug the php during an ajax call? 无论如何,我如何在ajax调用期间调试php? The response must only include the data needed to populate a jquery DataTable. 响应必须仅包含填充jquery DataTable所需的数据。 If I echo debugging output, then the datatable will fail to populate. 如果我回显调试输出,则数据表将无法填充。

Here's some code that successfully submits the form to the php and returns good json data. 这是一些将表单成功提交到php并返回良好json数据的代码。

    <form id="criteriaForm" method="post" action="screen_custom.php"> 
    <table>
    <tr>
        <th>Filter By</th><th>Criteria</th><th title="Show this column">Show</th>
    </tr>
    <tr>
        <td><label title="Percentage increase">Appreciation Potential (%)</label></td>
        <td>
            <select name="upside">
                <option value="any">Any</option>
                <option value="gt0">> 0%</option>
                <option value="gt10">> 10%</option>
            </select>
        </td>
        <td><input type="checkbox" id="showUpside" name="showUpside" value="true">
    </tr>

    </table>
    <input type="submit" value="Run">
    </form>

Here's some code that fails to return the same json data: 这是一些无法返回相同json数据的代码:

<form id="criteriaForm" method="post" action=""> 
<table>
<tr>
    <th>Filter By</th><th>Criteria</th><th title="Show this column">Show</th>
</tr>
<tr>
    <td><label title="Percentage increase">Appreciation Potential (%)</label></td>
    <td>
        <select name="upside">
            <option value="any">Any</option>
            <option value="gt0">> 0%</option>
            <option value="gt10">> 10%</option>
        </select>
    </td>
    <td><input type="checkbox" id="showUpside" name="showUpside" value="true">
</tr>

</table>
<button type="button" onclick="runCustomScreen()">Run</button>
</form>

And the javascript: 和javascript:

function runCustomScreen() {
    $('#customTable').DataTable({
        "ajax": {
            type: 'POST',
            url: "screen_custom.php",
            data: $('#criteriaForm').serialize(),
            complete: function(jqXHR, textStatus) {
                console.log("Run was successful, textStatus="+textStatus);
            },
            error: function(e, textStatus, errorThrown) {
                alert("textStatus="+textStatus +", errorThrown="+errorThrown);
            }
        }    
    }); 
    return false;   //needed if called by onclick event
}

Here's the parts of the php that handle the form data and return the json: 这是php中处理表单数据并返回json的部分:

$upside = $_POST["upside"];
$sql = "SELECT * FROM stocks " .$where;
//echo '<script>'
//echo 'sql=' .$sql .'<br>';
//echo 'console.log(' .$sql .')';
//echo '</script>';

$rs = mysql_query($sql);

//store result in an array
$results = array();
while($row = mysql_fetch_object($rs))
{
    $results[] = $row;
}
$data = json_encode($results);
echo '{"data":' .$data .'}';

//close the db connection
mysql_close($connection);

Note that if I uncomment any but the last echo, then the datatable won't populate. 请注意,如果我取消注释除了最后一个回声,则不会填充数据表。 So how can I debug this? 那么我该如何调试呢?

Did you tried to use the isset($_POST['value']) method on your php script? 您是否尝试过在PHP脚本上使用isset($_POST['value'])方法? where value is the name that you will assign to your submit button. 其中value是您将分配给提交按钮的名称。

I figured out how to debug it. 我想出了如何调试它。 In the chrome debugger, select the Network tab, then select "screen_custom.php" from the list, then select the Response tab. 在chrome调试器中,选择“网络”标签,然后从列表中选择“ screen_custom.php”,然后选择“响应”标签。 It shows the output from the php. 它显示了php的输出。 I uncommented the echo sql statement and could see that in fact, the form parameters are not being read, as I suspected. 我取消对echo sql语句的注释,并且可以看到实际上,正如我所怀疑的,表单参数没有被读取。

Then I googled on that problem and found the solution was to modify the data paramenter js as shown below: 然后我搜索了这个问题,发现解决方案是修改数据paramenter js,如下所示:

        data: function(d) {
            var form_data = $('#criteriaForm').serializeArray();
            $.each(form_data, function(key,val) {
                d[val.name] = val.value;
            });
        },

I don't know why the first method didn't work as I could see the correct parameters being sent in the header. 我不知道第一个方法为什么不起作用,因为我可以看到在标题中发送了正确的参数。 But this works. 但这有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM