通过AJAX将变量传递给PHP函数变得不确定,但无法理解为什么索引在构造中丢失 [英] passing variable through AJAX to PHP function getting undefined vairable but not understanding why the index is missing in the construct

查看:43
本文介绍了通过AJAX将变量传递给PHP函数变得不确定,但无法理解为什么索引在构造中丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新-想知道它是否与PHP函数中的这一行有关- $ cid [] ='';

UPDATE - Wondering if it has to do with this line in the PHP function - $cid[] ='';

我正在获得可怕的未定义索引,但是即使在阅读了有关它的答案之后,我仍不确定为什么实例中不存在该索引.我知道索引丢失了,这就是为什么我看到错误,但是我不确定为什么索引丢失了.是导致问题的字符串的初始构造吗?是我检索 $ _ POST ['Cid'] 的方式吗?

I am getting the dreaded undefined index but even after reading SO answers about it, I am uncertain why the index isn't existing in my instance. I get that the index is missing which is why I see the error, I am not sure why the index is missing however. Is it the initial construction of the string that is causing the issue? Is it the way I am retrieving the $_POST['Cid']?

当我进行var-dump时,我得到了预期的结果.如果我在 phpfunc 内部 var_dump($ cid); ,我将得到输出45(或任何数字).如果我在任一AJAX函数中 console.log(cid)我都得到相同的结果

When I var-dump I get the expected result. If I var_dump($cid); inside of phpfunc I get the output 45 (or whatever the number is). If I console.log(cid) in either of the AJAX functions I get the same result

对于传递数据,我使用了此参考资料-如何返回数据来自ajax成功功能?

For passing data I had used this reference - How to return data from ajax success function?

AJAX

function one() {
  jQuery.ajax({
      data: {action: 'phpfunc'},
      type: 'post',
      url: my_ajax.ajax_url,
      dataType: 'JSON',
      success: function(data) {  
          two(data.cid); //<<< passing to function 'two'
          console.log(data.cid)
      }
  })
}

function two(cid) {
    jQuery.ajax({
         type: 'post',
         url: my_ajax.ajax_url,     
         data: { 
             action: 'phpfunc2',
             Cid : cid,
         },
         success: function (data) {
              console.log(data);
         }
    })
}

PHP

$example = $wpdb->get_var("SELECT id FROM mytablename");

add_action( "wp_ajax_phpfunc", "phpfunc" ); 
add_action( "wp_ajax_phpfunc", "phpfunc" );

function phpfunc() {            
    global $wpdb;   
    global $example;
        
    $cid[] ='';
 
    foreach( $example as $value ) {
         $cid = $value-> id_In_Restaurant;
    }; 

    echo json_encode(array('cid' => $cid));

    die;
}

add_action("wp_ajax_phpfunc2", "phpfunc2"); 
add_action("wp_ajax_phpfunc2", "phpfunc2");

function phpfunc2() {
    global $wpdb;
    $cid = $_POST['Cid'] //<<< this line it tells me is an undefined index
......unassociated code here
}

推荐答案

我只想确保这就是您想要的.

I just want to make sure this is what you want.

此:

$cid[] ='';

分配一个数组.

$cid[] ='';
print_r( $cid );
-----------------------------output-----------------------------------
Array
(
    [0] =>
)

此:

foreach( $example as $value ) {
    $cid = $value -> id_In_Restaurant;
}; 

$ cid 分配给示例的id_In_Restaurant的最后一个元素.我不知道那是什么,比方说是4.

assign the $cid to the last element of example's id_In_Restaurant. I have no idea what that would be, let's say it's 4.

print_r($cid);
-----------------------------output-----------------------------------
Array
(
    [cid] => 4
)

如果需要,不要将$ cid分配为数组,只需执行 $ cid = $ example [count($ example)-1]; .

If you want that, don't allocated $cid as an array just do $cid = $example[count($example)-1];.

如果您不希望这样做并希望追加到数组,则需要在for循环中的 $ cid 之后放置括号.

If you don't want that and want to append to the array, you need to put brackets after $cid in the for loop.

foreach( $example as $value ) {
    $cid[] = $value-> id_In_Restaurant;
}; 

下一部分会变得棘手,具体取决于您希望数据如何输出.如果希望将数组中的每个id都索引为'cid',则不能这样做,因为这会导致冲突.为了解决这个问题,您可以制作一个数组数组.

The next part gets tricky depending on how you want your data to come out. If you want every id in the array to be indexed as 'cid', you can't because it would cause a collision. To get around this, you can make an array of arrays.

foreach( $example as $value ) {
    $cid[] = ['cid' => $value-> id_In_Restaurant];
}; 
echo json_encode( $cid );
-----------------------------output-----------------------------------
Array
(
    [0] =>
    [1] => Array
        (
            [cid] => 1
        )

    [2] => Array
        (
            [cid] => 2
        )

    [3] => Array
        (
            [cid] => 3
        )

    [4] => Array
        (
            [cid] => 4
        )

    [5] => Array
        (
            [cid] => 7
        )

    [6] => Array
        (
            [cid] => 95
        )

    [7] => Array
            [cid] => 394
        )

    [8] => Array
        (
            [cid] => 23156
        )

    [9] => Array
        (
            [cid] => 4
        )

)

但是,如果您要将所有ID的数组分配给"cid",你可以做

But if you want to assign the array of all the ids to "cid" you can do

foreach( $example as $value ) {
    $cid[] = $value-> id_In_Restaurant;
}; 
echo json_encode( [ 'cid' => $cid ]);
-----------------------------output-----------------------------------
{"cid":["",1,2,3,4,7,95,394,23156,4]}

这篇关于通过AJAX将变量传递给PHP函数变得不确定,但无法理解为什么索引在构造中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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