如果提交表单,如何在同一页面上显示消息 [英] How to display a message on the same page if a form is submitted

查看:83
本文介绍了如果提交表单,如何在同一页面上显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php页面,其中包含带有文本框和提交按钮的表单,如果有人在文本框中输入"hello"并提交,我想显示任何消息.我在同一页面上制作了一个包含php和html的相似页面,但是当我加载php页面时,页面加载时会显示问候消息,我希望它显示它,然后我提交表单.以下是我的代码.

I have a php page which contains a form with a text box and a submit button, i want to display any message if someone enters ''hello'' in the textbox and submits it. I have made a similar page with both php and html on the same page, but when i load the php page, it shows the hello message as the page loads and i want it to display it then i submit the form. Following is my code.

if($_POST['txt'] == 'hi')
{
echo helo;	
}
else 
echo what;




< html xmlns ="http://www.w3.org/1999/xhtml">
< head>
< meta http-equiv ="Content-Type" content ="text/html; charset = utf-8"/>
< title>无标题的文档</title>
</head>

< body>
< form action ="this.php" method ="post">
<输入type ="text" name ="txt" value ="/>
<输入type ="submit" value =提交"/>
</body>
</html>

此页面在页面加载时显示什么"消息,而我不想先加载. php和html代码都在同一页面上




<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="this.php" method="post">
<input type="text" name="txt" value="" />
<input type="submit" value="submit" />
</body>
</html>

This page displays the ''what'' message as the page loads which i don''t want to load first. Both php and html code is on the same page

推荐答案

_POST [' ' echo helo; } 其他 echo 什么;
_POST['txt'] == 'hi') { echo helo; } else echo what;




< html xmlns ="http://www.w3.org/1999/xhtml">
< head>
< meta http-equiv ="Content-Type" content ="text/html; charset = utf-8"/>
< title>无标题的文档</title>
</head>

< body>
< form action ="this.php" method ="post">
<输入type ="text" name ="txt" value ="/>
<输入type ="submit" value =提交"/>
</body>
</html>

此页面在页面加载时显示什么"消息,而我不想先加载. php和html代码都在同一页面上.




<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="this.php" method="post">
<input type="text" name="txt" value="" />
<input type="submit" value="submit" />
</body>
</html>

This page displays the ''what'' message as the page loads which i don''t want to load first. Both php and html code is on the same page


有很多方法可以做到这一点.您只需要在发布前以及发布后告诉加载的页面即可.当然,您编写的内容在
There is a number of ways to do it. You simply need to tell the page loaded before post and as a result of the post. Of course what you write displays "what" as


_POST["txt"]返回空字符串时显示为"what",请看一下HTML.

一种非常简单的方法是使用不同的URL(带有URL查询字符串).假设您的页面URL是"http://mydomain.org/this.php".在您的表单中,使用:

_POST["txt"] returns empty string — look at your HTML.

One very simple method would be using different URLs — with URL query string. Suppose your page URL is "http://mydomain.org/this.php". In your form, use:

<form action="this.php?posted" method="post">



现在,您可以从使用原始URL加载的同一页面告诉发布的表单页面a:



Now you can tell the posted form page a from the same page loaded using original URL:

if (QUERY_STRING == ''posted'')
   echo "Thank you for posting your information";
else
   echo "Hello! Please fill in your data and press ''Post''";



—SA



—SA


这篇关于如果提交表单,如何在同一页面上显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆