Jump to content

AJAX and PHP data persistence


Recommended Posts

Hi, just a question in regard to how PHP works over ajax requests. I haven't had a chance to test it; on top of that, the scenario I'm presenting probably isn't the most efficient, but bare with me.

Lets assume there is the wrapper page: main.php, and inside, the ajax request is made, and a request.php is summoned via that request.

Now lets say main.php contains this variable declaration at the top of the document:

[code]$variable = 'test';[/code]

Now, my assumption is that main.php has been interpreted and 'finalized' by the time the ajax request is made, thus request.php will not have access to $variable.

Summarized question: Does request.php have access to $variable while it's being interpreted? Or has main.php been 'finalized' beforehand?

(sorry if this has been asked before, but I really wouldn't know what to search for; and what i did try, didn't bring up the results I was looking for... And yes, I'm sure $variable could simply be POSTed or put into the querystring on the ajax request, but rather, I'm just exploring the odds and ends of using php with ajax)
Link to comment
https://forums.phpfreaks.com/topic/30114-ajax-and-php-data-persistence/
Share on other sites

No.... apparently you don't understand server/client association and how AJAX works.

The PHP is on the server side.  Javascript is on the client side.  Javascript makes calls to a script on the server and it executes and spits back some response.

When you execute an AJAX call, that is done with JS, so all the processing on that page with PHP has completed.  Any AJAX call returns a response from the server, but by the time it gets back to the client, all PHP has been processed.

Does that make sense?  It seems like you're unclear on what is actually happening here.
I understand the difference between serverside/clientside; I've got quite a background in both, but I've only got about a year with PHP. And it's a bit different to the C#/Coldfusion/ASP scenes.

What I was asking is if there was any way for the php scripts on the calling page and the called page to interact (without use of session variables), and without needing to POST or GET the variables to the called page via the ajax request.

More specifically--I wanted to know if there was any way (WITHOUT using cookies, the session, POST or GET etc.) to directly get the called page to use a variable present in the calling page.

Like so:

[code]
<? php
        $test = 'value';
?>
< script language="javascript" >
        // assume that setting up the xmlhttprequest object has already been done
ajaxRequest.open( "GET", "requestedPage.php", true );
</ script >
[/code]

And then, requestedPage.php would be able to make use of $test.

But, based on simply how the nature of serverside languages work, I figured it most likely couldn't. But I wanted confirmation.

[quote author=ober link=topic=118058.msg482184#msg482184 date=1165772805]
...but by the time it gets back to the client, all PHP has been processed.
[/quote]

This was confirmation enough.

Sorry for the trouble and thanks for your time.
I still think you're a little unclear on how things really work or when you use AJAX.  And I'm not trying to talk down to you or anything, I'm just trying to make you understand.

Just keep in mind that you're not touching the "backend" or "called page" until after the initial document is loaded.  Even if you did something with the "called page" on the initial loading of the page, that variable would cease to exist by the time you make the AJAX call later.
What you need to do is set javascript variables on your main page.

[code]
<script language='javascript'>
var foo = '<?=$foo_val ?>';
function bar() {
  ...do ajax call
  ...change foo based on results of ajax call
}
[/code]

So in one of my pages, I set the userID in javascript using my PHP variable. Then I have 8 divs that ajax can write to. I need to keep track of what is in each div so as the function is getting the content for a div, it is also saving info about what is in that div.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.