问题
UPDATE - Wondering if it has to do with this line in the PHP function - $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']
?
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
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
}
回答1:
I just want to make sure this is what you want.
This:
$cid[] ='';
allocates an array.
$cid[] ='';
print_r( $cid );
-----------------------------output-----------------------------------
Array
(
[0] =>
)
This:
foreach( $example as $value ) {
$cid = $value -> id_In_Restaurant;
};
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
)
If you want that, don't allocated $cid as an array just do $cid = $example[count($example)-1];
.
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;
};
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
)
)
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]}
来源:https://stackoverflow.com/questions/65909464/passing-variable-through-ajax-to-php-function-getting-undefined-vairable-but-not