PHP 表单 - 提交时停留在同一页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17333901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:41:58  来源:igfitidea点击:

PHP form - on submit stay on same page

phphtmlformssubmit

提问by mewebs

I have a PHP form that is located on file contact.html.

我有一个位于文件中的 PHP 表单contact.html

The form is processed from file processForm.php.

该表单是从 file 处理的processForm.php

When a user fills out the form and clicks on submit, processForm.phpsends the email and direct the user to - processForm.phpwith a message on that page "Success! Your message has been sent."

当用户填写表单并单击提交时, processForm.php发送电子邮件并将用户引导至 -processForm.php在该页面上显示一条消息“成功!您的消息已发送”。

I do not know much about PHP, but I know that the action that is calling for this is:

我对 PHP 了解不多,但我知道要求这样做的操作是:

// Die with a success message
die("<span class='success'>Success! Your message has been sent.</span>");

How can I keep the message inside the form div without redirecting to the processForm.phppage?

如何在不重定向到processForm.php页面的情况下将消息保留在表单 div 中 ?

I can post the entire processForm.phpif needed, but it is long.

processForm.php如果需要,我可以发布整个,但它很长。

回答by Tom Groot

In order to stay on the same page on submit you can leave action empty (action="") into the form tag, or leave it out altogether.

为了在提交时保持在同一页面上,您可以将 action 留空 ( action="") 到表单标签中,或者将其完全省略。

For the message, create a variable ($message = "Success! You entered: ".$input;") and then echo the variable at the place in the page where you want the message to appear with <?php echo $message; ?>.

对于消息,创建一个变量 ( $message = "Success! You entered: ".$input;"),然后在页面中您希望消息以 出现的位置回显该变量<?php echo $message; ?>

Like this:

像这样:

<?php
$message = "";
if(isset($_POST['SubmitButton'])){ //check if form was submitted
  $input = $_POST['inputText']; //get input text
  $message = "Success! You entered: ".$input;
}    
?>

<html>
<body>    
<form action="" method="post">
<?php echo $message; ?>
  <input type="text" name="inputText"/>
  <input type="submit" name="SubmitButton"/>
</form>    
</body>
</html>

回答by ChaseC

The best way to stay on the same page is to post to the same page:

保持在同一页面上的最佳方式是发布到同一页面:

<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">

回答by Ashrith Sheshan

There are two ways of doing it:

有两种方法可以做到:

  1. Submit the form to the same page: Handle the submitted form using PHP script. (This can be done by setting the the form actionto the current page URL.)

    if(isset($_POST['submit'])) {
        // Enter the code you want to execute after the form has been submitted
        // Display Success or Failure message (if any)
      } else {
        // Display the Form and the Submit Button
    }
    
  2. Using AJAX Form Submissionwhich is a little more difficult for a beginner than method #1.

  1. 将表单提交到同一页面:使用 PHP 脚本处理提交的表单。(这可以通过将表单action设置为当前页面 URL 来完成。)

    if(isset($_POST['submit'])) {
        // Enter the code you want to execute after the form has been submitted
        // Display Success or Failure message (if any)
      } else {
        // Display the Form and the Submit Button
    }
    
  2. 使用 AJAX 表单提交对于初学者来说比方法 #1 稍微困难一些。

回答by Surendra Kumar Ahir

You can use the #action in a form action:

您可以#在表单操作中使用该操作:

<?php
    if(isset($_POST['SubmitButton'])){ // Check if form was submitted

        $input = $_POST['inputText']; // Get input text
        $message = "Success! You entered: " . $input;
    }
?>

<html>
    <body>
        <form action="#" method="post">
            <?php echo $message; ?>
            <input type="text" name="inputText"/>
            <input type="submit" name="SubmitButton"/>
        </form>
    </body>
</html>

回答by john abraham

Friend. Use this way, There will be no "Undefined variable message" and it will work fine.

朋友。使用这种方式,不会有“未定义的变量消息”,它会正常工作。

<?php
    if(isset($_POST['SubmitButton'])){
        $price = $_POST["price"];
        $qty = $_POST["qty"];
        $message = $price*$qty;
    }

        ?>

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
        <form action="#" method="post">
            <input type="number" name="price"> <br>
            <input type="number" name="qty"><br>
            <input type="submit" name="SubmitButton">
        </form>
        <?php echo "The Answer is" .$message; ?>

    </body>
    </html>

回答by john abraham

Try this... worked for me

试试这个……对我有用

<form action="submit.php" method="post">
<input type="text" name="input">
<input type="submit">
</form>

------ submit.php ------

------ 提交.php ------

<?php header("Location: ../index.php"); ?>

回答by Kamil

You have to use code similar to this:

您必须使用与此类似的代码:

echo "<div id='divwithform'>";

if(isset($_POST['submit']))  // if form was submitted (if you came here with form data)
{
    echo "Success";
}
else                // if form was not submitted (if you came here without form data)
{
    echo "<form> ... </form>";
} 

echo "</div>";

Code with iflike this is typical for many pages, however this is very simplified.

if像这样的代码在许多页面中都是典型的,但是这是非常简化的。

Normally, you have to validate some data in first "if" (check if form fields were not empty etc).

通常,您必须在第一个“if”中验证一些数据(检查表单字段是否不为空等)。

Please visit www.thenewboston.orgor phpacademy.org. There are very good PHP video tutorials, including forms.

请访问www.thenewboston.orgphpacademy.org。有非常好的 PHP 视频教程,包括表单。

回答by Majbah Habib

You can see the following example for the Form action on the same page

您可以在同一页面上看到以下表单操作示例

<form action="" method="post">
<table border="1px">
    <tr><td>Name: <input type="text" name="user_name" ></td></tr>
    <tr><td align="right"> <input type="submit" value="submit" name="btn"> 
</td></tr>
</table>
</form>

<?php
  if(isset($_POST['btn'])){
     $name=$_POST['user_name'];
     echo 'Welcome '. $name; 
   }
 ?>

回答by Patrick Smit

What I do is I want the page to stay after submit when there are errors...So I want the page to be reloaded :

我所做的是我希望页面在出现错误时提交后保留......所以我希望页面重新加载:

($_SERVER["PHP_SELF"])

While I include the sript from a seperate file e.g

虽然我从一个单独的文件中包含了 sript,例如

include_once "test.php";

I also read somewhere that

我也在某处读到

if(isset($_POST['submit']))

Is a beginners old fasion way of posting a form, and

是初学者发布表格的老式方式,并且

if ($_SERVER['REQUEST_METHOD'] == 'POST')

Should be used (Not my words, read it somewhere)

应该使用(不是我的话,请在某处阅读)