form handling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

form handling

Post 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?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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++;
    }
  }
}
?>
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

nope

Post 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.
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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.
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

nope

Post by uberpolak »

Yes I am passing items, I tried renaming the vars and it still comes up with nothing.
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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).
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

it's a numeric field

Post by uberpolak »

$book[3] is numeric, actually I tried checking if it was null before anything else...
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post 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?
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

new development

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