簡體   English   中英

使用ajax作為json將多維數組發送到php並接收html文本以在div中顯示

[英]send multidimensional array to php using ajax as json and receive html text to show in a div

第一部分完成,使用ajax作為json將數據成功發送到php(我通過跟蹤此站點上已經發布的問題的答案來完成此操作)。

現在如何在php中訪問這些值,以及在使用abc [2]中的字符串作為sql查詢並以表格格式(在第二頁中)使用html將結果打印在php(第二頁)中之后,如何在之后接收該響應ajax調用在第一頁完成,以在第一頁的div中顯示它。

實際上,我並不是在問運行查詢和顯示值的過程。 我在使用php訪問這些數組值並使用ajax在第一頁中顯示它們時遇到問題。

每當我從第一頁返回一些值時(使用echo或print函數),我都會收到有關語法錯誤的警報:ajax調用從第二頁返回后出現意外的問題。 第一頁中的代碼是

var abc= {};

abc[0] = {};
abc[0]['name'] = 'first col';
abc[0]['width'] = 123;

abc[1] = {};
abc[1]['name'] = 'second col';
abc[1]['width'] = 456;

abc[2]="abcdefghijklmnopqrstuvwxyz";



$.ajax(
{
    type: "POST",
    url: "query.php",
    data: {abc: abc},
    dataType: "json",
    beforeSend:function()
    {
    // Do something before sending request to server
    },
    error: function(jqXHR, textStatus, errorThrown)
    {

        alert(errorThrown);
    },
    success: function(data)
    {

        alert(data);
    }
});

我不知道...您可以嘗試這個。

$param = cleanAll();

您可以通過以下方式進行操作:

  • 使用ajax將參數發送到您的query.php文件。
  • 在query.php文件中,編寫邏輯以處理已發布的數據,以從DB保存/編輯/從DB獲取數據,並創建HTML以在div中打印並回顯該html
  • 成功時,在ajax調用中將html放到div,這是從query.php返回的。
    這是對ajax代碼的一些更改:

數組會像這樣

 var abc= {abc :[{name:'first col',width:123},{name:'second col',width:456},{name:"abcdefghijklmnopqrstuvwxyz",width:456}] }; 

阿賈克斯會喜歡這個

    $.ajax(
        {
            type: "POST",
            url: "query.php",
            data: abc,
            dataType: "json",
            beforeSend:function()
            {
            // Do something before sending request to server
            },
            error: function(jqXHR, textStatus, errorThrown)
            {    
                alert(errorThrown);
            },
            success: function(my_html)
            {

                $('#my_div').val(my_html);
            }
    });


代碼未經測試,但可以正常工作。

從我最近的實驗中可以了解到,在轉換為JSON之前,會將數組放置到對象上。 在我的代碼下面:

JavaScript的:

...    
var read_numbers = new Array();
...
function read_ajax_done(num, num_in_word){
    rec_count_in_cache = rec_count_in_cache + 1;

    var number = {"num" : "", "word" : ""}; // Object type
    number.num = num;
    number.word = num_in_word;

    read_numbers[rec_count_in_cache-1] = number; // Array is multidimensional
} 

function save_to_db(read_numbers) {
    var object_read_numbers = {"read_numbers" : read_numbers}; // Array placed to object
    JSON_read_numbers = JSON.stringify(object_read_numbers); // Object converted to JSON

    request = $.ajax({
                    type     : "POST",
                    url      : "post.php",
                    data     : {read_numbers : JSON_read_numbers}
                });
    request.done(function(msg) { 
        alert("Respond: "+ msg);
    });
    request.fail(function(jqXHR, textStatus) {
            alert("Function inaccessible: " + textStatus)
    });
} 

PHP:

if (isset($_POST["read_numbers"])) {
   $read_numbers = json_decode($_POST["read_numbers"], TRUE);
   ..... 
   $response = $read_numbers["read_numbers"][n]["word"];
}

echo $response;

第二頁PHP

<?php
    //need for displaying them back to the $.ajax caller
    header('Content-type: application/json');
    //accessing data
    $post = $_POST['abc'];
    /*
     * how to access multid array
     * $post[0]['name'] = 'first col'
     * $post[0]['width'] = 123
     * $post[1][name] = 'second col'
     * $post[2] = 'abcdefghijklmnopqrstuvwxyz'
     */
    //now to pass them back to your $.ajax caller
    echo json_encode($post);
?>

第一頁

$.ajax(
{
    type: "POST",
    url: "query.php",
    data: {abc: abc},
    dataType: "json",
    success: function(data)
    {
    //prints your response variable
    console.log(data);
    }
});

暫無
暫無

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

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