简体   繁体   English

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

[英]How to echo on same html page after form submit?

When the form is submitted, my page is redirected to the PHP and the echo is displayed. 提交表单后,我的页面将重定向到PHP并显示回显。 How can I display the echo without being redirected to the PHP page? 如何在不重定向到PHP页面的情况下显示回声? So the echo should be displayed in the html page (where the html form is located). 所以回声应该显示在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!';
} 
?>

You can't do it on the same page - at least not without asynchronous client-server communication techniques such as AJAX. 您无法在同一页面上执行此操作 - 至少在没有异步客户端 - 服务器通信技术(如AJAX)的情况下也是如此。 Otherwise, you can use the following: 否则,您可以使用以下内容:

<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. $ _SERVER ['PHP_SELF']包含对当前请求页面的引用,因此即使重命名也始终设置为正确的页面。 This can save you time when bug-tracking. 这可以节省您跟踪错误的时间。

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>
<?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。

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

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

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

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