dsfsdfsdfsdf6455 2011-07-17 09:23
浏览 28
已采纳

Jquery AJAX和2个以上变量的问题

I am trying to add a tell a friend section to a website I am making but I am having difficulty trying to send more than 2 variables through a URL with AJAX. This is the code I have at the moment:

    jQuery("#tellafriendsubmit").click(function() {

        var email = jQuery('#tellafriendemail').val();
        var name = jQuery('#tellafriendname').val();

            jQuery.ajax({
          type: "POST",
      url: "http://www.example.co.uk/tell-a-friend-processor-page/?postname=<?php echo $post_name; ?>&name=name&email="+email,

      success: function(msg){ 
            alert('Your tell a friend recommendation has been sent. Thank you for recommending us.');

      }
    });

});

If I remove the '&name=name' part so that I'm only sending the postname and email address, it works fine but I need to send the name as well so I can write 'Dear $name....'

How can I send the extra 3rd variable? Thanks for any help

Edit:

The part I'm using in the tellafriendprocessor page looks like this:

$email = $_POST['email']; $post_name = $_GET['postname']; $name = $_POST['name'];

             $to = $email;
             $subject = "Example - Tell a friend";
             $body = "Dear $name http://www.example.co.uk/ads/$post_name";

             if (mail($to, $subject, $body, $headers)) {

              } else {

              }
  • 写回答

4条回答 默认 最新

  • douqing5981 2011-07-17 09:26
    关注

    You could try sending them as part of the POST request body (using the data property) which will ensure that those values are properly encoded:

    var email = jQuery('#tellafriendemail').val();
    var name = jQuery('#tellafriendname').val();
    jQuery.ajax({
        type: 'POST',
        url: 'http://www.example.co.uk/tell-a-friend-processor-page/?postname=<?php echo $post_name; ?>',
        data: { name: name, email: email },
        success: function(msg) { 
            alert('Your tell a friend recommendation has been sent. Thank you for recommending us.');
        }
    });
    

    On the server side make sure you are reading them from the $_POST hash ($_POST["name"] and $_POST["email"]).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘