0

I tried to find a solution but nothing is working for me, or too complicated.

I'm trying to send a variable from a script in my HTML to my php file.

Here my HTML script

<script type="text/javascript">
    function DetectAndServe() {
        urlp=[];u=location.search.replace("?","").split("&").forEach(function(d){e=d.split("=");urlp[e[0]]=e[1];})
        var redirectlink = "https://www.mywebsite.com/redirect_app?utm_medium=" + urlp.utm_medium + "&utm_source=" + urlp.utm_source + "&utm_campaign=" + urlp.utm_campaign;
    }   
</script>

What's the easiest way to use the redirectlink variable in my PHP file

8
  • AJAX is the only option here. Commented Jan 22, 2019 at 14:18
  • Ok thanks, can you be more specific, how to use it in my case, I'm beginner Commented Jan 22, 2019 at 14:25
  • Please do some research into using AJAX, and then come back here when you have a specific question -- SO is not a drive-thru code generation service. Commented Jan 22, 2019 at 14:26
  • 1
    You can also make a form to post you're URL into the php file Commented Jan 22, 2019 at 14:27
  • do you need to invoke that link so you can capture urlp.utm_medium, urlp.utm_source and urlp.utm_campaign as $_GET['utm_medium'], $_GET['utm_source'] and $_GET['utm_campaign'] respectively? Commented Jan 22, 2019 at 14:31

2 Answers 2

2

You actually need to create a form to send the result of your function into the PHP file.

Example of form with JavaScript:

<form method="post" action="#">
    <input type="text" id="redirectlink" name="redirectlink">
    <button type="submit">Send</button>
</form>
<script>
function DetectAndServe() {
    urlp=[];u=location.search.replace("?","").split("&").forEach(function(d){e=d.split("=");urlp[e[0]]=e[1];})
    var redirectlink = "https://www.mywebsite.com/redirect_app?utm_medium=" + urlp.utm_medium + "&utm_source=" + urlp.utm_source + "&utm_campaign=" + urlp.utm_campaign;
    return redirectlink;
}  
document.getElementById('redirectlink').value = DetectAndServe();
</script>

In your PHP you retreive with:

<?php
if(isset($_POST['redirectlink'])){
    $link = $_POST['redirectlink'];
    echo $link;
}
Sign up to request clarification or add additional context in comments.

4 Comments

DetectAndServe is not returning anything.
Yep, just fixed it
Thank you so much it's working well, I just have an other issue now I'm unable to reset the form with document.frm.reset(); I have the error "document.frm.reset is not a function", I maybe need to post an other question
You need to use it like this document.getElementById("idform").reset();
0

In your html file

<script type="text/javascript">
window.location.href = 'https://www.mywebsite.com/myfile.php?variable1=value1&variable2=value2';
</script>

In your php file you can get the value pass to the url via $_GET request

echo $_GET['variable1'] // print value1
echo $_GET['variable2'] // print value2

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.