7 Replies - 2300 Views - Last Post: 11 October 2012 - 08:13 AM Rate Topic: -----

#1 epicepiphron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-January 12

PHP form post to itself

Posted 10 October 2012 - 04:55 PM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: PHP form post to itself

#2 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

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.
Was This Post Helpful? 2
  • +
  • -

#3 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

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.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,886
  • Joined: 08-August 08

Re: PHP form post to itself

Posted 10 October 2012 - 09:21 PM

Try this code with and without line 2:
<?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.
Was This Post Helpful? 1
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: PHP form post to itself

Posted 10 October 2012 - 11:52 PM

View Postsas1ni69, 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.)
Was This Post Helpful? 1
  • +
  • -

#6 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: PHP form post to itself

Posted 10 October 2012 - 11:57 PM

View PostAtli, on 11 October 2012 - 02:52 PM, said:

View Postsas1ni69, 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.

View Postepicepiphron, 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?

Was This Post Helpful? 0
  • +
  • -

#7 epicepiphron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-January 12

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?
Was This Post Helpful? 0
  • +
  • -

#8 Sho Ke  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1