Click to See Complete Forum and Search --> : CheckBox and its Value?!


said_fox
04-23-2003, 07:50 PM
Hi,
The default situiation for checkbox form element onsubmit, is to send
its value if it is checked, or not send any value at all and the server side
script does not catch its value or in other word, it does not feel with
its presence at all, if it it's not checked.

My question is:
How to change this default situiation?
i.e If checkbox is checked, send a value, and if it is NOT checked, send another value.

You may get a head with this problem if you look at the following Example:

Note: This example may need PHP server enabled to produce dynamic form elements.

<html>
<head>
<title>ToFormProcess</title>
</head>
<body>
<form action="form2.php" method="post">
<?
for ($i = 0; $i < 4; ++$i)
{
?>
<input type="text" name="txt[]">
<input type="text" name="txt1[]">
<input type="checkbox" value="yes<?=$i?>" name="chk[]" id="c<?=$i?>">
To Send this row check before submit <?=$i ?><br>
<?}
?>
<input type="submit">
</form>
</body>
</html>

The above code will produce a form with 4 rows of form elements, each row contain two text
fields and a check box

The serverside script interbreter, in our case here is PHP, will assign each element value
in an array of its name,i.e the first text field in the first row value will be $txt[0],
the second text field in the first row value will be $txt1[0] and the value of the check box
in the first row will be $chk[0]. HERE is the problem, i.e if the first checkbox does not checked
The $chk[0] will be assigned to the second checkbox.!?:(

DrDaMour
04-23-2003, 08:59 PM
not that i ever heard of, what the form submits is:

checkboxname=id

so either you can use differnet names of the checkbox name attributes, OR if php really doesn't set the indexes right what you do, is you loop through the checkbox array and see if any of the indexes are equal to a certain value


so say you have a checkbox with value="ab" & value="bc"

in php you have a variable

ab = false;
bc = false;

for(int i = 0;i<#of checkboxes in array;i++){
if (chk[i] == "ab") ab = true;
if (chk[i] == "bc") bc = true;
}

then you'll know what's true and what isn't

said_fox
04-24-2003, 01:01 AM
After deep thinking, I found what I want.
To know directly what I had mean look at these two files
toform.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>ToForm</title>
</head>
<body>
<form action="form2.php" method="post">
<?

for ($i = 0; $i < 10; ++$i)
{
?>
<input type="text" name="txt[]" value="<?=$i?>">
<input type="text" name="txt1[]" value="<?=$i?>">
<input type="checkbox" value="y<?=$i?>" name="chk[]" id="c<?=$i?>">
<input type="hidden" name="hide[]" value="y<?=$i?>">
Chhos Num <?=$i?><br>
<?}
?>
<input type="submit">
</form>
</body>
</html>


And the second file form2.php


<?
$time1 = microtime();
function said_fox(){
global $hide,$chk,$txt,$txt1;
for ($i =0; $i < count($hide); ++$i)
//The main loop inwhich an examination for the value of hide
//and its corresponding value of chk to secure printing of
//checked rows only.
{
if ($hide[$i] == $chk[$i])
{
print($txt[$i]."|| ".$txt1[$i]."<br>");

}
else
{
//In the case of uneqaulity between chk and hide, so, the
//existing row, indicated by i value, is not checked.
//so this loop move the check for the succesive row.
for($k =$i; $k < count($hide); ++$k)
{

if($hide[$k] == $chk[$i])
{
print($txt[$k]."|| ".$txt1[$k]."<br>");
break;
}

}

}

}

}
said_fox();
echo"<hr>";
$time2 = microtime();
$time = $time2 - $time1;
echo($time);
?>

If you put these two files in the same folder on PHP enabled webserver, you will feel and know exactly what I had mean.
Thank You again

said_fox
04-24-2003, 01:14 AM
You will notice, in the solutio, that,
The presence of hidden form element has the same value with the corresponding checkbox in the same row. This hidden element will work as a reference to be used later inside the serverside script to determine which row should be processed, if its checkbox is checked,.
also Mr. Clark supply me with a valuable notice about using radio button, however, in this case I would liked it to be appeared as what going in web based mail like yahoo or hotmail during selection some messages to be deleted or moved to another folder, by a mean of checkbox then pressing on the suitable button.
Again Thank you so much for help:)