47

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

表格是从文件处理的processForm.php

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

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

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

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

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

4

10 回答 10

50

为了在提交时保持在同一页面上,您可以将操作留空 ( action="") 到表单标记中,或者完全不使用它。

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

像这样:

<?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>
于 2014-07-20T21:15:30.270 回答
18

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

<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
于 2013-10-18T20:13:54.310 回答
14

有两种方法:

  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 更难一些。

于 2013-06-27T03:15:38.723 回答
8

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

<?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>
于 2017-01-10T07:39:26.757 回答
5

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

<?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>
于 2018-02-23T03:32:33.493 回答
2

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

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>";

像这样的代码if对于许多页面来说是典型的,但是这非常简化。

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

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

于 2013-06-27T03:19:26.933 回答
2

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

<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; 
   }
 ?>
于 2019-05-01T17:10:58.777 回答
1

试试这个……对我有用

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

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

<?php header("Location: ../index.php"); ?>
于 2017-10-25T14:08:37.750 回答
0

简单的只是忽略action属性并在 php 中使用!empty (非空)。

<form method="post">
        <input type="name" name="name">
        <input type="submit">
    </form>
    <?PHP
       if(!empty($_POST['name']))
       { 
           echo $_POST['name'];
       }
    ?>
于 2021-11-29T18:23:44.920 回答
-1

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

($_SERVER["PHP_SELF"])

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

include_once "test.php";

我也在某处读到

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

是一种初学者的旧式张贴表格的方式,并且

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

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

于 2019-01-30T19:35:48.187 回答