Pass PHP variable from one page to another and to another [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
pass value from page to another in PHP
For example, I have 3 pages. First page will be a regular HTML login page which will ask for username. Second page will get this username variable thru POST function and assign a php variable such as $username. Third page will take this $username variable and display it. How can this be done? I have done the first two pages but I need help with passing it to the third page.
Thanks!

Check the PHP documentation for sessions. There are many examples of how to do this.
http://www.php.net/manual/en/session.examples.basic.php

Use a $_SESSION superglobal:
http://us3.php.net/manual/en/reserved.variables.session.php
You'll need to use a session_start() function at the beginning of any page you want to maintain session data for. After that, you can just assign to and retrieve from $_SESSION['username'].

Related

Exchange variables between two pages without changing the page

I am aware that this kind of question has already been asked before.but my case is i want to send a value to another php page, and get the value from it without changing the page. is that possible ? and if it is. can i have an example ?.
You can use AJAX as Jeff said.
Here's an example:
PHP - AJAX and PHP
You can use session in this case
in which one page will send the data to another page

Is it possible to call a PHP function from js file? [duplicate]

This question already has answers here:
Call php function from JavaScript
(5 answers)
Closed 5 years ago.
Is there any way I can call a PHP function from jQuery? I have a .js file and a .php file.
I have a jQuery event applied on a button click. Now I want to set a condition on what happens next when the button is clicked depending on whether a user is logged in or not. Is there any way I can do this from my .js file ?
You can call any php file by the common HTTP methods, e.g. GET and POST.
What you might be looking for is AJAX. Google the term, and also look into jQuery for a simplified approach.
You can send it to a php file with ajax and use your php file to check if the user's logged in. Then you can send a response back to your js file to see which event you should trigger
Technically, yes. But why would you want to in this case? My suggestion is to look into storing values in cookies, since both PHP and JS will have access to them.

Pass variables in url using POST

Assuming I have a two pages (page1 and page2), there are variable values I want to send to page2 without using $_GET function but instead $_POST through a url.
Example on page1
<?php
$name="Dman";
$id="12";
$level="122";
?>
Goto Page2
On page 2, I want to receive the variable values using POST.
name:dman
id: 12
level:122
UPDATE
Please what I am trying to achieve, is to prevent showing items being transmitted from one page to the another, when url is been used. I read some blogs and they are like, using POST workaround can help achieve this.On the contrary if there is a way to avoid using "?id=$id&name=$name" on a url but still receive your variables on the next page, I would be grateful to know.
Thanks for helping
You need to use the GET method if you want to pass parameters in the URL. POST is a different method and it's not possible to represent or reference POST data in the URL.
Some reading on the definition and difference between post and get: http://www.w3schools.com/tags/ref_httpmethods.asp
The GET method pass the parameters INTO the url, for example :
http://your/path?yourvariable=value
The POST method hide the parameters from the url.
The simplest way to pass parameter with POST method is to make a HTML form. In your Php sript, you can access to this value with the $_POST superglobal array.

need to use variables in another page [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP Pass variable to next page
Is it possible to use super global variables on another page. Basically I have two pages form.php and search.php. I'm calling search.php using ajax on submit button on-click also I am using method post and without action. If I try to use super global variable in search.php which is in form.php then it gives error for undefined variable.
I don't want to use session, cookies or $_get. I have to fetch data from MySQL using those vaiables.
You have to include one page in another or create 3rd page with your variables.
Or you just use session superglobal.
in place of global variable you can use session variables

Using PHP variables on multiple pages

Is it possible to use a variable from one page in a piece of code on another? e.g. submit a form on one page and on the second page use a PHP script to add the data from the form to a MySQL table
Thanks for all the help
Your best bet would be to use PHP's session functions. That way if the user navigates away from your page and then comes back, the session variables will still be there (provided the session hasn't expired). You can get more information here -- PHP Sessions. Basically all you have to do is call
session_start();
at the top of each php page (before anything is outputted to the browser) where you want to have access to the session variables. You can then set/retrieve a variable using
// set
$_SESSION['varname'] = "something";
// retrieve
$somevar = $_SESSION['varname'];
This is what the GET and POST 'super' globals are for.
http://www.tizag.com/phpT/postget.php
The script that generates the form does not have to be the script the processes the form. all it takes is for the FORM tag to point to the correct script. The only caveat is that whatever page you submit the form data to also has to then generate some suitable output (unless you do a AJAX POST).
If you're instead asking if two page executions can handle the same POSTed data, well that is quite a different question. At a simplistic level, you can have the first include() the second so it has access to all the same data. A bit more advanced is to re-create the original POST and synthesize a POST. This will probably require some JavaScript assistance. Another approach is to use PHP's session feature and put the value aside so a later script has access to it. This relies on the user then following the correct link, however!
Use session to access your variable to the second/other page.
Example:
index.php
<?php
session_start();
$_SESSION['yourData'] = 'yourData';
?>
=========================================================================
second_page.php
<?php
echo $_SESSION['yourData'];
?>
You may try this one. Visit this link for more details. Or PHP's official website.

Categories