Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Getting PHP variables within appended HTML

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #167012
    Josh Johnson
    Participant

    Okay so I have profile pages which have a “Hire” button. When a user clicks the button, a jQuery $.get request is made and a HTML form is appended to the DOM.

    Screenshot: http://i.imgur.com/XVgUsQj.png

    The plan is to allow a user to input a message into the text area, post the form data to a PHP file which will then send an email to the user with the message and who the message is from.

    Right now I can get the data from the logged in user (using a sessions variable) however, I can’t access PHP variables (such as $developer[’email’]), so cannot send an email. Any ideas on how I would go about achieving this?

    #167025
    __
    Participant

    I can get the data from the logged in user (using a sessions variable) however, I can’t access PHP variables (such as $developer[’email’])…

    To clarify, you’re doing something like this?

    your-web-page.php

    <?php
    $developer = ["email"=>"[email protected]"];
    print "All my HTML and AJAX stuff - [click here to send AJAX email]";
    

    your-ajax-controller.php

    <?php
    $_SESSION["username"] = "itsAllGood";
    $developer["email"] = Notice: undefined variable…
    

    yes?

    Obviously, the php script that handles your ajax request won’t know about anything you don’t tell it about. You would have to pass any needed data between the two scripts intentionally.

    You might do this via the $_SESSION, by putting the needed data in a common config file that can be included, pulling the info from a DB, or a number of other methods.

    Using the session would probably be the easiest way, but could lead to messy code if you’re not very careful. If you plan on doing more than one or two things via AJAX, you should probably spend the time to make a separate, (semi) independent API for your ajax calls.

    Getting PHP variables within appended HTML

    Just in case there is any confusion, there is no such thing as a PHP variable “within” HTML. On the server, that HTML is nothing more than plain text. By the time it is on the browser and parsed as HTML, PHP is long gone.

    #167076
    Josh Johnson
    Participant

    @traq Yes, you are correct!

    If I used a session to store the variable, could it be destroyed on page exit or something similar? I don’t really want to set a global session when the variables are only relevant when a user navigates to a profile page?

    #167098
    __
    Participant

    you can use unset.

    #167127
    Josh Johnson
    Participant

    But where would I actually do the unsetting? Is it possible on page exit or would it have to be on the proceeding page?

    #167141
    __
    Participant

    Is it possible on page exit

    Do you mean, “automatically”? No.

    would it have to be on the proceeding page?

    No. The best place to unset the variable would probably be right after you’re done using it. I don’t know what your code looks like, but, for example:

    page1.php

    <?php
    session_start();
    $_SESSION["myVar"] = "I need this on page 2";
    

    page2.php

    <?php
    session_start();
    $myVar = $_SESSION["myVar"];
    
    // as long as you don't need this on page3, 
    //   you can get rid of it now
    unset( $_SESSION["myVar"] );
    
    print "$myVar …and now I have it!";
    

    I don’t really want to set a global session when the variables are only relevant when a user navigates to a profile page

    BTW, this is a very wise approach. If you find yourself needing to pass more than one or two variables to the session, it can get “crowded” rather quickly. In such cases, it’s best to organize session variables hierarchically.

    For example, if I have session vars that are only needed on page_x, then instead of doing

    <?php
    $_SESSION['var1'] = "one";
    $_SESSION['var2'] = "two";
    

    I’d do

    <?php
    $_SESSION['page_x']['var1'] = "one";
    $_SESSION['page_x']['var2'] = "two";
    

    …and so forth. Same thing with vars that belong to classes, and so forth. Makes things much easier to think about, and much harder to screw up. : )

    #167165
    tamha
    Participant

    Session variables exist on browsers if you don’t clear cache.

    #167174
    __
    Participant

    Session variables exist on browsers if you don’t clear cache.

    No: PHP sessions are saved on the server, (by default) in text files.

    They have nothing to do with caching, and they never exist “on browsers.”

    How long the session file lasts depends on your configuration, but will typically persist around 25 minutes (of inactivity) before being marked as “garbage.” After that, it may (or may not) be deleted during the garbage collection process. The session cookie is usually gone long before that.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.