Jump to content

Cyburg

New Members
  • Posts

    5
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Male
  • Location
    UK

Cyburg's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi Fry, Thanks for joining the party, and for your suggestion!!! The session variable was to be used as a potential solution to the problem of passing a variable from the JavaScript on a page to some PHP code on the very same page. I have now realised that no matter how I pass the variable, the PHP on the same page will never get to see the updated value I have set, as the PHP has already been processed server side. If I had thought about this more before I start believing that AJAX might provide me with a holy grail solution to this issue, I would have realised that it was never going to work. Cheers, Paul
  2. Hi theonlydrayk, I think I was a little optimistic when I thought that I could solve this problem with AJAX, in fact I have come to the realisation now that it isn't possible to do what I want at all. It doesn't matter how I pass the variable to the server; be it using a session or cookies, or some other method, the PHP code which I intended would access said variable, has already been run. The PHP code which needs the session variable assignment appears later in the same .php file as the JavaScript which initiates the session variable update, and remember, no page refreshes allowed. So, the code I wrote following your suggestion does indeed update the session variable, as required; but unfortunately, that doesn't solved my problem, as it stands at the moment. However, I do intend to use the technique I have learnt here to pass the variable to the server; I am just going to have to rework the page that it is to be included on, so that I can use a page refresh (and bearing that in mind, it will be much simpler just to pass the value on a query string url, and forget the AJAX altogether). Cheers, Paul
  3. Hi again, I decided to try setting a session var first, using a modified version of the code that was suggested by theonlydrayk. I can get the code called by the AJAX function to update a session var, but it would seem that the new session var value only becomes available to the PHP code (in index.php) after a page reload. Here is the code I am using (the 'ajaxrequest.js' file used for this has the same content as the file I was directed to by theonlydrayk) FILE: index.php <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" /> <script type="text/javascript" language="javascript" src="ajaxrequest.js"></script> <title>AJAX Test</title> </head> <body> <script type="text/javascript" language="javascript"> MyAjaxRequest('main','updatesessionvar.php?tempdistance=255'); </script> <!-- we are not using this next output div --> <div id="main" style="display:none;"></div> <!-- display the session var passed from JavaScript --> <p>DISTANCE: <?php echo $_SESSION['tempdistance']; ?></p> </body> </html> FILE: updatesessionvar.php (this is my modified 'hello.php') <?php session_start(); ?> <?php header("Content-Type: text/html; charset=ISO-8859-15"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); // do we have a "tempdistance" session var to update? if(!empty($_GET['tempdistance'])) { // update the "tempdistance" session var with passed value $_SESSION['tempdistance'] = $_GET['tempdistance']; } ?> I understand why I am not seeing the updated var; the PHP is being processed by the server prior to the JavaScript running in the clients browser; so back to my original question: is it actually possible to pass my JavaScript variable for use in my PHP code without a page reload? Cheers, Paul
  4. Hi Jackpf and theonlydrayk, Thank you very much for your replies. Jackpf, I had thought about using cookies, but the code in question is a component of a shopping cart, and we wanted to try and avoid using cookies if possible to accommodate those users who might have them disabled (the value I need to pass is essential for the correct calculation of the visitors delivery charges). It may be that I am forced to go down that road if another solution cannot be found; I would just have to ensure that the cart's functionality degrades gracefully when a browser with cookies disabled is encountered. theonlydrayk; I will take a good look at the page you provided an URL to, and see if I can apply the ideas presented there to the task I am facing. I think the biggest issue I am having at the moment is just getting my head around the way AJAX calls are processed; I'm sure I'll get there in the end. I will let you both know which option I ran with, and how I got on later today; thanks both again for your replies. Cheers, Paul
  5. Hi all, I am new to AJAX, and am having difficulty working out if what I want to achieve is even possible; I would be most grateful if someone could give me some pointers, so that I can stop wasting my time thrashing around with something I am struggling to understand as yet.... ....I have some JavaScript on a page which generates a variable that I need access to within the server-run PHP code for the same page; my idea is to update a session variable on the server with the value I need to pass. From the time that the variable is created by the JavaScript and the time that I need to access the variable using PHP, I cannot perform any page refresh; the JavaScript which creates the variable and the PHP that needs to subsequently use that variable are both part of the same page. Any suggestions will be gratefully received. Cheers, Paul
×
×
  • 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.