0

I'm trying to track page referrals, so I might have a URL like:

www.example.com/?ref=google

I've successfully captured that referral as a variable like so:

if ($_GET['ref'] !== "") {
    $ref = $_GET['ref'];
}
else {
    $ref = "";
}

How can I pass it through my jQuery POST function?

$.post("signup.php", {email: email, ref: /* what goes here */}, function(data){
                $("#validation-message").text(data).addClass("error-text");
            });

I've read about json_encode() but is that just for arrays?

9
  • 4
    You already have access to that value in PHP through $_GET. Why would you need JS to send it to PHP too? Commented Jan 17, 2018 at 13:17
  • echo out the value Commented Jan 17, 2018 at 13:17
  • whatever your php script writes to the document is received by jquery response Commented Jan 17, 2018 at 13:18
  • @RoryMcCrossan because I'm using jQuery to avoid a page refresh when submitting the form. Is there a more elegant way of doing this? Commented Jan 17, 2018 at 13:18
  • Put the $_GET['ref'] in a session. Commented Jan 17, 2018 at 13:19

2 Answers 2

4

You can echo the variable to pass it to js.

var ref = '<?php echo $ref; ?>'; /*Assigning the php value to js*/

$.post("signup.php", {email: email, ref: ref }, function(data){
       $("#validation-message").text(data).addClass("error-text");
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can echo out the php value into an input hidden textbox value, and call it out via jQuery or assign it to a js value, there are so many ways to do it. It all depends on how and what you want to achieve

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.