user2580108
user2580108

Reputation:

Check box information throught Ajax and php

I am trying to send an e-mail with the information in a form, the thing is I am not doing correctly the part of the checkbox (without it works perfectly). I am doing this:

HTML:

<label for="rimg">Request images</label>//display:none; for this
<span>Request Images</span><input type="checkbox" name="rimg" id="rimg">

Jquery (after the ready function):

var rimg = 'Images are NOT requested';

if ($('#rimg').val()) {
            rimg  = 'images ARE requested'; //changes what is inside rimg if checked
        }

Ajax:

//organize the data properly
        var data = 'name=' + name.val() + '&email=' + email.val() + '&phone='
        + phone.val() + '&rimg=' + rimg.val() + '&message=' + encodeURIComponent(message.val());


        //start the ajax
        $.ajax({
            //this is the php file that processes the data and send mail
            url: "process.php", 

            //GET method is used
            type: "GET",

            //pass the data         
            data: data,     

            //Do not cache the page
            cache: false,

            //success
            success: function (html) {  
                //things in here
            }

PHP:

 //Retrieve form data. 
    //GET - user submitted data using AJAX
    //POST - in case user does not support javascript, we'll use POST instead
    $rimg = ($_GET['rimg']) ?$_GET['rimg'] : $_POST['rimg'];


    $message = '
        //things in here
        <tr><td colspan="2">' . $rimg . '</td></tr>
        </table>
        </body>
        </html>';

The thing is when the form is sent, it gets it as a POST and not as GET, so, it goes to the .php page. The email that is received hasn't the proper information of the checkbox only an 'on' if it is checked or nothing if is not checked. Hope to be clear with this. Thank you in advance

Upvotes: 1

Views: 380

Answers (1)

Undefined Behavior
Undefined Behavior

Reputation: 2208

before:

var rimg = 'Images are NOT requested';

if($('#rimg').val()) {
    rimg  = 'images ARE requested'; //changes what is inside rimg if checked
}

after:

var rimg = 'Images are NOT requested';

if($('#rimg').is(':checked')) {
    rimg  = 'images ARE requested'; //changes what is inside rimg if checked
}

Upvotes: 1

Related Questions