#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Php form not receiving checkbox data


    hello,
    i am working on a form. All information is received correctly except for the 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";?>:
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,464
    Rep Power
    594
    Welcome to the forum. There are a few problems with your question that make it difficult to understand what you are asking. First you need to enclose your PHP code in [ PHP ] tags and other code in [ CODE ] tags. See ManiacDan's New User Guide to learn how to post a question that will result in useful help along with many good debugging tips. Second, a minor point, Java != Javascript. Third and more directly related to what you are asking, I see no reference to your checkboxes in the PHP code so it is not clear if this is a javascript question or a PHP question.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    In jQuery, calling .val() on a checkbox doesn't take into account whether or not the checkbox is checked.

    To do that, you need to use something like this:
    Code:
    $('#id').is(':checked');
    Which will evaluate to true or false depending on whether the checkbox is checked. If it is checked, you can then retrieve and include its value if you wish.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo