php 提交后显示消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4581733/
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 13:33:18  来源:igfitidea点击:

Show message after submit

phpforms

提问by codedude

How can I display an alert after a user clicks a submit button in a form?

如何在用户单击表单中的提交按钮后显示警报?

I want the alert to show when the page has loaded after the submit button has been clicked.

我希望在单击提交按钮后页面加载时显示警报。

Is there any way to do this?

有没有办法做到这一点?

EDIT:

编辑:

I need the message to be HTML text that is displayed on the page--not a javascript alert.

我需要消息是显示在页面上的 HTML 文本——而不是 javascript 警报。

采纳答案by Brad Christie

pagewithform.php

pagewithform.php

<html>
  <head>
  ...
  </head>
  <body>
    ...
    <form action="myformsubmit.php" method="POST">
      <label>Name: <input type="text" name="name" /><label>
      <input type="Submit" value="Submit" />
    </form>
    ...
  </body>
</html>

myformsubmit.php

我的表单提交.php

<html>
  <head>
  ....
  </head>
  <body>
    <?php if (count($_POST)>0) echo '<div id="form-submit-alert">Form Submitted!</div>'; ?>
    ...
  </body>
</html>

EDITEDFits new critieria of OP on last edit.

已编辑在上次编辑时符合 OP 的新标准。

EDITv2Try it at home!

EDITv2 在家试试吧!

<html>
  <head>
    <title>Notify on Submit</title>
  </head>
  <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
      <label>Name: <input type="text" name="name" /></label>
      <input type="submit" value="Submit" />
    </form>
    <?php if (count($_POST)>0) echo "Form Submitted!"; ?>
  </body>
</html>

Try that on for size.

试穿它的大小。

回答by jmort253

Since you're submitting back to the same page, a cleaner and more modern way of doing this would be to use JQuery to submit the form using AJAX. You can then specify a callback method that will update a container on the page to reflect the change in state:

由于您要提交回同一页面,因此一种更简洁、更现代的方法是使用 JQuery 使用 AJAX 提交表单。然后,您可以指定一个回调方法,该方法将更新页面上的容器以反映状态的变化:

$('#myForm').submit(function() {
   $('#myResultDiv').text("Form submitted");
   return false;
});


... 

<div id="myResultDiv"></div>

This prevents the unnecessary reloading of the page, making your web application snappier and more responsive.

这可以防止不必要的页面重新加载,使您的 Web 应用程序更加快速和响应更快。

This also has the added benefit of keeping your HTML and JavaScript (content and behavior) separate, for which your web designers will thank you for.

这还有一个额外的好处,就是让你的 HTML 和 JavaScript(内容和行为)分开,你的网页设计师会为此感谢你。

This would work with just about any server-side platform, including but not limited to PHP.

这几乎适用于任何服务器端平台,包括但不限于 PHP。

回答by ShoeLace1291

<?php

echo "<html>
<head>
</head>";

if($_POST['submit']){
     echo 'The form was submitted!";
} else {
    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST"><input type="submit" name="submit" value="Submit">';
}

echo "</html>";

?>

回答by user6618995

Since you're submitting back to the same page, a cleaner and more modern way of doing this would be to use JQuery to submit the form using AJAX. You can then specify a callback method that will update a container on the page to reflect the change in state:

由于您要提交回同一页面,因此一种更简洁、更现代的方法是使用 JQuery 使用 AJAX 提交表单。然后,您可以指定一个回调方法,该方法将更新页面上的容器以反映状态的变化: