简体   繁体   English

通过ajax将变量发送到php

[英]Send variable to php via ajax

I'm trying to send a input value to php via ajax but I can't seem to get this right. 我正在尝试通过ajax将输入值发送到php,但似乎无法正确执行。 I'm trying to create a datatable based on the user input. 我正在尝试根据用户输入创建数据表。

This is my code: 这是我的代码:

<input class="form-control" id="id1" type="text" name="id1">

My javascript code: 我的JavaScript代码:

<script type="text/javascript">
$(document).ready(function() {

var oTable = $('#jsontable').dataTable();  //Initialize the datatable

    $('#load').on('click',function(){
        var user = $(this).attr('id');
        if(user != '') 
        { 
        $.ajax({
            url: 'response.php?method=fetchdata',
            data: {url: $('#id1').val()},
            dataType: 'json',
            success: function(s){
            console.log(s);
                    oTable.fnClearTable();
                        for(var i = 0; i < s.length; i++) {
                         oTable.fnAddData([
                                    s[i][0],
                                    s[i][1],
                                    s[i][2],
                                    s[i][3],
                                    s[i][4],
                                    s[i][5],
                                    s[i][6],
                                    s[i][7]
                                           ]);                                      
                                        } // End For

            },
            error: function(e){
               console.log(e.responseText); 
            }
            });
        }
    });
});
</script>

My php script: 我的PHP脚本:

<?php
    $conn = pg_connect(...);
$id1 = $_POST["id1"];
    $result = pg_query_params($conn, 'SELECT * FROM t WHERE id1 = $1 LIMIT 20', array($id1));
    while($fetch = pg_fetch_row($result)) {
        $output[] = array ($fetch[0],$fetch[1],$fetch[2],$fetch[3],$fetch[4],$fetch[5],$fetch[6],$fetch[7]);
    }
    echo json_encode($output);
?> 

I don't know a lot of js but my php is correct i test it. 我不太了解js,但是我的php是正确的,我对其进行了测试。 So i guess the problem is in the javascript code. 所以我想问题出在javascript代码中。 The problem is, my datatable is not being created based on the user input. 问题是,未根据用户输入创建我的数据表。 Thank you! 谢谢!

change 更改

data: {url: $('#id1').val()},

to: 至:

type: 'POST',
data: {id1: $('#id1').val()},

However the problem might be bigger. 但是问题可能更大。 You might not be getting the correct data from PHP. 您可能无法从PHP获取正确的数据。 You can debug by adding the error option to your ajax() call, like this: 您可以通过将error选项添加到ajax()调用中进行调试,如下所示:

$.ajax({
    url: 'response.php?method=fetchdata',
    type: 'POST',
    data: {id1: $('#id1').val()},
    dataType: 'json',
    success: function(s){
    },
    error: function (xhr, status, errorThrown) {
        console.log(xhr.status);
        console.log(xhr.responseText);
    }
});

Then check your browser's Console for the output, this should give you some type of error message coming from PHP. 然后检查浏览器的控制台的输出,这应该给您来自PHP的某种类型的错误消息。

My assumption is that since you are using dataType: 'json' , the ajax request expects JSON headers back, but PHP is sending HTML/Text. 我的假设是,由于您使用的是dataType: 'json' ,所以ajax请求要求返回JSON标头,但PHP正在发送HTML / Text。 To fix, add the correct headers before echoing your JSON: 要解决此问题,请在回显JSON之前添加正确的标头:

header('Content-Type: application/json');
echo json_encode($output);

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

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