简体   繁体   English

无法从ajax获取发布数据到php

[英]Not able to get post data from ajax to php

I really need your help guys. 我真的需要你们的帮助。 I am not able to access post data in php from ajax. 我无法从ajax访问php中的发布数据。 I have no idea what's going on here. 我不知道这是怎么回事。 I tried searching for solutions in stackoverflow but none of them helped me. 我尝试在stackoverflow中搜索解决方案,但没有一个对我有帮助。 Here is my script,.. 这是我的剧本,..

    <script>
                    $('form.ajax').on('submit',function() {
                        var that = $(this),
                        url = that.attr('action'),
                        type = that.attr('method'),
                        data = {};

                        that.find('[name]').each(function(index, value) {
                            var that = $(this),
                            name = that.attr('name'),
                            value = that.val();

                            data[name] = value;
                            console.log(data.name);
                        });
                        $.ajax({
                            url: url,
                            type: type,
                            data: data,
                            success: function(response) {

                            }

                        });
                        return false;
                    });
                </script>

here is my php code, 这是我的PHP代码,

                  <?php
$con=mysqli_connect("localhost","username","password","databasename");
if(mysqli_connect_errno())
{
    echo "failed to connect to MySQL:"+mysqli_connect_error();
    die();
}
$myQuery = "SELECT * FROM Invoice";
$result1 = mysqli_query($con,$myQuery);
if(isset($_POST['name'])){
    $name = $_POST['name'];
    $query = "SELECT * FROM Invoice WHERE InvoiceNumber LIKE '%".$name."'";
    $result1 = mysqli_query($con,$query);
}
$data1 = array();
while($row = mysqli_fetch_array($result1)) {
    $row_data = array(
        'InvoiceNumber' => $row['InvoiceNumber'],
        'InvoicePONumber' => $row['InvoicePONumber']
    );
    array_push($data1,$row_data);
}

echo json_encode($data1);

?> ?>

The default request method for $.ajax is GET, so you will never get any results in $_POST var. $.ajax的默认请求方法是GET,因此您永远不会在$_POST var中获得任何结果。 instead you should use $.post or add the request type to your ajax call. 相反,您应该使用$.post或将请求类型添加到您的ajax调用中。

$.ajax({
    ...
    type: 'POST'
});

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

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