3 Replies Latest reply on Mar 17, 2008 1:05 AM by Greg Dove

    Submiting a form with variables packed into an associative array

      I have a complex form created in flash that submits to a shopping cart. I was having trouble getting the variables in the right format for the shopping cart to accept.

      So I created a bridging script in php that fills in the hidden fields of a form then uses javascript to submit it to the cart. This works but I would like to submit the variables directly to the cart and eliminate the need for a bridge.
      At the moment I am simply using

      getURL("bridge.php", "_self", "POST");

      to POST the variables to the bridge.

      The bridge code is

      $id[txt_1] = "$colortext";
      $id[txt_2] = "$xheight";
      $id[txt_3] = "$xwidth";
      $id[txt_4] = "$nopanels";
      $id[txt_5] = "$shutterstyle";
      $id[txt_6] = "$squarearea";
      $id[txt_7] = "$finish";
      $id[txt_8] = "$bifoldoption";
      $id[txt_9] = "$slidingoption";
      $id[txt_10] = "$hingedoption";
      $id[txt_11] = "$totalprice";
      $id[txt_12] = "$colorcode";
      $products_id = "28";
      <form action="product_info.php?products_id=28&action=add_product" method="post" name="MyForm" target="_self">
      <input type="submit" name="Submit" value="Submit">
      <input name="id[txt_1]" type="hidden" value="<?php print($id[txt_1]); ?>">
      <input name="id[txt_2]" type="hidden" value="<?php print($id[txt_2]); ?>">
      <input name="id[txt_3]" type="hidden" value="<?php print($id[txt_3]); ?>">
      <input name="id[txt_4]" type="hidden" value="<?php print($id[txt_4]); ?>">
      <input name="id[txt_5]" type="hidden" value="<?php print($id[txt_5]); ?>">
      <input name="id[txt_7]" type="hidden" value="<?php print($id[txt_7]); ?>">
      <input name="id[txt_6]" type="hidden" value="<?php print($id[txt_6]); ?>">
      <input name="id[txt_8]" type="hidden" value="<?php print($id[txt_8]); ?>">
      <input name="id[txt_9]" type="hidden" value="<?php print($id[txt_9]); ?>">
      <input name="id[txt_10]" type="hidden" value="<?php print($id[txt_10]); ?>">
      <input name="id[txt_11]" type="hidden" value="<?php print($id[txt_11]); ?>">
      <input name="id[txt_12]" type="hidden" value="<?php print($id[txt_12]); ?>">
      <input name="products_id" type="hidden" value="28">
      <script type="text/javascript" language="JavaScript"><!--

      I am trying to create in flash 8 the equivalent of the above form using the function below.

      function submitvars() {
      var id: Array = new Array();
      id.txt_1 = colortext;
      id.txt_2 = xheight;
      id.txt_3 = xwidth;
      id.txt_4 = nopanels;
      id.txt_5 = shutterstyle;
      id.txt_6 = squarearea;
      id.txt_7 = finish;
      id.txt_8 = bifoldoption;
      id.txt_9 = slidingoption;
      id.txt_10 = hingedoption;
      id.txt_11 = totalprice;
      id.txt_12 = colorcode;
      var myvar:LoadVars = new LoadVars();
      myvar.id = id;
      myvar.products_id = '28';
      myvar.send("product_info.php?products_id=28&action=add_product", "_self", "POST");

      So far the variable products_id = ‘28’ transfers over to the cart fine but not the variables in the ‘id’ array
        • 1. Re: Submiting a form with variables packed into an associative array
          Greg Dove Level 4
          I'm afraid I don't really understand what you're trying to do with the bridge.php script. I can't see it doing anything with variables received from flash via getURL. There is no access to the $_POST array in your script.

          LoadVars cannot send complex data types without encoding it in some way so you should assign all those values as properties of the LoadVars instance itself and not try to reference an array from a property of LoadVars (id).

          So it would be more like:
          myvar.txt_1 = colortext;
          myvar.txt_2 = xheight;

          when those are received by the php script they would be accessible in the $_POST array like:

          And, as a side note, in actionscript Arrays are not intended to be used in that way assign properties as an associative array. Its best to use a generic Object instance to do that.
          • 2. Re: Submiting a form with variables packed into an associative array
            celshader Level 1
            Thanks for the input
            I think I am on the right track now.
            I did not include any $_POST declarations because I have register globals turned on.
            I will include then however since its good coding practice and register globals is now deprecated.
            Thanks again for your help.
            • 3. Submiting a form with variables packed into an associative array
              Greg Dove Level 4
              You're welcome.
              I've never used register globals, couldn't figure that out, thanks for explaining.
              The other way to do it if you want to send an array of simple data (usually all of one data type like string or number) is to use array.join("delimiter_sequence"); in flash. You can then assign it to a single property of your loadvars (e.g. 'id') and use explode in php with the same delimiter to recreate an array. This does not give you an associative array though, just a regular numeric indexed one.
              To send complex data like arrays and objects, you can use other encoding techniques. You can even use json: json.org has a json class(es) for flash and php 5 has json encoding/decoding support for example.
              Another way is to use amfphp and remoting, which does all the work for you. And of course, you have XML support as well at both ends, so you can always represent complex data in XML if you prefer. You may want to investigate these or other methods on future occasions.