I'm using check-boxes to alter an SQL query.

The HTML:

        <input type="checkbox" name="chapter" value="7"  /><label>ch. 7</label><br />
        <input type="checkbox" name="chapter" value="13"  /><label>ch. 13</label>

The PHP:

   //adjust query for chapter
   $chap = $_POST['chapter'];
   $chapter = "";
   if(!empty($chap)){
        $N = count($chap);
        if($N == 1){
         $chapter .= " AND chapter = '".$chap[0]."'";
        }else{
         $chapter .= " AND (chapter = '".$chap[0]."' OR chapter = '".$chap[1]."')";
        }
   }    

       $zquery = "SELECT * FROM records WHERE ".$zipcodes.$chapter;

When the check boxes are left unchecked it works fine. When the chapter 7 box is checked it works fine. The trouble is with that 13 box. When I check that, $chap[] only returns a value of "1" instead of "13". For example, when I var_dump $zquery I get string(3384) "SELECT * FROM records WHERE (party_zip='34683' OR party_zip='34682' ) AND chapter = '1'".

Also, $N is always returning 1, even if I check both boxes. Is $_POST['chapter'] not an array? Why is it only returning the first character in the string?

Namaste

share|improve this question

use foreach($_POST as $value){ echo "values=".$value."</br>";} for knowing the each and every value of Post items use print_r($_POST); – Ankur Saxena 13 hours ago
Does it work when both 7 and 13 are selected? – Douglas A. Crosby 13 hours ago
@AnkurSaxena if you have an answer write in answer section.. – Dinesh 13 hours ago
@Douglas No, the var_dump I showed was after checking both boxes. The $N = count($chap); is retuning 1 every time. – Adelphia 13 hours ago
feedback

5 Answers

up vote 1 down vote accepted

Both Dr. Molle and Douglas are correct, but I'd like to expand a little on their answers.

$_POST is an array. It can contain a variety of values, either in a key => value structure or in a series of values indexed numerically. Each value can be any other type of data. In this case, your variable:

$_POST['chapter']

is a string, most likely - PHP interprets the array index ([0], etc) as a character index when used against a string (or against a variable that can be trivially cast to string, like an integer or float.) This is why you're getting the first letter out instead of the first value: there's only the one value to get!

Of course, you might run into situations where the value of an array is itself an array - in which case you could access the value by typing, say, $array[0]['candles'] or something, but that's not going to happen with the $_POST array (which is always an array of strings.)

share|improve this answer
Thanks for actually explaining that. I was wondering why it worked in my other scripts and not this one. – Adelphia 13 hours ago
No problem. Good luck with your script! – sudowned 12 hours ago
feedback

$_POST['chapter'] is not an array, use square brackets inside the name of the inputs:

<input type="checkbox" name="chapter[]" value="7"  /><label>ch. 7</label><br />
<input type="checkbox" name="chapter[]" value="13"  /><label>ch. 13</label>
share|improve this answer
feedback

Try removing the [0] if you only want one value returned from your form

$chapter .= " AND chapter = '".$chap."'";

Alternatively (and likely what you want), change your inputs to arrays and it should work

<input type="checkbox" name="chapter[]" value="7"  /><label>ch. 7</label><br />
<input type="checkbox" name="chapter[]" value="13"  /><label>ch. 13</label>
share|improve this answer
feedback

$_POST['chapter'] is not an array. $N is returning 1 beacuse $chap is not an array, if variable is not an array then count() function always returned 1.

You can use like

 name="chapter[]"  

Then $_POST['chapter'] should be an array.

share|improve this answer
feedback

$_POST is work as array it print all value of items post through form to know post values use

echo"<pre>";
 print_r($_POST);

$_POST work as array it contains all values which we posted through form.

for getting each value we can also use

foreach ($_POST as $value ) 
{
   echo $values."</br>";
}
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.