通过Ajax和php复选框信息

时间:2013-09-25 02:32:58

标签: php jquery ajax

我正在尝试使用表单中的信息发送电子邮件,事情是我没有正确执行复选框的部分(没有它完美地工作)。我这样做:

HTML:

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

Jquery(在就绪函数之后):

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>';

事情就是在发送表单时,它将其作为POST而不是GET,因此,它将转到.php页面。收到的电子邮件没有复选框的正确信息,如果选中则只有“打开”,如果未选中则没有。希望能够清楚这一点。提前谢谢

1 个答案:

答案 0 :(得分:1)

之前:

var rimg = 'Images are NOT requested';

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

后:

var rimg = 'Images are NOT requested';

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