New Posts  All Forums:Forum Nav:

php Checkbox error

post #1 of 6
Thread Starter 
Hey Peeps. Learning php atm, and I have a question on something that is, I am sure, very simple and starring me in the face. I have spent about 3 hours on this simple line, and I am still getting my "unidentified Index" error when my check box is not checked.

Even through the search function here and google, I haven't found my answer...
So, I come to my fellow OCN'ers.


The check box code on my form..
Code:
<p><input type="checkbox" name="box1" value="yes" /> Checkbox</p>


The code for my POST method....
Code:
$box1 = $_POST['box1'];

        if (isset($_POST['box1']) && $_POST['box1'] == 'yes') {
            $box1 = 'checked';
        } else {
            $box1 = 'not checked';
        }

echo = $box1

Now, I understand that I have to check to see if the box is checked, as the value isn't sent to the page if it isn't.
Do I need to do a nested elseif statement in order to check for the check box THEN return the values? I have tried it so many ways and I have not gotten anything correct.

I very much appreciate any help!
    
CPUMotherboardGraphicsRAM
3770k Z77x-UD5H v1.1 GTX 560ti 1050/2100/1100 Crucial Ballistix 32g 1.35v 
Hard DriveOptical DriveOSMonitor
256g 840 Pro/ 250 Evo x2 Raid 0 / 2TB F4 Samsung BD +/- RW Win 7 x64 Ultimate LG IPS237L-BN 
PowerCase
Corsair AX850 Lian Li PC-K62 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
3770k Z77x-UD5H v1.1 GTX 560ti 1050/2100/1100 Crucial Ballistix 32g 1.35v 
Hard DriveOptical DriveOSMonitor
256g 840 Pro/ 250 Evo x2 Raid 0 / 2TB F4 Samsung BD +/- RW Win 7 x64 Ultimate LG IPS237L-BN 
PowerCase
Corsair AX850 Lian Li PC-K62 
  hide details  
Reply
post #2 of 6
Thread Starter 
Nevermind...........I just had to get rid of my variable assignment statement....

§


EDIT: If a MOD wants to close this thread, feel free to do so. I can't do it.
Thanks!
Edited by shlunky - 3/19/13 at 12:43pm
    
CPUMotherboardGraphicsRAM
3770k Z77x-UD5H v1.1 GTX 560ti 1050/2100/1100 Crucial Ballistix 32g 1.35v 
Hard DriveOptical DriveOSMonitor
256g 840 Pro/ 250 Evo x2 Raid 0 / 2TB F4 Samsung BD +/- RW Win 7 x64 Ultimate LG IPS237L-BN 
PowerCase
Corsair AX850 Lian Li PC-K62 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
3770k Z77x-UD5H v1.1 GTX 560ti 1050/2100/1100 Crucial Ballistix 32g 1.35v 
Hard DriveOptical DriveOSMonitor
256g 840 Pro/ 250 Evo x2 Raid 0 / 2TB F4 Samsung BD +/- RW Win 7 x64 Ultimate LG IPS237L-BN 
PowerCase
Corsair AX850 Lian Li PC-K62 
  hide details  
Reply
post #3 of 6
In case you are wondering why you had to get rid of that: It's because you were attempting to access an element of the $_POST array which doesn't exist, and you check for its existence (using isset) after you've already tried to access it.
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
post #4 of 6
Thread Starter 
Quote:
Originally Posted by randomizer View Post

In case you are wondering why you had to get rid of that: It's because you were attempting to access an element of the $_POST array which doesn't exist, and you check for its existence (using isset) after you've already tried to access it.

That makes total sense. I had run around everywhere looking on php sites and other places, and no where did it say to not define the variable, and I, being as new as I am, figured I had to define everything coming from the form.

It was one of those, AH HAH moments you know? lol

I am new to the programming aspect of the computer. I have always been a hardware junkie, but the job market in my area has way more opportunities for software, especially web, than it does for hardware. Not to mention the pay and hours are better too.

Thank you much for clarifying for me though. I saw a few examples on the web where people were using empty instead of isset. I didn't understand that, as I thought that isset was used for the checkbox?.
Is that the case or am I mistaken?

§
    
CPUMotherboardGraphicsRAM
3770k Z77x-UD5H v1.1 GTX 560ti 1050/2100/1100 Crucial Ballistix 32g 1.35v 
Hard DriveOptical DriveOSMonitor
256g 840 Pro/ 250 Evo x2 Raid 0 / 2TB F4 Samsung BD +/- RW Win 7 x64 Ultimate LG IPS237L-BN 
PowerCase
Corsair AX850 Lian Li PC-K62 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
3770k Z77x-UD5H v1.1 GTX 560ti 1050/2100/1100 Crucial Ballistix 32g 1.35v 
Hard DriveOptical DriveOSMonitor
256g 840 Pro/ 250 Evo x2 Raid 0 / 2TB F4 Samsung BD +/- RW Win 7 x64 Ultimate LG IPS237L-BN 
PowerCase
Corsair AX850 Lian Li PC-K62 
  hide details  
Reply
post #5 of 6
isset checks a subset of the conditions that empty checks. isset only checks if a variable exists and is not null (ie. it "is set" - one of the few PHP function names that makes sense!), whereas empty will return true if the variable is any of the following (ripped straight from the documentation):

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

Therefore you should be careful when using empty as it may return true unexpectedly.
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
post #6 of 6
Thread Starter 
Quote:
Originally Posted by randomizer View Post

isset checks a subset of the conditions that empty checks. isset only checks if a variable exists and is not null (ie. it "is set" - one of the few PHP function names that makes sense!), whereas empty will return true if the variable is any of the following (ripped straight from the documentation):

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

Therefore you should be careful when using empty as it may return true unexpectedly.

Awesome clarification. That actually makes logical sense.
Thanks for the info. I am surprised to say, I actually enjoy learning programming. In school right now. This semester is Java, PHP, VB, and Android development.
Lets hope I understand way more when it is time to go get a job! lol

Thanks again!
§
    
CPUMotherboardGraphicsRAM
3770k Z77x-UD5H v1.1 GTX 560ti 1050/2100/1100 Crucial Ballistix 32g 1.35v 
Hard DriveOptical DriveOSMonitor
256g 840 Pro/ 250 Evo x2 Raid 0 / 2TB F4 Samsung BD +/- RW Win 7 x64 Ultimate LG IPS237L-BN 
PowerCase
Corsair AX850 Lian Li PC-K62 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
3770k Z77x-UD5H v1.1 GTX 560ti 1050/2100/1100 Crucial Ballistix 32g 1.35v 
Hard DriveOptical DriveOSMonitor
256g 840 Pro/ 250 Evo x2 Raid 0 / 2TB F4 Samsung BD +/- RW Win 7 x64 Ultimate LG IPS237L-BN 
PowerCase
Corsair AX850 Lian Li PC-K62 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding