簡體   English   中英

無法從ajax獲取發布數據到php

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

我真的需要你們的幫助。 我無法從ajax訪問php中的發布數據。 我不知道這是怎么回事。 我嘗試在stackoverflow中搜索解決方案,但沒有一個對我有幫助。 這是我的劇本,..

    <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>

這是我的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);

?>

$.ajax的默認請求方法是GET,因此您永遠不會在$_POST var中獲得任何結果。 相反,您應該使用$.post或將請求類型添加到您的ajax調用中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM