0

I want to do an ajax call in a php file. (the final goal is to get a localStorage). If I have well understood i begin with a start session in the php.

<?php
session_start();
?>

Then, ajax call of another php file.

<script>
    src="jquery.js";
    var variableToSend = 'foo';
    $.ajax({
        type: "post",
        async : false,
        url: "/login_getStorage.php",
        data: { variable: variableToSend }
    });
</script>

In this file php login_getStorage.php I have only

<?php
$_SESSION['toto'] = "titi";

If I have well understood, the variable $_SESSION['toto'] should be initialized everywere with "titi"

So in the main file :(so after script)

<?php
echo($_SESSION['toto'] );

I thought it will display 'titi'. But I have the error PHP Notice: Undefined index: toto

It seems that my ajax call is not done

How can we do to do this ajax call ?

Best regards.

CD001
  • 8,332
  • 3
  • 24
  • 28
C Morell
  • 81
  • 1
  • 3
  • 10
  • I *think* the problem is probably that the first PHP script is done, dusted and finished before the Ajax call takes place - so if the session variable is set in the script called over Ajax you'll not see it in the first script until you reload the page. [This QA](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) might help explain the situation better. – CD001 Dec 17 '19 at 10:46
  • So how can I do the ajax call withour dusting the php ? – C Morell Dec 17 '19 at 10:49
  • set async : true in ajax and try again ! – BhAvik Gajjar Dec 17 '19 at 10:58
  • What you'd generally do in the script you're calling over Ajax is echo out a JSON representing the state change, which you use in the `success()` callback to do what you need to do (set what you want in `localStorage`); you can't *go back* to PHP at that point as it runs on the server not the client. – CD001 Dec 17 '19 at 11:01
  • @BhAvikGajjar : Thanks but it does not work. – C Morell Dec 17 '19 at 11:07
  • @CD001 : So what can we do ? Can you detail the solution ? – C Morell Dec 17 '19 at 11:08
  • 1
    It depends on what *exactly* you're trying to do - [this QA](https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) should cover the specifics of using Ajax/jQuery. As an aside though, you realise there's a typo in your code here? ` – CD001 Dec 17 '19 at 11:20
  • I correct the typo, but it does not change anything. – C Morell Dec 17 '19 at 11:28
  • To answer better what I want to do : In the beginning of php (session_start), I have something in the local storage. So I want to retreive this. So as it is impossible to access to localStorage inside php I try to do an ajax call. – C Morell Dec 17 '19 at 11:31
  • 1
    What do you want PHP to do with that data from `localStorage` ? I've got a feeling you're sort of approaching the problem backwards - but I could be wrong. – CD001 Dec 17 '19 at 11:41

2 Answers2

2

In this file php login_getStorage.php I have only

<?php
$_SESSION['toto'] = "titi";

You need to call session_start() before you can use $_SESSION['toto'] in any PHP file.

Your Ajax call is probably working just fine, but since you are editing the contents of a session that doesn't exist, it isn't having the effect you want.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

PHP is run on the server BEFORE any of the javascript is parsed.

There are a few questions:

  • Did you include the jquery library?
  • Are there any JS errors?

Try to add the success to your ajax:

success: function(data){ 
    // when success
}

Your PHP is run before your ajax call so the value doesn't exist on page load.
Instead of assigning a value on the PHP page you can also return a value.

<script>
    src="jquery.js";
    var variableToSend = 'foo';
    $.ajax({
        type: "post",
        async : false,
        url: "/login_getStorage.php",
        data: { variable: variableToSend },
        success: function(data){ 
           // when success display data 
           alert(data); // alert the session value
        }
    });
</script>

And then your login_getStorage.php:

<?php
session_start();
$_SESSION['toto'] = "titi";
echo $_SESSION['toto'];
Webdeveloper_Jelle
  • 2,868
  • 4
  • 29
  • 55