在php中回显而不改变页面
我的网站上有一个简单的输入表单,供人们输入信息以提交。在他们不输入任何内容的情况下,代码看起来像这样:
这是 form.php
if ($_POST['q']) == NULL ){
echo "Please enter your information"
代码工作得很好,但它会将用户发送到带有 echo 的 form.php,我希望在我的主页索引上对此进行回显。 html 位于输入框正下方 - 基本上这样它就不会离开页面。这在 php 中可行还是我需要一些 javascript。我会寻找方法来做到这一点,但我不知道这个方法叫什么。
谢谢!
I have a simple input form on my site for people to enter in information for submission. The code looks like this in the case they do not enter anything:
this is form.php
if ($_POST['q']) == NULL ){
echo "Please enter your information"
The code works great, but it sends the user to form.php with the echo, where I want this to be echoed on my main page index.html right below the input box - basically so it doesn't navigate away from the page. Is this doable in php or will I need some javascript. I would have searched for ways to do this but I don't know what this method is called.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要在 url 中设置操作,它会提交给自己,如果仍然不起作用,您将需要重写规则。
dont set a action in the url and it will submit to its self, if that still wont work you will need rewrite rules.
如果您不想离开该页面,则需要使用 Javascript。将 onSubmit 添加到表单,然后让您调用的函数返回 false,如果输入不完整并且不应提交表单。
If you don't want to navigate away from the page you will need to use Javascript. Add a onSubmit to the form, and then let the function you call there return false, if the input is not complete and the form should not be submitted.
您可以使其回发到自身,然后如果输入字段下方有值 else echo,则将页面重定向到 post.php?q=value 。
you can make it postback to itsself and then redirect the page to post.php?q=value if there is a value else echo below the input field.
你可以使用 AJAX 来做这件事。当您不想重新加载页面以在 HTML 页面的特定位置或 Div 中执行任务时,AJAX 非常适合解决此类问题。
对于您的问题,您需要创建一个包含表单的 HTML 文件,并通过 AJAX 提交。并通过相同的 AJAX 获取您的响应。
这是第一页。现在根据需要在 PHP 文件(可能是 Submit_file.php)中使用您的脚本,然后通过验证或其他方式在 div 中回显您想要的文本。示例将是
希望您得到您想要的...
You can use AJAX for this thing. AJAX is great for this type of problems when you don't want to reload pages to do task in specific place or Div of HTMLpages.
For your problem, You need to create a HTML file with your form in it, and submit it via AJAX. and get your response via same AJAX.
This was the first page. Now Use your script in your PHP file(probably, submit_file.php) as you want and then echo the text you want in your div by validation or something.. a sample would be
Hope you got what you wanted...
最简单的方法是将错误消息分配给一个名为的变量(例如 $errorMsg)
使用回显或打印功能将其打印在页面上。
将此代码放在您希望错误出现的位置
The simplest way would be assigning the error message to a variable called (e.g $errorMsg)
the printing it on page using a echo or print function.
Place this code where you want the error to appear