How to modify variable that was initiated in different PHP page? [duplicate] - php

This question already has answers here:
pass value from page to another in PHP
(3 answers)
Closed 8 years ago.
i have a variable for displaying a msg at the end of a procces,
and that procces in includeed in different page , i wanted to manipulate that variable in the other page(the one i included the procces in it)
the problem is that the variable is intitiated at the beggining of the procces page,
so whenever i use that page , the variable will reset.
i tried different ways but nothing worked
it just output the value i assign it in the procces page
i tried making it global and modifying it but didnt work ..

Your PHP page doesn't remember anything at all, not by itself. When someone types your page in their browser, the server executes your script. PHP blindly executes it and doesn't try to remember anything.
Except if you use sessions, or some form of persistent storage such as a database. In your case, you want to use sessions: Take a look.

HTTP is a stateless protocoll and so is PHP: you can't read variables in one page request from the other. You need to use sessions for that, session_start() and $_SESSION.

Related

PHP code vs server vs HTML / Handling POST requests [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm having a hard time grasping how PHP/HTML/the server works and I couldn't find any info (it has to be somewhere so if you have a link I'd appreciate it). I'll try to explain best what I understand/don't understand and ask a concise question.
My Understanding
PHP code is in the same file as HTML files and needs the server to have a PHP parser? to read and execute the PHP code.
PHP code executes first on the server side, then HTML/JS execute on the client side (hence you can't see the PHP code on the browser).
What I don't understand
When a PHP code calls a $_POST request, where is the request going? Let's say I call a $_POST request to mypage2.php. How do I handle this $_POST request in my mypage2.php code? (I setup an iFrame and tried to make a $_POST request to it, but it changed to that page).
How do I use multiple .php files on a site? I always see something like authentication.php but are those pages just classes to never be used (aka I can never go to mysite.com/authentication.php)?
My end goal is to get data from an external $_POST call, and send it to my page embedded in an iFrame. I feel I don't understand the basic concepts first though and all the tutorials discuss the syntax of PHP (which I think I understand).
I am running on a windows machine and using XAMPP if that is relevant. Thank you.
Your html POST going to $_POST array on page2.php.
This array has the same index of page1.html input/form.
<input name="username"> <input name="password">
To use it in your page2.php, you can store it on a variable like this
$username = $_POST['username'];
$password = $_POST['password'];
You kind of have it right:
PHP is in the same file with your HTML/CSS/JavaScript code. It requires a PHP interpreter on the Web server. The Web server will not send a raw PHP file (with PHP code in it) to a client browser, if configured correctly.
When there is a request for a PHP file, the PHP interpreter on the Web server processes the PHP file, which removes the PHP code and replaces it with the interpreted result (HTML/CSS/JavaScript code). It sends the processed file to the client browser as the PHP file. That file only contains HTML/CSS/JavaScript code. The client browser runs the HTML/CSS/JavaScript code that the Web server sends.
There are global variables in PHP that store data that's POSTed or otherwise sent to the PHP file. Have a look at $_POST, $_GET and $_REQUEST. Note that you should never trust data that's submitted by a user and you should always "sanitize" it (see What's the best method for sanitizing user input with PHP?)
You can use more than one PHP file on a website, but no variables or other data carry automatically from file to file, so each file starts with a blank sheet. There are ways to pass values among pages by passing them in the URL or by using PHP Sessions.
Some applications that use more than one PHP file don't want a user to be able to run a PHP file on its own. They only want the PHP file to be executed by the application, from another PHP file. So what the programmer will sometimes do is to set a global variable and store it as a session variable. The first thing a PHP file does is check that variable to see if it's set. If it's not, it means the user tried to run the PHP file by itself, and it just exits. That might be why you can't run a particular PHP file you mentioned.
If you have a file mypage2.php and you post 'mydata' to that file, for example from an HTML form where name='mydata', you can retrieve the data from the global $_POST, for example, <?php $mydata = $_POST['mydata'];?>. After you sanitize it, if the IFRAME is also within mypage2.php, you can insert it into the IFRAME by echoing it, for example, <?php echo $mydata;?>

/?q=#/ in URL Query String? [duplicate]

This question already has an answer here:
What is the shebang/hashbang for?
(1 answer)
Closed 9 years ago.
/?q=#/ in Query String, sometimes I saw in URL of facebook and twitter, what it is and why is it used?
Please explain in detail. I query it most of time in google but have not found satisfactory results.
I think it used in Ajax based framework.
It is for updating the page's URL without fully reloading the page. /?q= is just an empty variable in the query string. But, everything after a # is actually in the hash part of the URL. That was originally used to automatically scroll a page to an anchor (<a name="blah">). It is accessible via JavaScript, and because it doesn't reload the page, it is perfect for URLs that work when copy-pasted to the URL box, change when you do stuff on the page, but still keep everything loaded.
Shortly: /?q= is an empty variable in the query string, #/ and everything after that is data for JavaScript to process. The slash(es) in the hash string make it look like it is also a part of the directory structure.
Section 3 of RFC 3986 may be helpful.
The /?q= is a query string, where the parameter named q is blank/empty.
Everything after the # is called the fragment. Sometimes it's used to jump to a bookmark on the page, but, usually it's just some data that the Javascript running in your browser can use. If you change the part after # in the URL, your page doesn't reload, but, the Javascript in your browser can react to it.

Send html during php page loading [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
PHP echo-ing content as page loads
I have a php page that has to load a heavy table, so after the page request there is a lot of time to wait.
Is it possible to send the page initially empty and, during the php page loading, send to the client the results, so one can see what has been loaded so far? (not a loading bar).
Yes. It's called flushing the document early. At its simplest, output some amount of HTML, and then calling flush();. As with most things, the devil is in the details of exactly what and when things would happen.

retrieve name of calling script / Javascript in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Securing a remote ajax method call
Is there a way to retrieve the name of the javascript file that called a php script from within the PHP code?
Unfortunately, since I have to call a PHP script from Javascript (for Google maps) on a webpage, this 'invites' to retrieve my data by just calling the PHP script by hand. Checking for the script/file that invoked the PHP script would bring at least some protection.
You could set a session variable with some random value on the server when your page is opened, and add that variable in the query string to your php script.
Then in your javascript-called php script, you start the session and check the session variable against the query one.

Assign JavaScript variable to Smarty variable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to assign a javascript variable to a smarty variable
I want to assign a textfield value to Smarty variable using JavaScript and call Smarty function but I could not do it.
Please guide me.
You cannot do that in an easy way. PHP, and by extension Smarty, is parsed and run on the server side before the browser get the data. JavaScript is run on the client side when the browser parses the HTML, CSS and Javascript that the server sent.
You will have to make a new HTTP request in some way, sending the new data. You can do that by reloading the entire web page and sending stuff in the querystring (after ? in the URL), or slightly more advanced by doing an Ajax call from your JS code and make JS do the changes of the page that you desire. The latter is more complex and requires some knowledge of Javascript, but the page does not need to be reloaded in its entirety.
Did you mean something like this?
<script>
var foo_bar = {$foo.bar|escape:javascript};
</script>
Note that, as mentioned above, the value is computed server-side.
UPD. I get it now, you wanted to pass value the other way around. No, that’s not possible.

Categories