Jump to content

Grabbing Values From an Array for Posting


cobalt30

Recommended Posts

I have a grid array that I am using with a form that when it hits the currently named test.php it logs in and grabs 3 coordinates (i.e. B1, D3, etc). which come back as XML fields "challenge1", "challenge2", and "challenge3" and then has to match up the value in the grid array and pass the correct value down to my post fields as "response1", "response2", "response3".  However, nothing I'm trying will get the values out to then post.  I'm sure my coding is wrong, but I cannot figure out the right syntax to make it work.

 

Here is part of my grid array:

 

$grid_array = array("A1" => "V", "A2" => "2", "A3" => "1", "A4" => "N", "A5" => "7", "B1" => "H", "B2" => "9", "B3" => "3", "B4" => "T", "B5" => "J", "C1" => "9", "C2" => "2", "C3" => "2", "C4" => "6", "C5" => "Y", "D1" => "N", "D2" => "1", "D3" => "R", "D4" => "8", "D5" => "K");

 

I tried coding this, which is completely wrong, but maybe I'm on to something at least?:

 

foreach ($grid_array as $key => $value) {

$grid_array['$xdoc1->challenge1'] = '$response1';

$grid_array['$xdoc1->challenge2'] = '$response2';

$grid_array['$xdoc1->challenge3'] = '$response3';

}

 

($xdoc1 is the XML return as a SimpleXMLElement, by the way)

 

But this returns an error of

Parse error: syntax error, unexpected T_DOUBLE_ARROW, expecting T_PAAMAYIM_NEKUDOTAYIM

 

Am I at least on the right track?  If not, any ideas on how I can code this to pull out the values that are returned from the first hit and have it pass the appropriate values to the second hit?

Using variables in single quotes take them literally.

 

Try using:

 

      $grid_array[$xdoc1->challenge1] = $response1;
      $grid_array[$xdoc1->challenge2] = $response2;
      $grid_array[$xdoc1->challenge3] = $response3;

 

Should give you correct results.

The grid array is what is above this line.  It is formatted just like this:

 

$grid_array = array("A1" => "V", "A2" => "2", "A3" => "1", "A4" => "N", "A5" => "7", "B1" => "H", "B2" => "9", "B3" => "3", "B4" => "T", "B5" => "J", "C1" => "9", "C2" => "2", "C3" => "2", "C4" => "6", "C5" => "Y", "D1" => "N", "D2" => "1", "D3" => "R", "D4" => "8", "D5" => "K");

 

foreach ($grid_array as $key => $value) {

      $grid_array[$xdoc1->challenge1] = $response1;

      $grid_array[$xdoc1->challenge2] = $response2;

      $grid_array[$xdoc1->challenge3] = $response3;

      }

$grid_array = array(
    "A1" => "V",
    "A2" => "2",
    "A3" => "1",
    "A4" => "N",
    "A5" => "7",
    "B1" => "H",
    "B2" => "9",
    "B3" => "3",
    "B4" => "T",
    "B5" => "J",
    "C1" => "9",
    "C2" => "2",
    "C3" => "2",
    "C4" => "6",
    "C5" => "Y",
    "D1" => "N",
    "D2" => "1",
    "D3" => "R",
    "D4" => "8",
    "D5" => "K"
   );

foreach ($grid_array as $key => $value) {
      $id1 = $xdoc1->challenge1;
      $id2 = $xdoc1->challenge2;
      $id3 = $xdoc1->challenge3;

      $grid_array[$id1] = $response1;
      $grid_array[$id2] = $response2;
      $grid_array[$id3] = $response3;
}

Now I get this:

 

Warning: Illegal offset type in /home/content/html/test.php on line 77

Warning: Illegal offset type in /home/content/html/test.php on line 78

Warning: Illegal offset type in /home/content/html/test.php on line 79

 

These 3 errors correspond to these lines:

 

      $id1 = $xdoc1->challenge1;

      $id2 = $xdoc1->challenge2;

      $id3 = $xdoc1->challenge3;

 

I really appreciate your help with this.  Any ideas?  I am using PHP5, if that makes a difference.

Ok, I've switched it back to just this:

 

foreach ($grid_array as $key => $value) {

      $grid_array['$xdoc1->challenge1'] = '$response1';

      $grid_array['$xdoc1->challenge2'] = '$response2';

      $grid_array['$xdoc1->challenge3'] = '$response3';

      }

 

No errors on this side, but the values still do not seem to be posting.  Here is the code where I'm trying to post, and maybe my syntax here is wrong as well?:

 

curl_setopt($ch_pay, CURLOPT_POSTFIELDS, array

        ('response1'=>$_POST[$response1],

'response2'=>$_POST[$response2],

'response3'=>$_POST[$response3]));

        curl_setopt($ch_pay, CURLOPT_RETURNTRANSFER, 1);

I figured it out.  I actually didn't need to try to pull the values before the POST.  Here is the syntax I used in my POST array that worked:

 

'response1'=>$grid_array["".$xdoc1->challenge1.""],

'response2'=>$grid_array["".$xdoc1->challenge2.""],

'response3'=>$grid_array["".$xdoc1->challenge3.""]

 

Thanks for all your help, it definitely helped lead me in the right direction and saved me a lot more debugging time.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.