Hi I currently have a php file with a form that posts back to itself. However, I'm confused what happens to the variables in the php file. If I have a variable defined in the php file (say $x = 10;) before the post, would $x still exist and qual 10 when i come back from the post? I'm thinking yes, but could someone explain why? I'm not sure how it works after a post.
Thank you.
PHP form post to itself
Page 1 of 17 Replies - 672 Views - Last Post: 11 October 2012 - 08:13 AM
Replies To: PHP form post to itself
#2
Re: PHP form post to itself
Posted 10 October 2012 - 05:10 PM
It won't retain the variables (such as your $x example). Each time the PHP script is called, you're operating on a clean slate.
#3
Re: PHP form post to itself
Posted 10 October 2012 - 08:23 PM
If $x = 10 has been defined in the file before post $x will still be 10 after post.
#4
Re: PHP form post to itself
Posted 10 October 2012 - 09:21 PM
Try this code with and without line 2:
Click submit several times.
<?php
session_start();
if(!empty($_SESSION['mynum'])) {
$num = $_SESSION['mynum'] + 10;
} elseif(!empty($_POST['mynum'])) {
$num = $_POST['mynum'];
} else {
$num = 10;
}
$form = <<<EXAMPLE
<form method="post">
<input type="text" name="mynum" value="{val}" >
<input type="submit" value="Submit">
</form>
EXAMPLE;
echo str_replace("{val}", $num, $form);
$_SESSION['mynum'] = $num;
Click submit several times.
#5
Re: PHP form post to itself
Posted 10 October 2012 - 11:52 PM
sas1ni69, on 11 October 2012 - 03:23 AM, said:
If $x = 10 has been defined in the file before post $x will still be 10 after post.
I doubt he meant that $x was hard-coded to 10, but rather that it had by some logic been made to have the value 10. In which case Mina-no-Hime is correct, it won't retain it's value.
The thing to keep in mind is that PHP scripts are executed very quickly, and once they are done and you've got the HTML on your browser screen in front of you, then the PHP script that generated it has ended and can not (easily) be returned to the state it was in while it was alive. - PHP was made to work with the stateless nature of the web; to only exist during the time it takes to respond to a single request.
If you need to keep values around for longer than a single request, you must use one of the many methods available to us to store variables. Those range from keeping them in databases, in files on the server, in session variables, or in the actual request/response body using cookies or HTML form elements. (CTphpnwb demonstrated the form fields and session variables methods in his post.)
#6
Re: PHP form post to itself
Posted 10 October 2012 - 11:57 PM
Atli, on 11 October 2012 - 02:52 PM, said:
sas1ni69, on 11 October 2012 - 03:23 AM, said:
If $x = 10 has been defined in the file before post $x will still be 10 after post.
I doubt he meant that $x was hard-coded to 10, but rather that it had by some logic been made to have the value 10. In which case Mina-no-Hime is correct, it won't retain it's value.
That was my first thought too but then this part here sounded different to me.
epicepiphron, on 11 October 2012 - 07:55 AM, said:
If I have a variable defined in the php file (say $x = 10;) before the post, would $x still exist and qual 10 when i come back from the post?
#7
Re: PHP form post to itself
Posted 11 October 2012 - 12:31 AM
Ahh ok. So if a form submits back to itself, then all the php code in that php file gets ran all over again starting from the top, and everything from the previous run is lost. Is this right?
#8
Re: PHP form post to itself
Posted 11 October 2012 - 08:13 AM
With your $x example, yes. However, you can take advantages of sessions to save information from page to page, granted session_start() will have to be called near the top of each page.
This post has been edited by Sho Ke: 11 October 2012 - 08:13 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|