Adding up check boxes, adding the price into a database
I'm trying to add tick boxes and when the boxes are ticked a value adds up (a variable) that variable is then added into the database along with each checkbox once the form is submitted, I've been trying to modifiy this code but cant figure out how to not use the parseInt() function and output a single variable that I can then add into the database/Email reply to the customer. I'm really stuck and would appreciate some help.
(the options are suppose to actually be (deodoriser,carpet,carpetrepair,furniture,tabs,urine but im using demo options for now below in the insert staement they are the correct names) This is my HTML:
<p><input type="checkbox" name="extras[]" value="option1" rel="11">furniture</p>
<p><input type="checkbox" name="extras[]"" value="option2" rel="12">tabs</p>
<p><input type="checkbox" name="extras[]" value="option3" rel="13">urine</p>
<p><input type="checkbox" name="extras[]" value="option4" rel="30">couch</p>
<p><input type="checkbox" name="extras[]" value="option5" rel="20">steam</p>
<span id="output"></span>
This is my javascript function
$(document).ready(function() {
function recalculate() {
var sum = 0;
$("input[type=checkbox]:checked").each(function() {
sum += parseInt($(this).attr("rel"));
});
$("#output").html(sum);
}
$("input[type=checkbox]").change(function() {
recalculate();
});
});
This is my email reply/datbase inserting at the moment
$idextra=$_POST['extras'];
$arr_num=count($idextra);
$i=0;
while ($i < $arr_num)
{
$q="INSERT INTO bs_reservations (dateCreated, name, email, phone, comments,status,eventID, qty,dropoff,deodoriser,carpet,carpetrepair,furniture,tabs,urine) VALUES (NOW(),'".$name."','".$email."','".$phone."','".$comments."','2','".$eventID."','".$qty."','".$dropoff."','{$idextra[1]}','{$idextra[2]}','{$idextra[3]}','{$idextra[4]}','{$idextra[5]}','{$idextra[6]}')";
$res=mysql_query($qu) or die('ERROR INSERTING: '.mysql_error());
开发者_高级运维 $i++;
}
Thanks heaps for any advice/help in coding this. I know its a big question but I feel it will help a lot of people in the future.
$res=mysql_query($qu)
change that to
$res=mysql_query($q)
and probably this too
name="extras[]""
to name="extras[]"
and i think array starts with 0, and the query would be:
while ($i < $arr_num){
$q = "INSERT INTO bs_reservations";
$q .= " (dateCreated, name, email, phone, comments,status,eventID, qty,dropoff,deodoriser,carpet,carpetrepair,furniture,tabs,urine)";
$q .= " VALUES (NOW(),'".$name."','".$email."','".$phone."','".$comments."','2','".$eventID."','".$qty."','".$dropoff;
$q .= "','".$idextra[0]."','".$idextra[1]."','".$idextra[2]."','".$idextra[3]."','".$idextra[4]."','".$idextra[5]."')";
$res=mysql_query($q) or die('ERROR INSERTING: '.mysql_error());
$i++;
}
精彩评论