Hi all,
I am looking to pass a javascript array that is created by the user (through checking different boxes and populating the array with id numbers) to a php script which this uses the id's in the array to make url calls. I am having difficulty passing the array to the script. I've tried many different ways of passing it but I decided to just ask for help instead of wasting more of my time on something I'm just starting to learn. Here's the snippet of code where I try to pass the array:

<form name="input" action="compare.php?id=graphs" method="get">
<input type="submit" value="Compare" />
</form>

where graphs is an array of id's. I'm aware this isn't proper syntax, but I'm assuming this is possible to do. I know that php is server side code and js is client side but since I am not executing the php script until after the page has been loaded I think I can still pass in a js array, right? Any help would be appreciated.

Recommended Answers

All 2 Replies

Ctaylo, passing your array the way you have done will only send a string value in a $_GET variable called id, accessible as $_GET. The solution to your problem will be for each of your checkboxes have their name attribute set to "graph[]", and their respective value attribute set, to the value they are to contain. Now when you select some options and click the Submit button, in your PHP page you can access your graph array through the $_POST variable.

eg. The checkboxes should be displayed in a similar fashion as below

echo "<input type='checkbox' name='graph[]' value='blue' /> Blue Color <br/>";
echo "<input type='checkbox' name='graph[]' value='green' /> Green Color <br/>";
echo "<input type='checkbox' name='graph[]' value='yellow' /> Yellow Color <br/>";
echo "<input type='submit' name='submit' value='Submit' />";

eg. The graph array should be accessed in PHP as below

$aGraph = isset($_POST['graph']) ? $_POST['graph'] : array();

foreach($aGraph as $iIndex => $sColor)
{
   echo $sColor."<br/>";
}

That should be your solution to the problem.

Sorry for the large delay in responding, I was on Thanksgiving break. I appreciate the response, but I need some clarification. I am building this array using checkboxes, but I also do a couple things with the array. Here is some snippets of my code:

var graphs = [];

 //Handles click on compare checkbox
    function compareClick(i)
    {
        //if box wasn't checked before, add id of marker to graphs array
    	if(gmarkers[i].checked == false)
    	{
    		gmarkers[i].checked = true;
    		graphs.push(gmarkers[i].id);
    		//check if more than 3 boxes are checked. If so, disbale the rest of the checkboxes.
    		if(graphs.length>=3)
        	{
    	    	    for(var q=0; q<gmarkers.length; q++)
    	    	    {
    		        if(gmarkers[q].checked==false)
    				    document.getElementById(q).disabled = true;
    		    }
        	}
    	}
    	else
    	{
    		//If we are unchecking marker, we need to remove marker id from the 
    		//graphs array
    		gmarkers[i].checked = false;
    		var temp = [];
    		var temp_num;
    		for(var t=0; t<graphs.length; t++)
    		{
    			temp_num = graphs.pop();
    			if(temp_num = gmarkers[i].id)
    			{
    				for(var u=0; u<temp.length; u++)
    				{
    					var temp2 = temp.pop();
    					graphs.push(temp2);
    				}
    				break;
    			}
    			else
    				temp.push(temp_num);
    		}
    		for(var q=0; q<gmarkers.length; q++)
				document.getElementById(q).disabled = false;
    	}	
    	
    }

I only want three checkboxes to be checked at once, so that's mainly what this code does. It also populates graphs with the id numbers of each marker.

where if a checkbox is clicked in my sidebar the code looks like this:

side_bar_html += '<td class="col1" align="center"><input type="checkbox" id=' + i + ' name="cmp" value=i onClick="compareClick('+ i + ')" ></td></tr>' ;

Where I is a global variable that keeps track of the marker index. When they click the compare button, this is the code that is fired:

<form name="input" action="compare.php?id='graphs'" method="get">
				<input type="submit" value="Compare" />
				</form>

I apologize if I didn't describe my problem well enough initially, or if I'm missing your point. There are probably better ways to do this, but if I don't use the checkboxes to create the array graphs but use my own function, how can I pass the array graphs to my php function and then run through it and get the id numbers? Thanks in advance!

Caleb

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.