Page 1 of 1

please help: sending complex array through url

Posted: Wed Aug 11, 2004 9:13 am
by talorlik
hi all,

i have this most frustrating problem:

i have a dynamic smarty template which uses JavaScript to comunicate with my PHP to get things loaded onto the template. this is one of the JavaScript functions i use to sent values through the url to my PHP page:

Code: Select all

<SCRIPT LANGUAGE="JavaScript">

function offer_fields()
  {
   var chk = false;
   var myobj = new Array();
   var offer_cnt = 0;
   var more_cnt = 0;
      
   for(var FormIndex = 0 ; FormIndex < document.forms.length ; FormIndex++)
    for(var ElementIndex = 0 ; ElementIndex <    
      document.formsїFormIndex].elements.length ; ElementIndex++)
      {
       myobjїElementIndex] = new Array();
             
       if(document.formsїFormIndex].elementsїElementIndex].name == "offering_suburb")
        {
         myobjїElementIndex]ї0] =                                                    
                                     document.formsїFormIndex].elementsїElementIndex].name;
	 
	 var offering = new Array();
	 for(var i = 0 ; i < 
                    document.formsїFormIndex].elementsїElementIndex].options.length ; i++)
	  if(document.formsїFormIndex].elementsїElementIndex].optionsїi].selected)
	   {
	    offeringїi - 1] =
                              document.formsїFormIndex].elementsїElementIndex].optionsїi].value;
	    offer_cnt++;
	   }
	     	    
	   myobjїElementIndex]ї1] = offering; 
	}
       else if(document.formsїFormIndex].elementsїElementIndex].name ==
                                                                                                                    "more_suburb")
        {
         myobjїElementIndex]ї0] =
                                           document.formsїFormIndex].elementsїElementIndex].name;
	 
	 var more = new Array();
	 for(var i = 0 ; i < 
                    document.formsїFormIndex].elementsїElementIndex].options.length ; i++)
	  if(document.formsїFormIndex].elementsїElementIndex].optionsїi].selected)
	   {
	    moreїi - 1] = 
                          document.formsїFormIndex].elementsїElementIndex].optionsїi].value;
	    more_cnt++;
	   }
	     	    
	   myobjїElementIndex]ї1] = more; 
	}	
       else if(document.formsїFormIndex].elementsїElementIndex].type == "select-one")
        {
         myobjїElementIndex]ї0] =
                                         document.formsїFormIndex].elementsїElementIndex].name;
	 myobjїElementIndex]ї1] =                                     document.formsїFormIndex].elementsїElementIndex].optionsїdocument.formsїFormIndex].elementsїElementIndex].selectedIndex].value;  
	 
	 if((document.formsїFormIndex].elementsїElementIndex].name == "status_type") &&
((document.formsїFormIndex].elementsїElementIndex].optionsїdocument.formsїFormIndex].elementsїElementIndex].selectedIndex].value == "offer") ||	     (document.formsїFormIndex].elementsїElementIndex].optionsїdocument.formsїFormIndex].elementsїElementIndex].selectedIndex].value == "both")))
	  chk = true;
	}
       else
        {
         myobjїElementIndex]ї0] =  
                                             document.formsїFormIndex].elementsїElementIndex].name; 
	 myobjїElementIndex]ї1] =
                                              document.formsїFormIndex].elementsїElementIndex].value;	
        }       
      }
      
   if(FormIndex == document.forms.length)
    window.location.href = "guest.php?form_name=offer_fields&myobj=" + myobj + "&chk=" + chk + "&offer_cnt=" + offer_cnt + "&more_cnt=" + more_cnt;
  }

</script>
description:

Firstly sorry for the poor indenting this thing is giving me problems not allowing me to indent the way i want. maybe its because im using Mozilla i dont know.

basicaly im creating a two dimentional array containing all the names and values of all the variables on the template. where "offering_suburb" and "more_suburb"
are multi-select boxes hence for their value part i store an array containing all the values
the reason i also have separate counters for each of those is because i have to know how many selected for each of those boxes for later use. when sending the created array through the url it looses comletely the structure and sends it as a comma separated !!!!string!!!!!. which is a problem. if anyone knows how to solve it will be of greate help and will be greatly appreciated

then i use the following code on my PHP side to rebuild the array that i want from the string sent:

Code: Select all

<?php

//turning the string into a single dimention array
       $myobj = explode(",", $myobj);
    
       //creating my "goal" array
       $obj = array();
       //declaring three counters
       $cnt = 0; //to count how many times it been trough the for loop (i.e. 2 at a time)
       $seccnt = 0; //set to 0 and 1 in respective to above (i.e. 0=name, 1=value)
       $arrcnt = 0; //increments by 1 untill eventualy is equal to the number of variables from the template
//these are the two counters sent from the template for the multi-select boxes       
//$offer_chk = 0;        
//$more_chk = 0;
       
       for($a = 0 ; $a <= count($myobj) ; $a++)
        {
         $cnt++;
      
         if($cnt == 1)
          $obj[$arrcnt] = array();
       
         if($myobj[$a] == "offering_suburb")
	  {
	   $o = true;
	   $obj[$arrcnt][$seccnt] = $myobj[$a];	   
           $seccnt++;
	   continue;
	  }
	 elseif($myobj[$a] == "more_suburb")
	  {
	   $m = true;
	   $obj[$arrcnt][$seccnt] = $myobj[$a];
           $seccnt++;
	   continue;
	  }
	 elseif((!$o) && (!$m))
	  {
	   $obj[$arrcnt][$seccnt] = $myobj[$a];
           $seccnt++;
	  }	 
	 
	 if(($o) && ($cnt == 2) && (!is_array($obj[$arrcnt][$seccnt])))
	  $obj[$arrcnt][$seccnt] = array();
	  
	 if(($o) && ($cnt == 2))
	  {
	   for($offer_chk = 0 ; $offer_chk <= ($offer_cnt - 1) ; $offer_chk++)
	    {
	     $obj[$arrcnt][$seccnt][$offer_chk] = $myobj[$a];	     
	     $a++;
	    }
	   
	   $o = false;
	  }
	  
	 if(($m) && ($cnt == 2) && (!is_array($obj[$arrcnt][$seccnt])))
	  $obj[$arrcnt][$seccnt] = array();
	    
	 if(($m) && ($cnt == 2))
	  {
	   for($more_chk = 0 ; $more_chk <= ($more_cnt - 1) ; $more_chk++)
	    {
	     $obj[$arrcnt][$seccnt][$more_chk] = $myobj[$a];
	     $a++;
	    }
	    
	   $m = false;	   
	  } 
	  
	 if($cnt == 2)
          {
           $cnt = 0;
	   $arrcnt++;
	   $seccnt = 0;
          }     
        }

?>
this works. but its causing me to write a lot of extra code in order to work with it.

is there a way to keep structure of the array the way it is made and send it as is through the url.

thank you in advance,
Tal.

Posted: Thu Aug 26, 2004 7:46 am
by Lord Sauron
how does the array look like, when it isn't passed as it should be?

Have you already tried the following functions:
base64_encode(serialize($var))
rawurlencode($var)