如何通过AJAX和JSON将数组从PHP转换为JAVASCRIPT? [英] How to get the array from PHP to JAVASCRIPT through AJAX and JSON?

查看:121
本文介绍了如何通过AJAX和JSON将数组从PHP转换为JAVASCRIPT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我吗?似乎我得到的是字符串而不是数组. 一旦我将数组(这是SQL查询的结果)通过PHP从AJAX通过PHP传递到Javascript,我便想循环数组并以表格形式填充某些字段. 我发布了代码:

Can someone help me out? It seems like I get a string instead of an array. Once I pass the array, which is the result of a sql query, from PHP to Javascript through AJAX, I would like to loop the array and populate some fields in a form. I post the code:

JAVASCRIPT

JAVASCRIPT

(function($){
    $(document).ready(function() {
        $('#input_12_153').change(function (){
            if ($('#input_12_153').attr("value")== 'no-selection'){
                $('#input_12_48').val( '' );}
            else{
                var valor = $('#input_12_153').attr("value");
                jQuery.ajax({ // We use jQuery instead $ sign, because Wordpress convention.
                    url : '/optcat/wp-admin/admin-ajax.php', // This addres will redirect the query to the functions.php file, where we coded the function that we need.
                    type : 'POST',
                    data : {
                        action : 'my_action', 
                        fieldvalue : valor,
                    },
                    success: function(data) {
                        alert( data );
                        //Here I would like to loop the array and get the values
                    }
                });
            }
        });
    });
})(jQuery);

PHP(Functions.php)

PHP (Functions.php)

add_action( 'wp_ajax_my_action', 'my_action' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action' ); // This lines it's because we are using AJAX on the FrontEnd.

function my_action(){
    global $wpdb;

    $tablename = $wpdb->prefix . 'rg_lead_detail';
    $lead_id = $_POST['fieldvalue'];  // This variable will get the POST 'fieldvalue'
    $form_id = 21;

    $sql = "SELECT value FROM $tablename WHERE lead_id = %d AND form_id= %d";

    $results = $wpdb->get_results( $wpdb->prepare( $sql, $lead_id, $form_id ), ARRAY_A );

    echo json_encode($results,JSON_FORCE_OBJECT);
} 

推荐答案

在我的情况下,无需再次使用JSON.parse(data);,否则我通常会收到错误消息"JSON中位置1处的JSON中出现意外令牌o" 仅通过放置dataType: "json"就足够了. 我发布了我的最终代码:

In my case there was no need to use JSON.parse(data); again otherwise I used to get an error of "Unexpected token o in JSON at position 1 at " By only putting dataType: "json" is enough. I post my final code:

functions.php

functions.php

add_action( 'wp_ajax_my_action', 'my_action' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action' ); // This lines it's because we are using AJAX on the FrontEnd.

function my_action(){
    global $wpdb;

    $tablename = $wpdb->prefix . 'rg_lead_detail';
    $lead_id = $_POST['fieldvalue'];  // This variable will get the POST 'fieldvalue'
    $form_id = 21;

    $sql = "SELECT value FROM $tablename WHERE lead_id = %d AND form_id= %d";

    $results = $wpdb->get_results( $wpdb->prepare( $sql, $lead_id, $form_id ), ARRAY_A );

    echo json_encode($results);

    die();
} 

JavaScript

Javascript

(function($){
    $(document).ready(function() {
        $('#input_12_153').change(function (){
            if ($('#input_12_153').attr("value")== 'no-selection'){
                $('#input_12_48').val( '' );}
            else{
                var valor = $('#input_12_153').attr("value");
                jQuery.ajax({ // We use jQuery instead $ sign, because Wordpress convention.
                    url : '/optcat/wp-admin/admin-ajax.php', // This addres will redirect the query to the functions.php file, where we coded the function that we need.
                    type : 'POST',
                    data : {
                        action : 'my_action', 
                        fieldvalue : valor,
                    },
                    dataType:'json',
                    success: function(data) {
                        //var results = JSON.parse(data); // like this
                        $(data).each(function(i,val){
                            $.each(val,function(k,v){
                                alert(k+" : "+ v);     
                            });
                        });
                    }
                });
           }
       });
    });
})(jQuery);

这篇关于如何通过AJAX和JSON将数组从PHP转换为JAVASCRIPT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆