PHP form post to itself
Page 1 of 17 Replies - 752 Views - Last Post: 11 October 2012 - 08:13 AM
#1
PHP form post to itself
Posted 10 October 2012 - 04:55 PM
Thank you.
Replies To: PHP form post to itself
#2
Re: PHP form post to itself
Posted 10 October 2012 - 05:10 PM
#3
Re: PHP form post to itself
Posted 10 October 2012 - 08:23 PM
#4
Re: PHP form post to itself
Posted 10 October 2012 - 09:21 PM
<?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:
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:
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:
#7
Re: PHP form post to itself
Posted 11 October 2012 - 12:31 AM
#8
Re: PHP form post to itself
Posted 11 October 2012 - 08:13 AM
This post has been edited by Sho Ke: 11 October 2012 - 08:13 AM
|
|

New Topic/Question
Reply



MultiQuote






|