-2

I am working on a project that sends a ajax post request to a php file and then sets a php variable and returns the variable in js. That all works fine. What I am struggling with is being able call $name in the index.php file once getPhpVar() function has run.

This is what the program is supposed to do: 1.run getPhpVar() function 2.sends ajax post to get.php 3. get.php set name variable 4. get.php sends name variable back through js 5. I am able to call name variable in php Code: index.php

<script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>     
<script>
function getPhpVar(varname) {
        $.post("get.php",
        {
          name: varname,
        },
        function(data,status){

            alert("Data: " + data + "\nStatus: " + status);
        });
}
getPhpVar(11);
</script>
<?php echo $name;
?>

get.php:

<?php
if( $_REQUEST["name"] ) {
   $name = $_REQUEST['name'];
   echo $name;
}

?>
16
  • 1
    Does the alert() not show the expected result? What does it show? Commented Dec 6, 2016 at 23:36
  • 1
    that <?php echo $name; ?> in index.php will always run before your ajax call. It runs on your server before the page is sent to user. Commented Dec 6, 2016 at 23:42
  • 1
    I'm not sure you understand the difference between server-side code and client-side code. That function which contains the alert is JavaScript. You have the value you got from the server, so that's the value you use. What exactly isn't working here? Commented Dec 6, 2016 at 23:43
  • 1
    @codebro: I don't really know how else to explain this. You should definitely take a look at the very comprehensive answers in the previously linked duplicate question. You are getting the value from $name in response to the AJAX request. You've proven this by alerting the value. If you want to also put that value in a page element, you would do so in that JavaScript function. The answer below demonstrates this. You have everything you need. There is genuinely no problem here, the code is working. Commented Dec 6, 2016 at 23:53
  • 1
    As others point out, you seem confused about server side vs client side. To help you out, here's the flow of your app from what I understand : 1. client browser requests index.php 2. Your server gets the request, and the PHP code executes immediately (that's your echo $name ) 3. After PHP is done, send result to client 4. Client browser executes Javascript code 5. At some point, the Javascript getPhpVar function executes 6. Send AJAX request to server @ get.php 7. PHP executes and sends back $name 8. Javascript callback from client browser gets the response Commented Dec 6, 2016 at 23:55

1 Answer 1

4

You can't do that by echo, you need to do that by javascript like this:

<script>
    function getPhpVar(varname) {
        $.post("get.php",
        {
          name: varname,
        },
        function(data,status){
            document.getElementById("result").innerHTML = data;
            alert("Data: " + data + "\nStatus: " + status);

        });
}
getPhpVar(11);
</script>

And your html file would be like :

<div id="result"></div>

In fact your $name variable is in server side but you want to use it in client side, to use it you need to put it in some var or use use equivalence of echo in javascript which is document.getElementById("result").innerHTML or document.write, Another way is to use $_SESSION variables in php and echo.

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

1 Comment

echo was just to shorten the code I actually need $name

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.