简体   繁体   English

PHP发布到另一个页面还是一样?

[英]php post to another page or the same?

I usually create another page to do my php posts like this: 我通常会创建另一个页面来做这样的php帖子:

page1.php: page1.php:

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

page2.php: page2.php:

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

one friend of mine told me that I should to this in the same page: page1.php 我的一个朋友告诉我,我应该在同一页面上这样做: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['...']; 第一部分中,您是将控件从page1传递到page2 ...这是通过提交按钮完成的,因此您可以使用$_POST['...'];直接获取值$_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. 但是这里需要检查是否已设置过帐数据,因此您可以使用isset方法。 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 因此,在第一部分中使用isset()方法是一个很好的习惯 ,但是在第二个解决方案中则是必要的

In my opinion Better use another page so that even the code does not look messed up. 我认为最好使用另一页,这样即使代码看起来也不会混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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