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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

      }

Link to comment
Share on other sites

$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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.