0

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
}
user14955679
  • 181
  • 8
  • in function one, you're not sending Cid, only action – Ivan Gajic Jan 26 '21 at 21:25
  • Hmmm that is how I understood how to pass the variable down to another AJAX function. But what youre saying it that it is only passing the action? I must admit I am not sure I understand what that means? – user14955679 Jan 26 '21 at 21:27
  • You are missing a quote `mytablename);` should be `mytablename");` – ArSeN Jan 26 '21 at 21:32
  • @arsen thanks just a typo in re-tping. That doesn't exist in my actual code. Edited above. – user14955679 Jan 26 '21 at 21:33
  • why did you re-type your code instead of copy&pasting it here? This way we can not be sure it is the actual code used – ArSeN Jan 26 '21 at 21:36
  • I changed the name of that so that I am not publishing my actual DB names on the web to exist for all time. Also, trying to make the question a pinch more general with the labels so that hopefully it helps someone else in the future. Personally, I find that sometimes the examples are so specific, I dont know enough to discern form the example, the lesson. I am intrigued by Ivan's comment above but dont know enough of what it means – user14955679 Jan 26 '21 at 21:38
  • 1
    @IvanGajic Why would OP send the `Cid` in function `one` if that's the function used to retrieve the Cid from PHP? Kind of hard to send something that you're trying to retrieve – icecub Jan 27 '21 at 03:12
  • Anyway, just for the heck of it, try adding `data = JSON.parse(data);` right after `success: function(data) {` inside function `one` of your jQuery code – icecub Jan 27 '21 at 03:19
  • Thanks for the help. Nope nada on that try. I am thinking the issue is that I am trying to get the index which doesn't exist since if I `var_dump` I get `array(3) { [0]=>string(2) "45" [1]=> string(2) "46" [2]=> string(2) "47" }` so then its just a question of how to define `cid` inside phpfunc2? if I cant use `$cid = $_POST['Cid']` then what do I use? – user14955679 Jan 27 '21 at 03:28

1 Answers1

0

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]}
zelarian
  • 121
  • 1
  • 4