简体   繁体   English

PHP 表格 - 在同一页面提交

[英]PHP form - on submit stay on same page

I have a PHP form that is located on file contact.html .我有一个 PHP 表格,位于文件contact.html中。

The form is processed from file processForm.php .该表单是从文件processForm.php处理的。

When a user fills out the form and clicks on submit, processForm.php sends the email and direct the user to - processForm.php with a message on that page "Success. Your message has been sent."当用户填写表单并单击提交时, processForm.php会发送 email 并将用户定向到 - 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.php page?如何在不重定向到processForm.php页面的情况下将消息保留在表单 div 中?

I can post the entire processForm.php if needed, but it is long.如果需要,我可以发布整个processForm.php ,但它很长。

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; ?>回显该变量<?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>

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

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

There are two ways of doing it:有两种方法可以做到:

  1. Submit the form to the same page : Handle the submitted form using PHP script.将表单提交到同一页面:使用 PHP 脚本处理提交的表单。 (This can be done by setting the form action to the current page URL.) (这可以通过将表单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. Using AJAX Form Submission which is a little more difficult for a beginner than method #1.使用 AJAX 表单提交对于初学者来说比方法 #1 稍微困难一些。

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>

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>

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 if like 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.org or phpacademy.org .请访问www.thenewboston.orgphpacademy.org There are very good PHP video tutorials, including forms.有非常好的 PHP 视频教程,包括表单。

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; 
   }
 ?>

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

simple just ignore the action attribute and use !empty (not empty) in php.简单的只是忽略action属性并在 php 中使用!empty (非空)。

<form method="post">
        <input type="name" name="name">
        <input type="submit">
    </form>
    <?PHP
       if(!empty($_POST['name']))
       { 
           echo $_POST['name'];
       }
    ?>

I know this is an old question but since it came up as the top answer on Google, it is worth an update.我知道这是一个老问题,但由于它是 Google 上的最佳答案,因此值得更新。

You do not need to use jQuery or JavaScript to stay on the same page after form submission.提交表单后,您无需使用 jQuery 或 JavaScript 停留在同一页面上。

All you need to do is get PHP to return just a status code of 204 (No Content).您需要做的就是获取 PHP 以仅返回状态代码 204 (无内容)。

That tells the page to stay where it is.这告诉页面留在原处。 Of course, you will probably then want some JavaScript to empty the selected filename.当然,您可能随后需要一些 JavaScript 来清空所选文件名。

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 eg虽然我从一个单独的文件中包含了 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)应该使用(不是我的话,请在某处阅读)

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

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