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

?>
codebro
  • 11
  • 6
  • 1
    Does the `alert()` not show the expected result? What *does* it show? – David Dec 06 '16 at 23:36
  • yes it does. But I need the php var not js var. – codebro Dec 06 '16 at 23:42
  • 1
    that `` in index.php will always run before your ajax call. It runs on your server before the page is sent to user. – leoap Dec 06 '16 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? – David Dec 06 '16 at 23:43
  • In the second to last line of index.php there is an echo for $name. I want to know how to get $name to translate from get.php to index – codebro Dec 06 '16 at 23:46
  • I think the problem is that he is trying to print variable name : He wants to print there, i think so – migueref Dec 06 '16 at 23:46
  • You can't echo because you have the variable name only if you send a request first – migueref Dec 06 '16 at 23:47
  • also I get that php is server side and js is client I am just trying to interface them together. – codebro Dec 06 '16 at 23:48
  • Possible duplicate of: http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – David Dec 06 '16 at 23:48
  • No, you are not understanding. Your variable $name only exists if you send a js request, if you try to print as you did, that variable is not exist – migueref Dec 06 '16 at 23:49
  • also i just had an idea would I be able to run js get.php and have it show up in index.php ex. could i call the ruturn for the getphpvar() fun. IN get.php and have it return it in index.php – codebro Dec 06 '16 at 23:50
  • is there any way to get $name from the send request – codebro Dec 06 '16 at 23:51
  • 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. – David Dec 06 '16 at 23:53
  • Sure there is a way, you need to use jquery to send the value of the php var, and put into a div per example. You could read this: http://stackoverflow.com/questions/6506873/change-div-content-using-ajax-php-and-jquery – migueref Dec 06 '16 at 23:54
  • 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 – Julien Zakaib Dec 06 '16 at 23:55
  • @codebro there's no way to set up a php variable in your index.php after an ajax call, its not the way theses technologies were built. ajax is an javascript technology, it can do dynamic requests to a server and get results in javascript format, you can only use javascript functions to handle the returned data from server. – leoap Dec 06 '16 at 23:56

1 Answers1

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.

Farzad Salimi Jazi
  • 760
  • 10
  • 25