简体   繁体   中英

php post to another page or the same?

I usually create another page to do my php posts like this:

page1.php:

<form action="page2.php" method="post">
...
</form>

page2.php:

<?php
$var = $_POST['...'];
?>

one friend of mine told me that I should to this in the same page: page1.php

<?php
if (isset($_POST['...'])){
...
}
else{
?>
   <form action="page1.php" method="post">
    ...
    </form>
<?php
}
?>

My question is, which one is a better or faster method and best practise? thank you friends!

You can do it in both the ways you have mentioned .

its not like you " Should to this in the same page "

In first par t you are passing the control from page1 to page2 ...which is done by submit button So you can directly get the values using $_POST['...'];

Now in seccond part you are passing the control to same page , Since you are calling the same page on submit .

But here need to check if Post data has been set so for that you use isset method. Most importantly you can use second solution if you want to stay on the same page after submission

Therefore, Use of isset() method in first part is a good habbit , but in second solution is a necessity

In my opinion Better use another page so that even the code does not look messed up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM