PHP while loop help |
PHP while loop help |
eddy556 |
Nov 15 2006, 03:08 PM
Post
#1
|
Group: Members Posts: 6 Joined: 14-November 06 Member No.: 880 |
I am trying to pass varibles from a HTML form using post to the script below. The HTML form is a list of checkboxes each named "chck_box1",
"chck_box2", etc.. as I don't know exactly how many check boxes will be checked I need this script to check through each sequentually to see if it is set, however I keep getting errors saying a box is not set. <?php session_start(); //SESSION record count contains the number of checkboxes on the previous form $i = $_SESSION['record_count']; $z = 0; while ($z < $i) { ($z ++); $chkbox = "chck_box" . $z; $y = $_POST[$chkbox]; if (isset($y)) {echo "$y"; } else {exit(); } } ?> Thank you |
Brian Chandler |
Nov 16 2006, 01:24 AM
Post
#2
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
I am trying to pass varibles from a HTML form using post to the script below. The HTML form is a list of checkboxes each named "chck_box1", "chck_box2", etc.. as I don't know exactly how many check boxes will be checked I need this script to check through each sequentually to see if it is set, however I keep getting errors saying a box is not set. <?php session_start(); //SESSION record count contains the number of checkboxes on the previous form $i = $_SESSION['record_count']; $z = 0; while ($z < $i) { ($z ++); $chkbox = "chck_box" . $z; $y = $_POST[$chkbox]; if (isset($y)) {echo "$y"; } else {exit(); } } ?> Thank you Can't see anything obviously "wrong", but I don't understand what error message you are getting. Do you have the session thing working properly? (I couldn't get it to work...) Couple of points: Learn to put in diagnostics: echo "<pre>"; print_r($_POST); echo "</pre>"; Above you do seem to be printing the _value_ of the checkboxes, which is typically 'x'. So even if it worked I would expect the above program to print something like "xxxxx" if 5 boxes are ticked. Generally it seems a good idea to go through the whole lot of POST arguments: foreach ($_POST as $arg => $val) { // parse $arg - if it is 'chk_boxX' process 'X' // if POST argument is entirely unexpected write a diagnostic to your error log // This may catch program errors or detect hacking attempts ... } |
eddy556 |
Nov 16 2006, 07:45 AM
Post
#3
|
Group: Members Posts: 6 Joined: 14-November 06 Member No.: 880 |
The session stuff works fine and I've managed to echo it to view the result, which is correct. What happens is as the checkboxes are checked it will display their value fine, the problem occurs when there is a checkbox left uncheck, I get an error saying variable not defined, I've tried isset() but it just doesn't work
|
Brian Chandler |
Nov 16 2006, 09:01 AM
Post
#4
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
The session stuff works fine and I've managed to echo it to view the result, which is correct. What happens is as the checkboxes are checked it will display their value fine, the problem occurs when there is a checkbox left uncheck, I get an error saying variable not defined, I've tried isset() but it just doesn't work Yes, that's what's meant to happen. The way an HTML form sends the parameters is all a bit of an ad-hoc mess (what do you expect from people who can't spell?), and in particular for checkboxes the parameter is sent if the checkbox is selected, not sent if it isn't. In practice if($_POST['chkbxname']) succeeds when selected and not when not. (In theory I suppose you should make sure that the value returned is not, for example 0, but that will not normally happen.) If a checkbox is not selected, and you try to do something with its value, obviously you _should_ get an error message. Incidentally, looking at your bit of program again, you seem to be saying "if not set, exit program" which can't be right. |
Curtis |
Dec 28 2006, 06:06 AM
Post
#5
|
Newbie Group: Members Posts: 11 Joined: 28-December 06 From: California Member No.: 1,397 |
If you want to stick to this method (alternative, see below), I'd suggest rewriting to something like this (if you started naming chk_box from 0, you'll need to change your code to increment at the end of the loop):
CODE <?php $i = 0; $j = intval($_SESSION['record_count']); // ensure int while ( $i++ < $j ){ // $i = 1, 2, 3, ..., $j if ( isset($_POST["chk_box{$i}"]) && !empty($_POST["chk_box{$i}"]) ){ // do stuff } } ?> You can do this much easier. PHP allows you to keep track of checkboxes easier: In your HTML form, make all checkbox names like this: CODE <input type="checkbox" name="chk_box[]" ...> <br> <input type="checkbox" name="chk_box[]" ...> When the PHP script receives the request, it stores a 2-dimensional array in the symbol table, looking like this: CODE print_r($_POST['chk_box']); Array ( [chk_box] => Array ( [0] => blah [1] => blah ) ) You'd have to rework each form, if you want to switch. This post has been edited by Curtis: Dec 28 2006, 06:08 AM |
Lo-Fi Version | Time is now: 23rd April 2024 - 04:44 AM |