开发者

Problem with Jquery AJAX and more than 2 variables

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 {

              }


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"]).


you can send your parameters to a page with AJAX by GET and POST method with this piece of code

data: { id : 'id', name : 'name' }, // multiple data sent using ajax

Here is an example

$.ajax({ type:"GET", cache:false, url:"welcome.php", data: { id : 'id', name : 'name' }, // multiple data sent using ajax success: function (html) { $('#add').val('data sent sent'); $('#msg').html(html); } });


You will need to move name outside the string. And to add more key-value-pairs you just append them to the query string: &key=value

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


Try to use object of params like this:

$.post("/tell-a-friend-processor-page/", { name: "John", email: "<?php echo $post_email; ?>", post_name: "<?php echo $post_name; ?>" },
function(data) {
     alert("Data Loaded: " + data);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜