php 表单提交后如何在同一html页面上回显?

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

How to echo on same html page after form submit?

phphtmlformssubmitecho

提问by zefs

When the form is submitted, my page is redirected to the PHP and the echo is displayed. How can I display the echo without being redirected to the PHP page? So the echo should be displayed in the html page (where the html form is located).

提交表单后,我的页面被重定向到 PHP 并显示回显。如何在不重定向到 PHP 页面的情况下显示回声?所以回声应该显示在html页面中(html表单所在的位置)。

<form method="post" action="../form.php">
<input type="submit" name="1" value="YES" />
<input type="submit" name="1" value="NO" />
</form>

-

——

<?php 
$answer = $_POST['1'];  
if ($answer == "YES") {          
    echo 'Good!!!';      
}
else {
    echo 'Try Again!';
} 
?>

回答by Bogdan

I guess you can use a combination of <iframe>and javascript to get the results.

我想您可以使用<iframe>和 javascript的组合来获得结果。

<form method="post" action="submit.php" target="myIframe" >
    <input type="submit" name="1" value="YES" />
    <input type="submit" name="1" value="NO" />
</form>

<iframe name="myIframe">
</iframe>

回答by Max Snijders

You can't do it on the same page - at least not without asynchronous client-server communication techniques such as AJAX. Otherwise, you can use the following:

你不能在同一个页面上做到这一点——至少在没有 AJAX 等异步客户端-服务器通信技术的情况下不能。否则,您可以使用以下内容:

<form action="<?php echo $_SERVER['PHP_SELF']; ? method=....>"

as your form opening tag, then put the PHP processing code at the top of the document, like this:

作为您的表单开始标记,然后将 PHP 处理代码放在文档顶部,如下所示:

<?php
  if(isset($_POST['form_field_name'])){
       //process your data here
?>

HTML in case of Form filled in goes here

<?php
}else{
?>

HTML in case of Form not filled in goes here

<?php
  }
?>

HTML in any case goes here

This way you can change the layout of your page depending on whether the form was filled in or not. the $_SERVER['PHP_SELF'] contains the reference to the currently requested page, and therefore is always set to the correct page even if it is renamed. This can save you time when bug-tracking.

通过这种方式,您可以根据表单是否已填写来更改页面布局。$_SERVER['PHP_SELF'] 包含对当前请求页面的引用,因此即使重命名也始终设置为正确的页面。这可以节省您跟踪错误的时间。

回答by Marcus Recck

<?php

if(isset($_POST['1'])){
    echo ($_POST['1'] == 'YES') ? 'Good!!!' : 'Try Again!';
}

?>

<!-- your HTML goes here -->

You can combine HTML and PHP on the same page.

您可以在同一页面上组合 HTML 和 PHP。

回答by Jagmohan Singh

<html>
<body>
<form method="post" action="">
<input type="text" maxlength="30" name="nm">
<input type="email" maxlength="30"  name="em"><br>
<input type="checkbox" name="chkbox1" value="male">Male
<input type="checkbox" name="chkbox2" value="male">Male
<input type="checkbox" name="chkbox3" value="male">Male
<input type="checkbox" name="chkbox4" value="male">Male
<input type="checkbox" name="chkbox5" value="male">Male
<input type="Submit" name="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
echo "Welcome :".$_POST['nm']."<br>";
echo "Your email Address is: ".$_POST['em']."<br>";
}
$count=0;
if(isset($_POST['chkbox1']))
$count++;
if(isset($_POST['chkbox2']))
$count++;
if(isset($_POST['chkbox3']))
$count++;
if(isset($_POST['chkbox4']))
$count++;
if(isset($_POST['chkbox5']))
$count++;
echo" Number of checkboxes ticked are:".$count;
?>

回答by Andrew

You can set the action to the same page for example:

您可以将操作设置为同一页面,例如:

If I am working on a file called index.php

如果我正在处理一个名为 index.php 的文件

that will be my form:

那将是我的表格:

<form action="index.php" method="post">
   <input type="text" name="Example" placeholder="Enter Text">