K.K. Smith
K.K. Smith

Reputation: 992

Simple issue with checkboxes in a SQL form submit

So forgive this dumb question. I'm a music teacher, not a programmer.

I am submitting a form. I have possibly checked checkboxes for "spoken languages".

<input type="checkbox" id="speakEnglish" name="speakEnglish" value="yes"  />
<input type="checkbox" id="speakGerman" name="speakGerman" value="yes"  />

And then on form submit I have

$speakEnglish = $_POST['speakEnglish'];
$speakGerman = $_POST['speakGerman'];

And formulate my SQL query from that. It was submitting and INSERTing fine on my local host, but since uploading to a server I'm getting this when a checkbox is empty...

AN error occurred in the script '/home/berli/public_html/TEST/signup.php' on line 295: 
Undefined index: speakGerman

This confuses me .. though I understand whats going on.

But why was my form submitting with empty checkboxes before, and now it throws an error? What is the normal way to handle this?

Something like..?

if(isset($_POST[checkboxVar]) { $SQLcheckboxVar = $_POST[checkboxVar]} };

I just don't understand why empty checkboxes didn't hang me up on local host, but now they do. I cant think of anything else I could have done to cause that and I certainly wasn't testing for the checkbox variables before... but I have a SQL table full of INSERTed entries.

Is there some kind of difference on how this would work locally vs on a server?

Upvotes: 1

Views: 305

Answers (3)

Handfeger
Handfeger

Reputation: 684

The simplest way to avoid this Notice is to check if your Checkbox has been checked with something like:

if (!isset($_POST['checkboxVar'])) $_POST['checkboxVar'] = 'no';

or

$checkboxVar = isset($_POST['checkboxVar']) ? $_POST['checkboxVar'] : 'no';

The Problem you ran into is, that the browser will not submit anything in the formdata if your checkbox hasn't been checked.

Upvotes: 1

Benno
Benno

Reputation: 3008

Your servers error reporting must show notices/warnings (not sure specifically which one that is).

You are correct in saying that if(isset($_POST['checkboxVar'])) is all you would need to hide that error if you can't change your php configuration (php.ini). This is what you need on your online server (usually /etc/php.ini):

;Show all errors, except for notices and coding standards warnings
error_reporting = E_ALL & ~E_NOTICE

Upvotes: 2

clops
clops

Reputation: 5235

Your local configuration is probably using the default php settings, which are suppressing any Notices. The production server has a stricter error reporting which lets you know of tiny (most probably not important) problems.

In this case you are trying to access a variable which is not defined, as the browser is NOT sending any data for checkboxes that have not been selected.

As already pointed out above, a simple if(isset($_POST[checkboxVar])) will do the trick to get rid of the notice on per-variable level.

In case you wish to stay a music teacher and not break your head against the wall every time these things pop up it may be easier to simply put the following as the first line of your program:

error_reporting(E_ALL & ~E_NOTICE);

Upvotes: 2

Related Questions