user3488750 user3488750 - 1 month ago 5
Ajax Question

php ajax checkbox always checked

I have a form to display quotes based on data recieved/checked.

It's a non-refreshing page, so it uses ajax get the submit click and then it sends data to php file.

The main problem is that the checkboxes are ALWAYS checked, and are echo'ing "ON". I need the checkboxes to work as normal, unchecked by default and then when user clicks on it, and submits the form they will actually be checked.

These are the checkboxes

<div class="checkbox">
<label><input type="checkbox" name="lighting" id="lighting"><font color="#2C3E50" value="">Lighting</label></font>
<label><input type="checkbox" name="heating" id="heating"><font color="#2C3E50" value="">Heating</label></font>
<label><input type="checkbox" name="dancefloor" id="dancefloor"><font color="#2C3E50" value="">Dancefloor</label></font>
<label><input type="checkbox" name="internaldressing" id="internaldressing" value=""><font color="#2C3E50">Internal Dressing</label></font>

I tried removing value attribute, I tried adding checked attribute. literraly nothing helps.

This is part of the AJAX file:

var dataString = 'eventtype=' + eventtype + '&peoplecount=' + peoplecount + '&location=' + location + '&eventdate=' + eventDate + '&lighting=' + lighting + '&heating=' + heating + '&dancefloor=' + dancefloor + '&internaldressing=' + internaldressing;

type: 'POST',
url: 'process.php',
data: dataString,
success: function(data) {
if( data == '0' )
alert( 'Błędne dane logowania!!!' );


And then my PHP looks like this.


// Total price of quote.
$price = 0;
// Prices of extras.
$lighting_price = 0;
$heating_price = 0;
$dancefloor_price = 0;
$internaldressing_price = 0;

$eventType = $_POST['eventtype'];
$peoplecount = (int)$_POST['peoplecount'];
$location = $_POST['location'];
$eventDate = $_POST['eventdate'];

$lighting = $_POST['lighting'];
$heating = $_POST['heating'];
$dancefloor = $_POST['dancefloor'];
$internaldressing = $_POST['internaldressing'];

// If there are less than 70 guests then make base price 650.
if($peoplecount < 70){
$price = $price + 650;
$lighting_price = 75;
$heating_price = 150;

// If there are between 71 and 130 guests then make base price 950.
if(($peoplecount >= 71) && ($peoplecount <= 130)){
$price = $price + 950;
$lighting_price = 150;
$heating_price = 250;

if (isset($_POST['lighting'])){
echo "IM TICKEED!!!!!!!";

if (isset($_POST['heating'])){
echo "IM TICKEED!!!!!!!";


echo $eventType . "<br>";
echo $peoplecount . "<br>";
echo $lighting . "<br>";
echo $heating . "<br><br>";
echo $price;


Thanks for any help.


First thing first. Please add value attribute to your checkbox. They will be passed as the value to your url.

Please check whether the checkbox is selected or not. Try putting the checked pseudo selector.


If the checkbox is not checked then it will give you undefined value so please check that condition while assigning value.

 var lighting = ($('#lighting:checked').val() === undefined )? null : $('#lighting:checked').val(); 

The above line won't effect code that much but it's a good to have thing.

While assigning it to your datastring variable you can check whether it is null or undefined or not and append the string like that. If the variable has a value then append it to the datastring variable which you will pass as ajax url.

Hope this will solve your problem.