SunQuest
 
           PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old December 11th, 2003, 03:38 PM
jsrehder jsrehder is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 1 jsrehder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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,

Reply With Quote
  #2  
Old December 11th, 2003, 03:59 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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.

Reply With Quote
  #3  
Old December 17th, 2003, 06:00 AM
Chump Chump is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 1 Chump User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm having this same problem too. I can't find any other posts on this, constructive feedback would be appreciated

Reply With Quote
  #4  
Old December 17th, 2003, 07:03 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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.

Reply With Quote
  #5  
Old December 18th, 2003, 10:45 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
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!";

Reply With Quote
  #6  
Old March 18th, 2004, 05:53 PM
nate nate is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 5 nate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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:
// create the sql string dynamically here
        
$allIDs substr($_POST["allIDs"],1); // Trim off first pipe (|)
        
$allIDs explode("|",$allIDs); // Put all the id numbers on the page into an array
        
        // First do the public checkboxes
        
foreach ($allIDs as $key => $val) { // iterate through all the ids
            
if (isset($_POST['public']) && array_key_exists($val,$_POST['public'])) {
                
// this id is checkmarked
                
$sql.=" or `listingID`=$val";
            } else {
                
// an ID is missing from the post, so include it in the sql changing it to false
                
$sql2.=" or `listingID`=$val";
            }
        }
        
        
// Now do it for the featured
        
foreach ($allIDs as $key => $val) { // iterate through all the ids
            
if (isset($_POST['featured']) && array_key_exists($val,$_POST['featured'])) {
                
// this id is checkmarked, but we need to set featured='f' if public=='f'
                
if (!isset($_POST['public']) || !array_key_exists($val,$_POST['public'])) {
                    
$sql4.=" or `listingID`=$val";
                } else {
                    
$sql3.=" or `listingID`=$val";
                }
            } else {
                
// an ID is missing from the post, so include it in the sql changing it to false
                
$sql4.=" or `listingID`=$val";
            }
        }
        
        
// Now finish and run the statements
        
if ($sql) { // finish the sql string to set public='t'
            
$sql substr($sql,4);
            
$sql "UPDATE `table` SET `public`='t' WHERE (".$sql.");
            // run it through the db
            $affected1 = $db->update($sql);
            //echo "
<br />".$sql;
        }
        if ($sql2) { // finish the sql string to set public='f'
            $sql2 = substr($sql2,4);
            $sql2 = "
UPDATE `tableSET `public`='f' WHERE (".$sql2.");
            
// run it through the db
            
$affected2 $db->update($sql2);
            
//echo "<br />".$sql2;
        
}
        if (
$sql3) { // finish the sql string to set featured='t'
            
$sql3 substr($sql3,4);
            
$sql3 "UPDATE `table` SET `featured`='t' WHERE (".$sql3.");
            // run it through the db
            $affected3 = $db->update($sql3);
            //echo "
<br />".$sql3;
        }
        if ($sql4) { // finish the sql string to set featured='f'
            $sql4 = substr($sql4,4);
            $sql4 = "
UPDATE `tableSET `featured`='f' WHERE (".$sql4.");
            
// run it through the db
            
$affected4 $db->update($sql4);
            
//echo "<br />".$sql4;
        



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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Php Checkboxes, not recognizing variables in Post page


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway