Page 1 of 1

recover checkbox values

Posted: Thu Jul 24, 2003 8:17 am
by phpdev
Hi!

I want recover the values of multiple checkbox, and pass the values to another page, I tryed the solution of php scott but it doesn't work.
the checkbox are generated dynamically with an sql request
Can you please help me !!!! :)
here is my code


<script language="JavaScript">
function CheckAllBox(e)
{
if (e.checked) {
CheckAll();
}
else {
ClearAll();
}
}

function Check(e)
{
e.checked = true;
}

function Clear(e)
{
e.checked = false;

}

function CheckAll()
{
var valuecheck="";

//alert(e1.name);
var ml = document.menuindex;
var size = ml.elements.length;
//var cont=document.getElementById('checkbox_name');

for (var i = 0; i < size; i++) {
var e1=document.getElementById('checkbox_name');
if (e1.name == "checkbox_name[]") {
Check(e1);
valuecheck+=e1.value+"#";
}
}
document.menuindex.check_name1.value=valuecheck;
ml.checkAll.checked = true;

}

</script>
/********the checkbox which check all the checkbox***********/

<input type="checkbox" name="chekAll" value=""
onClick="CheckAllBox(this)">

<?php
mysql_select_db($database_connection, $connection);
$view = mysql_query($query_limit_view, $connection) or die(mysql_error());

$row_result= mysql_fetch_row($view);
$totalRows_view = mysql_num_rows($view);
$fieldcounts = mysql_num_fields($view);

do{
unset($checkbox_name);
/*********create the checkbox**********/
echo "<input type=\"checkbox\" name=\"checkbox_name[]\"
id=\"checkbox_name\" value=\"".$row_result[0]."\">\n";

}while ($row_result = mysql_fetch_row($view));

Posted: Tue Jul 29, 2003 7:52 pm
by jmarcv
You are having the trouble with checking the checkboxes, I assume and not the javascript?

This is how to do it.
do{
unset($checkbox_name);
/*********create the checkbox**********/

$check='';
if ($row_result[0]) $check='CHECKED';
echo "<input type=\"checkbox\" name=\"checkbox_name[]\"
id=\"checkbox_name\" $check>\n";

}while ($row_result = mysql_fetch_row($view));

Posted: Wed Jul 30, 2003 5:10 am
by kristian
For multiple checkboxes, cant you just use the code below for each box?

Code: Select all

<?php
  if ($publish==1)&#123; // is true then print checkbox with checked attribute
?>
  <INPUT checked name="publish" type="checkbox" class="forms_tickbox" value="1" id="publish"> 
<?
  &#125;else&#123; // is false then print checkbox without checked attribute
?>
  <INPUT name="publish" type="checkbox" class="forms_tickbox" value="1" id="publish">
<?
  &#125;
?>

Posted: Wed Jul 30, 2003 9:05 am
by jmarcv
Yes kristian, you can, but it is bulky and redundant. Best as I illustrated, to make a variable hold the word checked, or nothing, and put that in your echo statement in double quotes to be expanded. That way, if you need to change the name, or the value, or whatever, there is less room for error by missing 1 of the 2.

eg: Your code:

Code: Select all

<?php 
$check='';
  if ($publish==1){ // is true then print checkbox with checked attribute 
   $check='CHECKED';
}
?> 
  <INPUT <? echo $check; ?> name="publish" type="checkbox" class="forms_tickbox" value="1" id="publish">

Posted: Wed Jul 30, 2003 11:24 am
by Nunners
Or even shorter... but slightly more messy:

Code: Select all

<INPUT name="publish" type="checkbox" class="forms_tickbox" value="1" id="publish" <?php if ($publish==1) { echo("CHECKED"); }?>>

Posted: Wed Jul 30, 2003 11:40 am
by jmarcv
Nunners
You can get rid of the curly brackets and make it even shorter!
Make it shorter yet, dump the () around your string!!
Still have a ways to got to 42, which I understand is the meaning of all things.

Is this the beginning of the optimization board?