User sessions with jquery and Ajax - php

I am using jquery to set a session, i have a php page which gets the values of the person logging. The value in the session array, is then used in another page where, it is stored in a hidden field for database entry.The problem is, the value is not set unless you refresh the page of which beats the purpose of AJAX and Jquery.Again,the session seems to be one session behind.How can I do this without page refresh/ reload?

It sounds like you are doing this...
1) User types some stuff in
2) AJAX requests fires that stuff off to the server
And it sounds like you need to add:
3) Using the result of step 2, set the value on the current page

ok, so if I get this straight, you use ajax to let a user log in on one page, and on another page you want some magic value from the session to be filled in? Is this other page already open at the point the user logs in? e.g:
user opens two windows, one to "loginpage.php" and the other to "formpage.php"
user logs in, data sent to server via ajax, magic value created in session
user switches to "formpage.php" window, fills in the form there, and submits
server expects magic value to be returned in hidden field, but it's not there
something like that? If that's the case, there's no way for the "formpage" window to know that you've logged in via the other window. The server can't 'push' a login notification to it, and generally javascript in one window can't affect the contents of another window, unless that window was created by the first window to begin with.
You could have the "Formpage" window poll the server to see if the user's logged in via another window, and then request this magic value and dynamically fill in the hidden field. But otherwise you'd have to refresh the page to get that hidden form field filled in.

Related

Is there a way to have a pop-up generate on the home page once you've been redirected from a form submission on another page?

I have a logging application. You click the "Log" button, it takes you to another page with all of the forms etc. After I click "Submit" on this page, it submits to my database and redirects back to the home page. What I want to happen is when I click "Submit", it redirects to home page, and then has a pop-up that says something like "Thanks for the submission!". I've looked through the forums but can't really find anything specific to this application. Thanks.
There are a ton of ways to do this. The general idea is
The home page must contain code that displays the "popup," but only under certain conditions (e.g. a flag is passed somehow). Normal access to the home page should not trigger the condition.
The DB submission page must trigger the aforementioned condition when redirecting to the home page.
The "condition" could be as simple as setting a variable in the querystring, e.g. http://domain.com/home.php?showConfirmation=true. The problem with using the querystring is that the user could bookmark it and see the confirmation every time he uses the bookmark.
Another way to set the condition is to set a cookie, session variable, or pass the data via form/post. All of these have advantages and disadvantages.
You could also do it some fancy way, e.g. perhaps your site needs the general capability of displaying one-time messages. If the user is registered and authenticated, you'd be able to look up a DB table to see what messages are left to display to the user, and the home page would be coded to automatically display them (marking the DB record so that it doesn't get displayed again). The DB submission page would add the record for the confirmation message, and in addition you could use this feature for other types of home page messages that you dream up later, e.g. special promotions or new features. (I don't recommend building features you don't really need).
Send a paramater in the query string.
//home-page/?registered=1
Or you can set a cookie and check for the cookie if its set.. show the popup and delete it when user clicks on close popup.

PHP - return the same page with changes

I have html page where you can insert some information and then submit this form, which will change information in database. I do it normally, that submit button call php file in server.
But what I want, is that this php file will return to me the same html page of which I sent request, with modified changes. e.g: there will be "Database update successfully" text added etc.
How can I do it without AJAX ?
Thanks
In the PHP file, do a call to the header() function to redirect the user. For example:
header('Location: url.php');
To change the content of that page they are redirected to, you could pass something in the URL that your page will check for. For example:
header('Location: url.php?submitted=1');
There are other ways to implement this, but this seems the most straightforward to me. Note that you don't want to call header() until the end of your submission page.
Use POST/REDIRECT/GET
Excerpt:
The user submits the form
This is pretty straight forward. The user completes the form and submits it by pressing the submit button or enter on their keyboard.
We store the form data in a session
After processing the data we discover an error so we need to redisplay the form with an error message but we also want to populate
it with their data so they don't have to refill the entire form just
to fix potentially one little mistake. So we store their data in a
session ($_SESSION). Session variables carry over from page-to-page
for as long as the session is valid or until they are deleted. This is
an ideal place to put their information since redirecting will cause
their information to be immediately discarded by the server.
We redirect the user back to the same page using a 303 redirect
Once we have saved the user's information in their session we need to redirect them back to the same page. In order for this to work
properly we need to use a 303 redirect. This means we need to send a
303 header with our redirect. A 303 redirect will cause the browser to
reload the page without the initial HTTP POST request to be
resubmitted. This includes when the user uses the back or refresh
buttons.
We re-populate the form using the data stored in the session
When the page is sent to the user we re-populate it with their information we saved in their session.
Only by generating the whole page in CGI first, unless you go through some horribly convoluted method of getting value of one of the fields to be set to document.innerHTML or something like that in Javascript. But you'll go through hell to get the quoting issues resolved. Use AJAX, it was created for precisely this purpose and exactly to avoid the utter hell associated with what you need.
Alternatively: the "modified piece" of the page may be an iframe, and you can set the target attribute of the form, so that the PHP returns only the iframe content.

Jquery Mobile: Passing Parameters

I'm experimenting with JQuery Mobile by making a basic mobile app, but I've ran into an issue with passing parameters to other pages, be they separate HTML files or additional data-content:pages on the original file.
All I want to do is select a username from a drop down list, press a button, then proceed to the next page, placing the username in the header (e.g. Logged in as: Username). I've looked into multiple solutions, such as sessionStorage, PHP sessions, POST/GET from form submission, and none of them seem to work.
The closest I've got to what I want is submitting the username via POST through a form, then saving it as a PHP session variable, which displays the username on the next page, but when I navigate to another page it once again fails to display.
What is the simplest way to pass a parameter forward? I have a suspicion that it doesn't work on the additional pages due to JQuery Mobile's loading method excluding the session_start() from the header...
I also have problem with php $_SESSION in jquerymobile, here i have 4 pages
submit form, save in session
start session, display correctly, click on link
start session, nothing to display, click on link
start session, nothing to display
but it ALL WORKS with Internet Explorer 10.
I already did some test with my own program, use $_COOKIE instead of $_SESSION. Now it runs correctly in every browser with jquerymobile

how to maintain data in text box when page reloads in codeigniter

how to maintain data in text box when page reloads in codeigniter.
Example:
Suppose that I fill up the username and password and I refresh the page then the username and password’s fields should not be blank, they should contain the values before the reload
If this cannot be done in codeigniter what is the normal php method
I think you must use cookies or ajax fire on text changed of your textbox.
If you use ajax, you can then store values insides session.
Do it on client side.
Save the value of textbox to a cookie value on each key press.
When the page is loaded, check the cookie for a value, if there is, set it on the box.
(Don't forget to flush the value, after submitting data or using for whatever reason you are using)
By refresh do you mean upon submitting and failing on validation refreshing the page or simply just refreshing ?? If ur talking about a simple refresh then I Doubt there is any method to do it in CI or php. Yeah you could do it by client side scripting (js) and writing it to a cookie or make an ajax call to php script and save it to a session variable. And yeah do flush those values once saved as marvin said

What to do with a timed out user trying to use ajax functionality?

Here is the scenario,
User Logs InUser gets up, goes to get some coffee, and talks to co-worker Steve in the kitchen for 15 minsUsers session times outUser comes back to desk and trys to use a field on his/her screen which utilizes ajax functionality
Now, In the ajax page I am checking to see if he/she is logged in, but what do I do with the user? If I just return nothing from the ajax page, then the user does not know why the field is not working. If I try to use header("Location: "), that will not work as expected.
I could return a message saying you need to refresh the page, but that is kind of lame. What I would like to do is return the user back to the main page. I could do this using javascript obviously, but that is relying on the fact that user did not just go to http://website/ajaxpage.ajax.php and has javascript disabled. So what is the best way to handle this?
UPDATE
What about automatically refreshing the page after 15 minutes passes? Maybe using a meta tag? Or a javascript timeout on the page? That would cause the user to just see the login screen automatically when they sit down, however if they are on the same page for 15 minutes it may refresh and be annoying.
Is it using jsonp? You might be able to return a function that sets window.location and is called as the callback.
Alternatively, you can modify your logic in the JS and return a JSON object that has a timed out indicator, in which case you can handle it appropriately in the AJAX callback. for instance, you can put up a timed box that says "Your Session has Expired - please login again" and then redirect them to the login page.
EDIT In response to your update, I wouldn't automatically refresh it. What you can do is put in some smart idle detection logic and manage the refresh with setTimeout. Here is an example of one using Prototype, but you probably don't want to base it off mouse move.
You could send back a special message from the Ajax page, to indicate to your JavaScript code (running in the user's browser) that it needs to refresh the page. To do that, all you need is
window.location.reload(true);
https://developer.mozilla.org/en/DOM/window.location
before sending whatever data you are going to send to the ajax page, send a real quick query that checks session status, return a simple 1 or 0. on 1, continue and do the ajax action. on 0, call another function that pops up a modal "Login" box, sends that login info via ajax and again gets a 0 or 1 for un/successful login. if returns 1, then return and continue inital action, otherwise re-present the modal login box.
You could set a session_id in the cookies and allow the user to "stay logged in on this computer" so when it expires, and the user tries to use the ajax function, it could actually log him back in.

Categories