-1

How can I use some PHP variables from the ajax-send.php to the index.php file? I use AJAX as shown below. Do I have to replace AJAX with something else?

index.php

$.ajax({
                    type: 'POST',
                    url: 'ajax-send.php',
                    data: { one: hash },
                    success: function(data) {


                    }
                    });

ajax-send.php

$token = $_POST['one'];
echo "ok"
$toINDEX = "use this in index.php"
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • Then `echo` the value of the variable and assign it to a JavaScript variable. You can't pass a variable from server-side to client-side; you can only pass its value. – afaolek Aug 07 '14 at 11:33

4 Answers4

0

Try this

Ajax

$.ajax({
    type: 'POST',
    url: 'ajax-send.php',
    data: { one: hash },
    success: function(data) {
       var response = data;
       //alert(data);To see what you have received from the server
    }
});

PHP

if(isset($_POST['one'])){
   $token = $_POST['one'];
   echo "ok";
   $toINDEX = "use this in index.php";
   die();
}
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
0

In PHP just echo variable or json_encode array. In JS do the following:

var result = $.ajax({
  url: this.fileUrl,
  type: "POST",
  data: data,
  async: false,
  dataType: 'json'
}).responseText;

Your vaiable is fully accessable.

m1k1o
  • 2,344
  • 16
  • 27
  • Nah! `responseText` will propably be null / empty or undefined. The reason is that `$.ajax` will return an `XMLHttpRequest` object upon call. Howver, the `responseText` won't be filled with the server response until ajax request is fully processed and adding `async` property will freeze the browser temporarily (consider a bit long request) – php-dev Aug 07 '14 at 11:41
0

take the variables in php sessions

    //On page 1(ajax-send.php)
    session_start();
    $_SESSION['token'] = $_POST['one'];

    //On page 2(index.php)
    session_start();
    $var_value = $_SESSION['token'];
mandar
  • 98
  • 6
  • hm this is interesting, I should give it a try – EnexoOnoma Aug 07 '14 at 11:52
  • This won't work because the session gets created only **after** the ajax call is complete. So in order for `index.php` to be able to access that session variable, the `index.php` page will need to be refreshed **after the ajax call is complete** – asprin Aug 07 '14 at 12:00
0

You can simply echo the variable and then access it via javascript inside the success function.

But a better approach would be to json_encode the data. The beauty of this is that it will help you to pass multiple values/variables in a single echo. So

PHP

.
..
if(<all is okay>)
{
   $toINDEX = "use this in index.php"
   $data['result'] = 'ok';
   $data['msg'] = $toINDEX;
   $data['some_other_value'] = 'blah blah';
   // notice how I'm able to pass three values using this approach
}
else
{
   $data['result'] = 'notok';
}
echo json_encode($data);

Javascript

$.ajax({
    type: 'POST',
    url: 'ajax-send.php',
    data: { one: hash },
    dataType:'json',
    success: function(data) {
        if(data.result == 'ok')
        {
            console.log(data.msg);
            console.log(data.some_other_value);
        }
        else
        {
            // something went wrong
        }
    }
});

The important thing to note here is dataType:'json' which tells the function to expect the returned data in json format.

EDIT:

As per you comment, you could do this

$toINDEX = "use this in index.php";
// now use the variable here itself
mysql_query("SELECT * FROM table WHERE column = '$toINDEX'");
.
.
if(<all is okay>)
{
   $data['result'] = 'ok';
   $data['msg'] = 'anything you would like to show the user';
   $data['some_other_value'] = 'blah blah';
   // notice how I'm able to pass three values using this approach
}
else
{
   $data['result'] = 'notok';
}
echo json_encode($data);
Community
  • 1
  • 1
asprin
  • 9,579
  • 12
  • 66
  • 119
  • `dataType: 'json',` an not `dataType:json,`. Otherwise, an error saying undefined variable json ... will be thrown – php-dev Aug 07 '14 at 11:43
  • Yes, I realized that after posting. Thanks for bringing that up – asprin Aug 07 '14 at 11:44
  • After the json is sent back to the javascript (where is in the index.php file) how can I make use of the variables result, msg using PHP? For example, i want to use them in a mysql_query – EnexoOnoma Aug 07 '14 at 11:50
  • Well, you can't. Because PHP runs on server side while Javascript on the client. What you could do, however, is use `mysql_query` inside `ajax-send.php` itself and return back the result to the index page – asprin Aug 07 '14 at 11:56