checkbox arrays and validation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all, 

this might be slightly off-topic in a way but Im looking for help not
flaming.

Ok I have a checkbox array that I populate from a DB using php:

while ($row = mysql_fetch_array($result))
      echo("<input type=checkbox name=chkmodels[] value=" .
$row['models_id'] . ">" . $row['models_type'] . "<br>");

So when I want to see each variable in the array I use:

		foreach ($_POST['chkmodels'] as $c)
		{
			echo("<br> value: " . $c);
		}

this is fine. 

but what I want to clarify is that if I change my PHP echo statement
to:

echo("<input type=checkbox name=chkmodels value=" . $row['models_id'] .
">" . $row['models_type'] . "<br>");

basically without the [] then it is still recognised as an array. But
when the array is passed back it is returned as a string separated by
commas. Then I should use the split() function to create an array.
IE:

$modelsArray = split('[,]', $chkmodels);

however when I try the second way (without the [])

like this:

if(isset($_POST['chkmodels']))
	$selModels=$_POST['chkmodels'];	
else
	header("Location: ../freesample.php");



if(isset($selModels))
{

	echo("selmodels: " .$selModels);
	$selModelsInd = split('[,]',$selModels);
	echo("<br>Ind modesl" . $selModelsInd);
	foreach ($selModelsInd as $c)
	{
		echo("<br> value: " . $c);
	}
}


I get the following results, when I select 2 checkboxes:

selmodels: 2
Ind modeslArray
value: 2

this is wrong and should output the following:

selmodels: 2
Ind modeslArray
value: 1
value: 2

as the values of the selected checkboxes are 1 and 2 and also there are
two that are selected.

Here is the OT part. how do I reference the checkboxes in javascript if
the chkmodels[] is used?

I have tried many things like:

myCheckboxArray = document.forms[0].elements["chkmodels[]"];

and 

myCheckboxArray = document.frmft[0].elements["chkmodels[]"]; //where
frmft is the name of my form.

If someone could please advise me as to what I'm doing wrong or why the
information is not being outputted as expected, that would be really
helpful.

thanks in advance
Angelo

--------------------------------------------------------------------
Disclaimer 
This e-mail transmission contains confidential information,
which is the property of the sender.
The information in this e-mail or attachments thereto is 
intended for the attention and use only of the addressee. 
Should you have received this e-mail in error, please delete 
and destroy it and any attachments thereto immediately. 
Under no circumstances will the Cape Technikon or the sender 
of this e-mail be liable to any party for any direct, indirect, 
special or other consequential damages for any use of this e-mail.
For the detailed e-mail disclaimer please refer to 
http://www.ctech.ac.za/polic or call +27 (0)21 460 3911

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux