form not sending checkbox data

General discussions related to php

Moderators: macek, egami, gesf

dirtmcgirt
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Oct 23, 2012 11:55 am

form not sending checkbox data

Postby dirtmcgirt » Thu Nov 01, 2012 5:35 am

hello,
i am working on this form, still having problems. I'm not sure how to send the data from checkboxes.
I've seen lots of examples online, but i can't get mine to work! I believe the problem is in the javascript but i just can't figure it out. Thanks for your help.

Here is a bit of my HTML:

<form id="contactForm" name="listForm" action="#" method="post">
<fieldset>
<input type="checkbox" id="A12 SS MER 19H30 CAS" name="choice2" value="260" onchange="checkTotal()"class="form-poshytip" title="Cochez ici" />
<label>Mercredi 19h30 à 22h, niveaux 1,2,3</label>
<br/>
<input type="checkbox" id="A12 SS MER 19H30 CAS" name="choice" value="260" onchange="checkTotal()"class="form-poshytip" title="Cochez ici" />
<label>Mercredi 19h30 à 22h, niveaux 1,2,3</label> <br/>

<input name="nom" id="nom" type="text" class="form-poshytip" title="Entrez votre nom" />
<label>Nom</label>


Java:

$(document).ready(function(){
// on submit...
$("#contactForm #submit").click(function() {
$("#error").hide();

var nom = $("input#nom").val();
var choice = $("input#choice").val();
var choice2 = $("input#choice2").val();
var sendMailUrl = $("#sendMailUrl").val();

//to, from & subject
var to = $("#to").val();
var from = $("#from").val();
var subject = $("#subject").val();

// data string
var dataString = 'nom='+ nom
+ '&choice2=' + choice2
+ '&choice=' + choice
+ '&to=' + to
+ '&from=' + from
+ '&subject=' + subject;
// ajax
$.ajax({
type:"POST",
url: sendMailUrl,
data: dataString,
});
});


PHP:
<?php

$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );

$from = $_POST['email'];

$msg = "NOM: ".$_POST['prenom']."<br> "
.$_POST['choice2]." <br>"
.$_POST['choice'];

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;

foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
<?php echo "Xy6Hy";?>:

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: form not sending checkbox data

Postby seandisanti » Fri Nov 02, 2012 2:49 pm

Checkboxes are funny, if they're checked, they're transmitted, if they're not, they don't transfer at all. so you can use

Code: Select all

isset($_POST['yourCheckboxNameHereInsteadOfThisRidiculouslyLongString'])

to see if the checkbox was checked.


Return to “PHP General”

Who is online

Users browsing this forum: No registered users and 4 guests