Page 1 of 1

[SOLVED] Moving an array of selected values to the next page

Posted: Sun Apr 17, 2005 12:57 am
by okposi
Hi Guys,
Could someone please help. I have jused a javascript to collect an array of items selected by a user.How can I send this array to another page for processing. More specifically, How can I pass this array to a php code to iterate though the items in the array.
Thanks for your help.
OJ

Posted: Sun Apr 17, 2005 1:38 am
by infolock
can you post your code?

Posted: Sun Apr 17, 2005 7:57 am
by okposi

Code: Select all

<script language = &quote;Javascript&quote;>
	function getAllCheckedBoxes(){
	var checkbox_choices = 0;
	var selectedArray= new Array();

		// Loop from zero to the one minus the number of checkbox button selections
		for (counter = 0; counter < checkbox_form.checkbox.length; counter++)
		{

			// If a checkbox has been selected it will return true
			// (If not it will return false)
			if (checkbox_form.checkboxїcounter].checked)
			{ 	
				selectedArray.push(checkbox_form.checkboxїcounter].value);
				checkbox_choices = checkbox_choices + 1; }

			}
		return selectedArray;

		}
	}
</script>
Here is the form

Code: Select all

<form method=&quote;get&quote; action=&quote;Show.php&quote; 
onsubmit=&quote;return checkbox_checker()&quote; name=&quote;checkbox_form&quote;>
<input type=&quote;checkbox&quote; value=&quote;1&quote; name=&quote;checkbox&quote;>Hank Aaron<br>
<input type=&quote;checkbox&quote; value=&quote;2&quote; name=&quote;checkbox&quote;>Ted Williams<br>
<input type=&quote;checkbox&quote; value=&quote;3&quote; name=&quote;checkbox&quote;>Hank Greenberg<br>
</form>

Then I want to pass the array returned from the javascript to a php function that will do the following:

Code: Select all

for($i=0;$i<selectedArray.length;$i++){
 print(selectedArrayї$i]);
 
 
 }


I will appreciate if someone can help me.Is it possible to pass the array returned by javascript to php session?. If so how can I do that
Thanks


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sun Apr 17, 2005 8:34 am
by feyd
your code is mixing Javascript and PHP without any translation between.

If you simply allowed the form to submit naturally, show.php would get each of the selected checkboxes.

Posted: Sun Apr 17, 2005 9:30 am
by okposi
Thanks for your response. If you look at the code, all the check boxes have the same name but different values. I tried that approach and I am only able to fetch the last value.
The post will look like:
checkbox=1&checkbox=2&checkbox=3
Therefore, I will only get Checkbox=3 on the next page.
I still need more help.
Thanks

Posted: Sun Apr 17, 2005 9:35 am
by feyd
change the name to 'checkbox[]' so php will create an array of the values...

Posted: Sun Apr 17, 2005 5:13 pm
by okposi
Thanks!! feyd,
It solved my problem. I did some twicking to manage th earray on the next page.
That was a smart solution for me.I don't even need use javascript any more.

Cheers
OJ