vendredi 5 mai 2017

Checkbox values are NOT passing to PHP using ajax

This is my HTML CODE(ONLY CheckBOX part) i need to check multiple choices from user and send it to php using ajax.then i need to post a mail to administrator

 <fieldset class="form-group" >
        <label >Preferred method of contact : </label> <br><br>
        <div class="form-check">

            <label class="checkbox-inline">
            <input type="checkbox" name="color" id="color" value="Whatsapp">Whatsapp

            </label>
            <label class="checkbox-inline">

            <input type="checkbox" name="color" id="color" value="Viber">Viber
            </label>
            <label class="checkbox-inline">
                <input type="checkbox" name="color" id="color" value="E-Mail">E-Mail
            </label>




        </div>
        </fieldset>

AND this is Ajax Part to SEND values to php. in my contact us form NAME PHONENO ADDRESS and drop-downs are working fine with ajax and php but only this check box part not working .

   <script>
    function _(id) {
        return document.getElementById(id);
    }

    function submitForm() {
        _("mybtn").disabled = true;
        _("status").innerHTML = 'please wait ...';
        var formdata = new FormData();

        formdata.append("n", _("n").value);
        formdata.append("e", _("e").value);
        formdata.append("m", _("m").value);
        formdata.append("no", _("no").value);
        formdata.append("exampleSelect1", _("exampleSelect1").value);
        formdata.append("exampleSelect2", _("exampleSelect2").value);
        formdata.append("exampleSelect3", _("exampleSelect3").value);

        formdata.append("color", _("color").value);





        var ajax = new XMLHttpRequest();
        ajax.open("POST", "example_parser.php");
        ajax.onreadystatechange = function () {
            if (ajax.readyState == 4 && ajax.status == 200) {
                if (ajax.responseText == "success") {
                    _("my_form").innerHTML = '<h2>Thanks ' + _("n").value + ', your message has been sent.</h2>';
                } else {
                    _("status").innerHTML = ajax.responseText;
                    _("mybtn").disabled = false;
                }
            }
        }
        ajax.send(formdata);
    }
</script>

And this is my PHP code From php page. i need to pass all checkbox result to this php part and declarer to a variable then i can use it for mailing .

if( isset($_POST['n']) && isset($_POST['e']) && isset($_POST['m']) ){
$n = $_POST['n']; // HINT: use preg_replace() to filter the data
$e = $_POST['e'];
$no = $_POST['no'];
$m = nl2br($_POST['m']);
$pkg = $_POST['exampleSelect1'];
$cnty = $_POST['exampleSelect2'];
$how = $_POST['exampleSelect3'];

$var = $_POST['color'];




Aucun commentaire:

Enregistrer un commentaire