PDA

View Full Version : checkbox inserting into table whether checked or not



Jenny Dithe
Mar 2nd, 2011, 05:39 PM
Hi,

I am using the below checkbox code and whether the checkbox is ticked or not it inserts into my table.

My code is:


<label class="two">&nbsp;</label>
<input type="checkbox" name="creditcard" id="creditcard" value="creditcard">Credit Cards Accepted<br />

I have also tried:


<label class="two">&nbsp;</label>
<input type="checkbox" name="creditcard" id="creditcard" value="" onclick="value='creditcard'">Credit Cards Accepted<br />


Neither works, and I can't work out why?

abduraooft
Mar 3rd, 2011, 08:29 AM
and I can't work out why?
How can we help here even without your relevant code?

Jenny Dithe
Mar 3rd, 2011, 06:37 PM
Ok, sorry I assumed the problem was at that end.

My complete code is:




<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){

$('#placeoverview, #submiteat').live('click',function(){

var btn = $(this).attr('id');
var mailfile, Form;
if(btn == 'placeoverview'){
mailfile = 'insertPlaceOverviewComment.php';
Form = '#AddPlaceOverview';
} else if(btn == 'submiteat'){
mailfile = 'insertPlacesEatMembers.php';
Form = '#placeeat';
}
var data = $(Form).serialize();
$.post (mailfile, data, function(){
alert("Thank you for your input");
$(Form).each (function(){
this.reset();
});
});
return false;
});
});


</script>
</head>
<body>

<form action="" name="placeeat" id="placeeat" autocomplete="OFF">



<label class="two">Days Open: </label><br />
<label class="two">&nbsp;</label>
<input type="checkbox" name="monday" id="monday" value="M">M &nbsp;
<input type="checkbox" name="tuesday" id="tuesday" value="T">T &nbsp;
<input type="checkbox" name="wednesday" id="wednesday" value="W">W &nbsp;
<input type="checkbox" name="thursday" id="thursday" value="Th">Th &nbsp;
<input type="checkbox" name="friday" id="friday" value="F">F &nbsp;
<input type="checkbox" name="saturday" id="saturday" value="Sa">Sa &nbsp;
<input type="checkbox" name="sunday" id="sunday" value="Su">Su &nbsp;
<br />
<br />
<label class="two">&nbsp;</label><input type="checkbox" name="creditcard" id="creditcard" value="" onclick="value='creditcard'">Credit Cards Accepted<br />
<br />
<label class="two">&nbsp;</label><input type="checkbox" name="alcohol" id="alcohol" value="" onclick="value='alcohol'">Alcohol Available

<input type="button" value="Submit" name="submiteat" id="submiteat"><br />
</form>


And my 'insertPlacesEatMembers.php' is:


<?php
include("dbconnect.php");

//validating and sanitizing

if($_POST['name'] !='Enter a Restaurant'){
$sql="INSERT INTO rest(monday, tuesday, wednesday, thursday, friday, saturday, sunday, creditcard, alcohol, date)
VALUES
('{$_POST['monday']}','{$_POST['tuesday']}','{$_POST['wednesday']}','{$_POST['thursday']}','{$_POST['friday']}','{$_POST['saturday']}','{$_POST['sunday']}','{$_POST['creditcard']}','{$_POST['alcohol']}', NOW())";

mysql_query($sql,$con) or die(mysql_error());
}
?>


I have tried to take out the irrelevant information from a much larger page.

What really confuses me is that my days of the week checkboxes work. I have tried the credit card/alcohol checkbox in both the ways I mentioned in my first post, yet checked or not they submit the value.

abduraooft
Mar 4th, 2011, 10:08 AM
Try
<form action="" method="post" name="placeeat" id="placeeat" autocomplete="OFF">

<input type="checkbox" name="alcohol" id="alcohol" value="alcohol" >

Jenny Dithe
Mar 12th, 2011, 10:12 PM
Thank you. Once I changed this I realised the problem must be how I was pulling the information from the data (single = instead of ==)