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