- This topic is empty.
-
AuthorPosts
-
March 28, 2014 at 9:48 am #167012Josh JohnsonParticipant
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?
March 28, 2014 at 12:33 pm #167025__ParticipantI 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 beinclude
d, 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.
March 29, 2014 at 3:56 am #167076Josh JohnsonParticipant@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?
March 29, 2014 at 12:39 pm #167098March 30, 2014 at 3:05 am #167127Josh JohnsonParticipantBut where would I actually do the unsetting? Is it possible on page exit or would it have to be on the proceeding page?
March 30, 2014 at 11:51 am #167141__ParticipantIs 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. : )
March 30, 2014 at 8:35 pm #167165tamhaParticipantSession variables exist on browsers if you don’t clear cache.
March 30, 2014 at 9:48 pm #167174__ParticipantSession 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.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.