Forum Moderators: coopster

Message Too Old, No Replies

PHP check box = "on" not working

         

darknyt

12:40 pm on Oct 26, 2009 (gmt 0)

10+ Year Member



hi

wonder if anyone can help....the below code is part of the page which goes thru a database and shows customer orders...the checkbox is named after sampleId row...so if tht is selected and remove completed pressed it shud go..it used to work fine with php 4..but not workin with php 5...
i guess if ($$i == "on") is the line which is not working

if ($ACTION == "REMOVECOMPLETED") {
//LOOP THROUGH SAMPLEIDS IF ANY CHECKBOXES ARE TRUE, QUERY DATABASE ALTERING SAMPLECOMPLETED FIELD TO TRUE.
$query = "SELECT sampleId FROM customersamples ORDER BY sampleId DESC";

$result = mysql_query($query);
$maxnumber = mysql_result($result, 0);

for ($i = 1; $i <= $maxnumber; $i++) {
//LOOP THE SAME NUMBER OF TIMES AS ACTIVE SAMPLES
//echo $$i . " Box $i";
if ($$i == "on") {
$query = "UPDATE customersamples SET sampleComplete = 'TRUE' WHERE sampleId = '$i'";
mysql_query($query) or die(mysql_error());
}

}

}

Zipper

2:20 pm on Oct 26, 2009 (gmt 0)

10+ Year Member



what exactly is $$i ?

darknyt

2:31 pm on Oct 26, 2009 (gmt 0)

10+ Year Member



sorry thts the checkbox...i hv linked it to the row sampleId

for instance:

<input type=\"checkbox\" name=\"$row[0]\"> Order completed</td>

rocknbil

5:13 pm on Oct 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard darknyt, a couple comments here.

Right off, don't know why the PHP upgrade broke it.

However, here is what I **think** you are doing, echo out statements to verify.

<input type=\"checkbox\" name=\"$row[0]\">

Generally "$row[0]" is the first field, normally an auto_increment field, which makes it a number. This is a bad idea for both the DOM (any JS or CSS should not begin with a number) and for PHP.

When you do this,

$$i

You have a variable variable, meaning, the name of the variable is stored in $i and $$i **should** be it's value.

But your variable starts with a number. Right?

PHP also disallows this. You can try something like

<input type=\"checkbox\" name=\"chk\_$row[0]\">

then

for ($i = 1; $i <= $maxnumber; $i++) {

$fieldname = 'chk_' . $i;
if ($$fieldname == "on") {
... etc.

However, something important to know about checkboxes and radio values: if they are **not checked,** they won't be in $_POST/$_GET/$_REQUEST. If checked, they will be set. So you don't need to test for "on."

if (isset($$fieldname)) {

The other possibility is that the posted values aren't getting into variables. So you may have to do

if (isset($_POST[$fieldname])) {