RE: HTML Forms question...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



	OK.  This has been most helpful, but now I am getting something
strange.  The first element of the array of data is behaving strangely.  For
the code snippet below I am not getting the first selected element
displayed:

<?php
... Some stuff snipped...
$a = 0;
?>
<?php
  while (isset($_POST['system'][$a])) { 
    $a++;
	echo $_POST['system'][$a];
  }
?>

	The second, third, and so forth selections all print, just not the
first one.  Also, I can test the contents of the first element by placing an
echo before the while loop.  Thanks in advance.


-----Original Message-----
From: 1LT John W. Holmes [mailto:holmes072000@charter.net]
Sent: Tuesday, November 19, 2002 11:11 AM
To: NIPP, SCOTT V (SBCSI); 'Ryan Jameson (USA)'; php-db@lists.php.net
Subject: Re:  HTML Forms question...


> OK.  If I am using the POST method and the checkbox 'name' for all
> of the checkboxes is 'system', how do I access the results?  I am
> researching on the PHP site trying to figure out what this array is, and
how
> I access this if the index name is the same for all elements.  I am hoping
> that this data will be in an array and I can simply loop through the
> contents of the array.  If this data is stored in a hash (Perl word for
this
> type of array, not sure if it's the same in PHP), how do I access this
data
> since the index for every element would be the same?
> I am SURE that I am probably missing some important conceptual
> issues here, but still learning as I go.  Unfortunately, this probably
means
> that I will be completely rewriting this application at least once in the
> future.  Oh well, live and learn.  Thanks again for the help.

If you have multiple checkboxes with the same 'name' then you should name
then with brackets, [], such as 'system[]'. This will tell PHP to make the
results an array when the form is submitted.

For any of the 'submit[]' checkboxes that are checked, they will be present
in PHP array on the processing page. Those unchecked will not be present.

Say you have the following.

<input type="checkbox" name="submit[]" value="1">One
<input type="checkbox" name="submit[]" value="2">Two
<input type="checkbox" name="submit[]" value="3">Three

If the method on your form is POST, and the user checks One and Three, then
you'll have the following values in PHP on the ACTION page of the FORM.

$_POST['submit'][0] ==> 1
$_POST['submit'][1] ==> 3

>From that data, though, there's no way to tell that checkbox "Two" was not
checked, unless you go through and check all of the values. That's fine for
small forms, but a hassle for large ones.

You could also name your checkboxes like this:

<input type="checkbox" name="submit[1]" value="1">One
<input type="checkbox" name="submit[2]" value="2">Two
<input type="checkbox" name="submit[3]" value="3">Three

and if the user checks one and three again, you'll get

$_POST['submit'][1] ==> 1
$_POST['submit'][3] ==> 3

Hopefully that clears some things up. If you have any other questions, just
ask.

---John Holmes...

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux