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
}