当前位置: 动力学知识库 > 问答 > 编程问答 >

Passing array from PHP to jQuery in wordpress

问题描述:

I have been struggling with this for a while. There are tons of topics on the subject but none is actually working for me, but being a newbie might be the cause so please elaborate.

I simplified the code and it won't work, I posted the code below

I am queering the Database, and saving the values to an array in a function

I then need to pass that array to a jquery in the same function. I used json_encode but i am getting this error:

Uncaught SyntaxError: Unexpected token <

Here is the code:

function Test()

{

$query1 = $wpdb->get_results("SELECT(element_value) FROM wp_piic_formmaker_submits WHERE form_id = 13 AND element_label = 1 AND user_id_wd = 1 ORDER BY date asc", OBJECT);

if($query1)

{

foreach($query1 as $q)

{

$PHPArray[$i] = $q->element_value ;

$i = $i + 1;

}

}

$UserInput = "<script type='text/javascript'>

jQuery('#wdform_1_element13').on('change',function()

{

var test = <?php echo json_encode($PHPArray); ?>;

jQuery.each(test , function (i, elem)

{

alert('here');

});

});

</script>";

return $UserInput ;

}

网友答案:

Change it to this:

$UserInput = "<script type='text/javascript'>
jQuery('#wdform_1_element13').on('change',function()
{   
    var test = ".json_encode($PHPArray)."; //Notice the change here
    jQuery.each(test , function (i, elem)
    {
        alert('here');
    });
}); 
</script>";

return $UserInput ;

}

网友答案:

You have to escape the string correctly:

function Test()
{
    $query1 = $wpdb->get_results("SELECT(element_value) FROM wp_piic_formmaker_submits WHERE form_id = 13 AND  element_label = 1 AND user_id_wd = 1 ORDER BY date asc", OBJECT);

    if($query1)
    {
        foreach($query1 as $q)
        {
            $PHPArray[$i] = $q->element_value ;
            $i = $i + 1;
        }
    }

    $UserInput = "<script type='text/javascript'>
    jQuery('#wdform_1_element13').on('change',function()
    {   
        var test = " . json_encode($PHPArray) . ";
        jQuery.each(test , function (i, elem)
        {
            alert('here');
        });
    }); 
    </script>";

    return $UserInput ;
}
网友答案:

If you put PHP inside JavaScript the PHP part is only text, do this:

function Test()
{
    $query1 = $wpdb->get_results("SELECT(element_value) FROM wp_piic_formmaker_submits WHERE form_id = 13 AND  element_label = 1 AND user_id_wd = 1 ORDER BY date asc", OBJECT);

    if($query1)
    {
        foreach($query1 as $q)
        {
            $PHPArray[$i] = $q->element_value ;
            $i = $i + 1;
        }
    }

    $UserInput = "<script type='text/javascript'>
    jQuery('#wdform_1_element13').on('change',function()
    {   
        var test = " . json_encode($PHPArray) . ";
        jQuery.each(test , function (i, elem)
        {
            alert('here');
        });
    }); 
    </script>";

    return $UserInput ;
}
网友答案:

At least from what is shown, neither $i nor $PHPArray are initialized. PHP can be messy about these things, but you cannot directly refer to indexes of non-existent arrays, or variables that have not been declared.

I'll guess the token error was from your browser trying to deal with the text output of a PHP error being flushed, along the lines of 'undefiend' or 'undefined index.'

Also, the real (or at least convenient) purpose of the foreach construct is not fooling with indexes. Try the following substitute for your current loop:

$PHPArray = array(); // $PHPArray = [];
if($query1){    
    foreach($query1 as $q){
       $PHPArray[] = $q->element_value;
    }
}

Beyond that, "wpdb" suggests calls related to specific frameworks, but in case this is a mysqli_stmt call, i believe the call is singular: get_result.

分享给朋友:
您可能感兴趣的文章:
随机阅读: