recover checkbox values

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
phpdev
Forum Newbie
Posts: 1
Joined: Thu Jul 24, 2003 8:17 am

recover checkbox values

Post 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));
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post 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));
kristian
Forum Newbie
Posts: 10
Joined: Tue Jul 29, 2003 1:21 pm

Post 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;
?>
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post 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">
Nunners
Forum Commoner
Posts: 89
Joined: Tue Jan 28, 2003 7:52 am
Location: Worcester, UK
Contact:

Post 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"); }?>>
jmarcv
Forum Contributor
Posts: 131
Joined: Tue Jul 29, 2003 7:17 pm
Location: Colorado

Post 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?
Post Reply