Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Changing a Variable without reloading the page

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Changing a Variable without reloading the page

    Is there any way to change a PHP variable on a page without reloading that page.
    Maybe with javascript?

    Maybe something like:
    HTML Code:
    <a href="#" onClick="change_variable()">Change variable</a>

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    php can only be done on the server side, however you can use javascript (AJAX) to send data to the server and back without reloading the whole page, but just a portion.

    read http://dynamicdrive.com/forums/showthread.php?t=19247

  3. #3
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    I'm already using the 'Ajax Tabs Content script' found here: http://www.dynamicdrive.com/dynamici...axtabscontent/

    Is there any way the I can modify this script to allow me to do what I want.

    So that if someone clicks on one of the tabs it sends information to the server to change a variable on the current page.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The PHP script used with the AJAX content script could be modified to store the requested page in the $_SESSION array, then just use that value from then on.

    $_SESSION is sorta like cookies, but better, and it will be persistent through someone's visit to your site, rather than just existing in one instance of the script running, as with any standard variable in the code.

    OR, you could actually just use cookies.

    You can set (or read) a cookie with javascript, then read (or set*) that cookie with PHP, as $_COOKIE['cookiename'].
    (*set would require use of a function, not just $_COOKIE = value;... set_cookie().)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    The $_SESSION option will be best as i have some experience of this already.
    Plus it will allow me to destroy the session when certain other links are clicked on.

    The only problem is that when i load the page initially, it requests content from the external page rather than having some 'intro text'
    however i wouldn't want to set the session variable until one of the other tabs are clicked on. Is this possible?

    If so would you know where i can get some script to do this? I have very little experience with javascript.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Code:
    <script language="text/javascript">
    <?php echo "var a = 5;"; ?>
    </script>
    PHP is output as text to the browser. Javascript runs from that. You can use PHP to create parts of the JS as well.

    Using the PHP, you can set the initial value, with ease.

    You will want to set the value initially, to make life easier, just have a starting value of the first page you want displayed.

    Have the script check if (isset($_SESSION['myvar'])) { dostuff() ; } and you can have an initial action, like setting that default value, etc.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    That is exactly what i'm trying to change:

    PHP Code:
    <?
    if (isset($_SESSION['myvar'])) 
      { 
      print 
    "var enabletabpersistence=1"
      }
    else
      { 
      print 
    "var enabletabpersistence=0"
      }
    ?>
    But what function actually sets the session variable? and how do I send that function from one of the clicked tabs?

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    $_SESSION['myvar'] = 5;
    echo $_SESSION['myvar']; //5


    echoing text like above will be used in javascript, nothing directly related to sessions. Sessions are secure and entirely PHP, so you will output the values of session variables then you can use those values in javascript.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    I still don't fully understand how I set the session variable by clicking on a tab?

    My thinking was:
    HTML Code:
    <a href="external.php" rel="ajaxcontentarea" onclick="run_some_javascript()">TAB 1</a>
    HTML Code:
    <script type="text/javascript" language="JavaScript">
    function run_some_javascript()
      {
      some code here to set the session variable;
      }
    </script>
    Do you know if this will work? If so, would you know of a script that will do this?

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    No. You cannot access the session from javascript. It is entirely PHP.

    Ajax is a javascript method of REQUESTING a PHP page... it runs PHP.
    WHEN it is run, you will then, in that script being run, use a code to update the variable.

    For example,
    $_SESSION['choice'] = $_SERVER['REQUEST_URI'];


    Remember, the art of Ajax is getting it to SEEM like php is executing client side, but it NEVER does. You must use javascript to get php to set the session, to update a link, etc.

    You could instead use cookies as I described above. These are accessible from both PHP and Javascript without trouble. That's a nice option, though Sessions are a bit less intrusive.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •