【问题标题】:Check box information throught Ajax and php通过 Ajax 和 php 获取复选框信息
【发布时间】:2013-09-25 10:44:31
【问题描述】:

我正在尝试以表格形式发送包含信息的电子邮件,问题是我没有正确执行复选框的部分(没有它可以正常工作)。我正在这样做:

HTML:

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

jquery(在ready函数之后):

var rimg = 'Images are NOT requested';

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

阿贾克斯:

//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 页面。收到的电子邮件没有复选框的正确信息,如果选中,则只有“打开”,如果未选中,则没有。希望清楚这一点。提前谢谢你

【问题讨论】:

  • 您可以在您的名称值对中使用 $("#MyFormID").serialize() 而不是自己进行名称值对构造,您还没有将 rimg 值添加到其余值中,这就是为什么它不存在于您的php 脚本。
  • 检查是否正确:data: data,
  • 试试这个:data: {data: data},
  • @bruno.karklis 他不需要将数据包装到对象中
  • @PatrickEvans 抱歉,我现在编辑了它,我已将 val 添加到其余值中,只是在这里输入了错误的名称

标签: php jquery ajax


【解决方案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
}

【讨论】:

  • 这行得通,但是 php 部分没有得到 rimg 的值。如果它被选中,它仍然会“打开”,如果没有被选中,它仍然没有。 Tho,我也通过 post 方法转到其他页面,而不是使用 ajax get。
  • 要使用来自 js var ring 的值,只需将 ring 替换为 ring.val()
  • 完美!就是这样!非常感谢
猜你喜欢
  • 2021-06-28
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 2012-01-07
  • 2021-10-19
相关资源
最近更新 更多