|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Php Checkboxes, not recognizing variables in Post page
There quite a few checkboxes on my pages
x.php If i "check" any of them, it's able to update the tables successfully and add it to the database. I use array names for the checkboxes i.e. <input type="checkbox" name="prob[]" value="hello"> Hello</input> However if i dont' select anything, upon hitting Submit, I would expect the array to be null or empty. However any reference i make to prob returns a message saying Undefined variable: prob in /path on line 10 The checks i have tried so far are if (strcmp($prob[0],"") != 0) if($prob != null) How does php deal with no checkbox selection ? There might be a very simple answer to it. Thanks, |
|
#2
|
||||
|
||||
|
There've been a couple of posts on this very topic in these forums fairly recently, I think. Try doing a search, and if that doesn't pan out, post again.
|
|
#3
|
|||
|
|||
|
I'm having this same problem too. I can't find any other posts on this, constructive feedback would be appreciated
|
|
#4
|
||||
|
||||
|
You might try if(is_set($_POST["prob"])) or even just if($_POST["prob"]). Part of the problem is probably that you're using an unscoped variable. If you use the $_POST array instead, you won't get the "variable not defined" error.
|
|
#5
|
||||
|
||||
|
jsrehder,
I recently encounted this issue on my own... if you don't check the checkbox, then the checkbox doesn't exist (sounds philisophical, doesn't it?)... dhouston sums it up pretty well... personally, i used: if(isset($_POST['checkbox[0]'])) print "its checked!"; alternatively: if(!isset($_POST['checkbox[0]'])) print "its not checked!"; |
|
#6
|
|||
|
|||
|
Answer
Here's the answer I came up with. Now, I'm just gonna post the whole code, but I needed two checkboxes per row. One for whether the user wanted the data public, and one for whether that data should also be considered "featured". I used checkboxes for both data to create a sql statement for each option. This required me to create 4 sql statements, however, which sucked.. but hey, it got the job done.
I needed to know 1) the id of the row's data, 2) whether 'public' was checked, and 3) whether 'featured' was checked. If 'featured' was checked and 'public' was not, I made sure to throw in a little extra snippit so that both 'featured' and 'public' were set to 'f'. (there can't be a featured piece of data unless it's public was my logic). So, here it all is. --------- the form page ------------- Code:
<?
$total_rows = 0;
foreach($items as $key => $val)
{
.............
.............
<td class="field-cell"><input name="public[<?=$val["listingID"]?>]" type="checkbox" id="public"<? if ($val["show"]=='t') { echo " checked"; } ?>></td>
<td class="field-cell"><input name="featured[<?=$val["listingID"]?>]" type="checkbox" id="featured"<? if ($val["featured"]=='t') { echo " checked"; } ?>></td>
<? $allIDs = $allIDs.'|'.$val["listingID"] ?>
</tr>
<?
$total_rows++;
}
............
............
<input type="hidden" name="allIDs" value="<?=$allIDs?>">
<input type="hidden" name="total_rows" value="<?=$total_rows?>">
<input class="buttons" name="save" type="submit" id="save" value="Save Changes">
--------- The processing page --------------- PHP Code:
BTW, if you're wondering about the $db->update($sql); stuff, that's an object I created to make things a bit easier for myself. Just execute the $sql and it will work for you fine. Enjoy. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Php Checkboxes, not recognizing variables in Post page |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|