need to use variables in another page [duplicate] - php

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

Related

How to pass a variable in php between pages automatically (without using Form Inputs)

I want to pass variable from one page to another say $id from page1 to page2 but I want that to happen automatically without hitting submit button like in $_POST or $_GET method. How to do that?
Use php php session , you can set/get session variables using $_SESSION super global.

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.

How can I pass variable from a view to another view [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
codeigniter : pass data to a view included in a view
How can I pass variable from a view to another view inside it?(codeigniter)
Thanks:D
If you mean, that you set a variable in one view, then you call an other view inside that view and you would like to use it there, the easiest way, to set it in the controller, which call the view.
$data['var'] = 'value';
$this->load->view('view file',$data);
Then you can reach that in every sub view.

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

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'].

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