Page 1 of 1

form handling

Posted: Fri Feb 14, 2003 2:28 pm
by uberpolak
I have a form, which generates fields like this:

Code: Select all

<?php

 for ($z = 1; $z <= $items; $z++)
 {
  $book = mysql_fetch_row($query);
  echo "<TR>";
  echo "<TD WIDTH="90%" ALIGN="left" VALIGN="middle">" . $book[2] . " - <I>"" . $book[1] . ""</I>" . "</TD>";
  echo "<TD WIDTH="10%" ALIGN="center" VALIGN="middle"><INPUT TYPE="checkbox" NAME="checkin_" . $z . "" VALUE="" . $book[3] . ""></TD>";
  echo "</TR>";
 }

?>
The action and method are decalred earlier in the code. It is using the POST method. In the script it calls, I have this code:

Code: Select all

<?php

for ($z = 1; $z <= $_POST['items']; $z++)
{
 if ($_POST["checkin_" . $z] > 0)
 {
 mysql_query("UPDATE books SET status=0, due='0000-00-00' WHERE bar=" . $_POST["checkin_" . $z],$con);
 $items++;
 }
}

?>
Problem is, the condition here keeps evaluating false, even when the boxes created in the previous form are checked, thus keeping the script from having any purpose. Anyone know what I'm doing wrong?

Posted: Fri Feb 14, 2003 2:38 pm
by daven

Code: Select all

<?php
for ($z = 1; $z <= $_POST['items']; $z++)
{
  $box_name="checkin_".$z;
  if(isset($_POST[$box_name])){ //use isset to see if the 
    if ($_POST["checkin_" . $z] > 0)
    {
      mysql_query("UPDATE books SET status=0, due='0000-00-00' WHERE bar=" . $_POST["checkin_" . $z],$con);
      $items++;
    }
  }
}
?>

nope

Posted: Fri Feb 14, 2003 2:43 pm
by uberpolak
I made the change and it's still no good... it still doesn't find that the variable is set, even though it is (I know this for a fact, I set it by checking the box in the form).

EDIT: I just tried adding two echo statements to the if statement, if the condition came up true, it would say "set", i then added an else {} bracket where it would say "not set", neither thing was done.

Posted: Fri Feb 14, 2003 2:57 pm
by daven
Are you actually looping through at all (ie--did you pass 'items')? If so, try naming your checkboxes with just the index (ie: $z, $_POST[$z]) to see if it is the dynamic naming that is getting you.

nope

Posted: Fri Feb 14, 2003 3:00 pm
by uberpolak
Yes I am passing items, I tried renaming the vars and it still comes up with nothing.

Posted: Fri Feb 14, 2003 3:07 pm
by daven
I am not sure what datatype the checkbox values are. However, if they are not numeric (ie--if $book[3] is a string), then your conditional statement will not function properly. Try
if (isset($_POST["checkin_".$z] && $_POST["checkin_" . $z] != "")
(ie--check for null status rather than numeric value).

it's a numeric field

Posted: Fri Feb 14, 2003 3:45 pm
by uberpolak
$book[3] is numeric, actually I tried checking if it was null before anything else...

Posted: Fri Feb 14, 2003 6:18 pm
by patrikG

Code: Select all

for ($z = 1; $z <= $_POST&#1111;'items']; $z++)
Form-objects start with 0, so your for-next loop should read:

Code: Select all

for ($z = 0; $z <= $_POST&#1111;'items']; $z++)
Apart from that: try eliminating the underscore "_" from the form-object's generic name. If I remember correctly, javascript can have a problem with that. But don't quote me on that :P

Posted: Sat Feb 15, 2003 9:44 am
by uberpolak
These elements are named starting with 1, I set them to do that in the first script. Also, no javascript is involved in this, so what it does and doesn't have a problem with will make no difference. Thanks for all your continued efforts.

EDIT: I gave the script a test, I put this line in the for:

echo "hi";

this wasn't inside any if statements or anything like that, it should've just echoed it, but nothing came up, so for some reason it's not executing the for loop. Does this suggest anything to anyone?

new development

Posted: Sat Feb 15, 2003 1:09 pm
by uberpolak
I modified the script a bit (out of curiosity) to not use the superglobals anymore and for some reason it worked.

(ie: $_POST["checkin_" . $z] becomes ${"checkin_" . $z})

I scanned through the php.ini file to see if someone may have disabled the superglobals anywhere, but I can't find an entry in the file that would do this to turn it back on. Does anybody know anything about enabling/disabling the superglobals? (or even if this is possible)

Thanks

PS: I'm using v4.2.3, so it's not that I'm using a version that predates the superglobals.