Ali Vojdanian
Ali Vojdanian

Reputation: 2111

checkbox status in php

I have a form with get method in php. In the form I created a checkbox which named Student.

I use these codes to show the details about the users in submit button action.

In the details page it shows error in it when the users aren't student. There is no problem when users are student :

if($_GET['student'] == "on")
{
    print "Student";
}
else
{
    print "Not student";
}

Upvotes: 0

Views: 83

Answers (2)

ohcibi
ohcibi

Reputation: 2698

A general solution is to add a hidden field which defaults to an empty value to omit the isset() call

<input type="hidden" name="student" value="">
<input type="checkbox" name="student" value="on">

The reason you have to do this is because the browser does not send any value at all if the checkbox is not checked (not even an empty one, the key is just missing).

Upvotes: 1

vusan
vusan

Reputation: 5331

Also put new condition as:

if(isset($_GET['student'] && $_GET['student'] == "on")

Then it should work.

Upvotes: 5

Related Questions