0

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 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

Hi Darin thanks for your answer, I've tried this but I still can't get it working (the alert function isn't displayed so it must be breaking before then). I've edited my question with what I have in the other file as well, could you see if I'm doing something wrong please?
@Daniel H, try analyzing with FireBug what AJAX request is sent to the server and the returned response. Also make sure there are no javascript errors. From what I can see you are subscribing to the click event of some button but you are not returning false from the .click callback at the end meaning that if #tellafriendsubmit is an anchor or a form submit button your AJAX will never have time to execute as the page will redirect. Also if the $post_name PHP variable contains some characters such as ' they might break your javascript.
Hi Darin, I use firebug to look at HTML/CSS but I don't know how to see AJAX calls using it. Where abouts do I look? Thanks
@Daniel H, it's the Console tab. It is also where your javascript errors will be shown.
Thanks Darin, I'll use this alot from now on! :)
|
1

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); } });

Comments

0

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

1 Comment

Hi Zanathel, sorry I should have mentioned in my question I've just put the value name in there to test it before I add in the javascript variable name, I've tried that too and it didn't work
0

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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.