Jquery Mobile: Passing Parameters - php

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

Related

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.

Implementing a 'Email to a friend' functionality. How to pass variables to the next page without using form, session and cookies?

I'm trying to create a "Email to friend" page using php. The objective of this page is that users can share the page that they are viewing with their friends.
When a user clicks on the 'share' link, it'll redirect user to a page that asks a user to input their own email address and a recipient email address. The subject will be the previous page title and the email body will be the URL of the previous page plus whatever a user may want to include.
I've got the whole concept here but I'm stuck on the implementation stage. I can't seem to figure the best way to pass the previous page title and the page URL to the share page.
Here's what I have thought of so far.
Using POST and GET method doesn't
seem to fit in because there is no
forms involved when a user clicks on
the share link.
Using session and cookies would be
very tedious as it requires assigning
and modifying the cookie / session
each time a user views a page.
Passing variables in URL would make
simply make the URL long and somewhat
undesirable.
Is there any other way that I could use to pass the page title and page url to the next page? I'm open for other suggestions on how I could implement this idea differently. Thanks in advance.
As far as I can see, passing the URL as a GET parameter is indeed the ideal solution.
http://example.com/share.php?url=http%3a%2f%2fwww.example.com
note that
You need to URL-encode the URL you are passing using urlencode()
the total resulting URL should not be longer than 2-4 kilobytes due to restrictions in some browsers.
I don't understand why POST and GET are not an option. Just because there isn't a form on the page doesn't mean you can't put one there. Can the link be turned into a button? If you don't like the look of a button, use CSS. Putting a form on the page would only take a few lines.
I would go for the session approach, even though you consider it tedious. This is called "flash messages" and it's quite commonspread. Zend Framework has these out of the box, CodeIgniter has a neat contributed library for it... Basically, you just need to write a few helper functions and you're set. To get the barebones functionality, you need:
adding a new message
retrieving a message/all messages
clearing messages (could be called after fetching messages)
The messages stored in the session will persist until you clear them, they are immune to redirecting and once you write your helper functions, it'll be as easy as:
//before redirect:
setFlash('You have successfully logged in!');
//after redirect
echo fetchFlash();
clearFlash(); //if fetchFlash doesn't call it automatically
So I wouldn't call it tedious, really. Rather a butt-saver.

browser backbtn with javascript

I am creating a quiz game in PHP.
I maintain a session from the start to the end of the quiz.
When the user clicks on the back button in the middle of the quiz
It display a page with message
"Confirm Form Resubmission This web page requires data that you entered earlier in order to be properly displayed"
So, I m trying to keep it on the same page on click of back button
I have handled the "onbeforeunload" event & it's firing up too.
code snippet
Redirectpage
{
javascript:window.history.forward(1);
}
but still am getting the same error?
thankyou folks.
Your page does not have rights to control the browser. OnbeforeUnload you can alert user and return false to stay on the page. Otherwise you cannot do much on it except expiring the previous page, so that user cannot go to previous page.
Two possible solutions:
Don't use use post, that's what causes the warning, if you submit using get, you won't get the warning.
If you can't do that:
Use AJAX instead of posting and replacing the entire page. That is so 1990s and leads to very confusing code. Keep state on the client. It doesn't seem you have a need to bookmark each of the pages, and if you did, you'd have to learn how to handle back button within an AJAX app. Here's one solution: http://www.zedwood.com/article/101/ajax-back-button-fix

Send POST from php Zend-Framework site

I am currently working on a zend framework site using an ACL.
The ACL works and uses a DB for storing privaliges. What I have done so far is, on in a preDispatch I capture where the user wanted to go to, and if they need to login to get there the page displays the login form. I have also captured if a user has submitted a form and stored the data (location and form data). No problems so far.
The user submits the login form, and I then check if I have a location to send them onto, again no problems here, this works.
However I want to be able to submit the original form data now they are autherised - the only problem is, if I redirect them to the page, the call to:
$this->getRequest()->isPost()
fails as it isn't a post request.
I can forward the user to the page on sucessful login, and in the preDispatch set $_POST to the data originally captured, this works as the original POST still stands, this works but I do not think is the correct way to do this - specially the URL - obviously the page displayed is correct, the form has been submitted correctly, but the URL is of the login process.
Obviously I could change from using $this->getRequest()->isPost() but as there are large amounts which would need changing I was hoping not to have to do this.
The way it's done usually (seen this on many sites), would be to store the form data and requested action in session, then redirect the user to a login page. When login is authorized, you read the session for any pending action, reload the form and populate it with data from session (properly clearing any pending action from session afterwards). The user would just have to click Submit again.
The other way to do this would be to use HttpClient and submit the data as POST with it.

User sessions with jquery and Ajax

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.

Categories