在php中回显而不改变页面

发布于 2024-10-31 00:47:11 字数 334 浏览 3 评论 0原文

我的网站上有一个简单的输入表单,供人们输入信息以提交。在他们不输入任何内容的情况下,代码看起来像这样:

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼泪都笑了 2024-11-07 00:47:11

不要在 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.

闻呓 2024-11-07 00:47:11

如果您不想离开该页面,则需要使用 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.

不一样的天空 2024-11-07 00:47:11

您可以使其回发到自身,然后如果输入字段下方有值 else echo,则将页面重定向到 post.php?q=value 。

<?php
$qisempty = false;
if(!empty($_POST['q']))
{
    header("Location:http://../post.php?q=".$_POST['q']);
}
else
    $qisempty = true;
?>
    <input name="q" type="text">
    <?php if($qisempty) echo "Please enter your information";?>

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.

<?php
$qisempty = false;
if(!empty($_POST['q']))
{
    header("Location:http://../post.php?q=".$_POST['q']);
}
else
    $qisempty = true;
?>
    <input name="q" type="text">
    <?php if($qisempty) echo "Please enter your information";?>
别想她 2024-11-07 00:47:11

你可以使用 AJAX 来做这件事。当您不想重新加载页面以在 HTML 页面的特定位置或 Div 中执行任务时,AJAX 非常适合解决此类问题。

对于您的问题,您需要创建一个包含表单的 HTML 文件,并通过 AJAX 提交。并通过相同的 AJAX 获取您的响应。

<script type="text/javascript">
function submit_ajax(val1,val2,param1,param2,subfile){
var http = new XMLHttpRequest();
var url = subfile;
var params = val1+"="+param1+"&"+val2+"="+param2;
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
        //Remove the alert and use whatever you want to do with http.responsetext like
    // document.getElementById("YourDiv").innerHTML=document.getElementById("YourDiv").innerHTML +http.responsetext
    }
}
http.send(params);
}
</script>
</head>
<body>


<form>
<input type="text" id="user" value="user" name="user" />
<input type="password" id="password" value="pass" name="pass" />
<button onclick="submit_ajax(user.name,password.name,user.value,password.value, "submit_file.php");">Submit</button>
</form>

<div id="YourDiv">
<!--Something appears here after callback-->
</div>

这是第一页。现在根据需要在 PHP 文件(可能是 Submit_file.php)中使用您的脚本,然后通过验证或其他方式在 div 中回显您想要的文本。示例将是

<?php

$username=$_POST['user'];
$password=$_POST['pass'];

if(validateuser($username,$password)){
    if(checkuserpass($username,$password){
        echo "You were successfully logged in!";
    }
echo "Sorry Username/Password Mismatch";
}
else {
echo "There was an error in your input!Please Correct";
}
?>

希望您得到您想要的...

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.

<script type="text/javascript">
function submit_ajax(val1,val2,param1,param2,subfile){
var http = new XMLHttpRequest();
var url = subfile;
var params = val1+"="+param1+"&"+val2+"="+param2;
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
        //Remove the alert and use whatever you want to do with http.responsetext like
    // document.getElementById("YourDiv").innerHTML=document.getElementById("YourDiv").innerHTML +http.responsetext
    }
}
http.send(params);
}
</script>
</head>
<body>


<form>
<input type="text" id="user" value="user" name="user" />
<input type="password" id="password" value="pass" name="pass" />
<button onclick="submit_ajax(user.name,password.name,user.value,password.value, "submit_file.php");">Submit</button>
</form>

<div id="YourDiv">
<!--Something appears here after callback-->
</div>

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

<?php

$username=$_POST['user'];
$password=$_POST['pass'];

if(validateuser($username,$password)){
    if(checkuserpass($username,$password){
        echo "You were successfully logged in!";
    }
echo "Sorry Username/Password Mismatch";
}
else {
echo "There was an error in your input!Please Correct";
}
?>

Hope you got what you wanted...

北陌 2024-11-07 00:47:11

最简单的方法是将错误消息分配给一个名为的变量(例如 $errorMsg)
使用回显或打印功能将其打印在页面上。

<?php

    if ($_POST['q'])  == NULL ){
       $errorMsg =' Please enter your information';
    }

    ?>

将此代码放在您希望错误出现的位置

<? print $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.

<?php

    if ($_POST['q'])  == NULL ){
       $errorMsg =' Please enter your information';
    }

    ?>

Place this code where you want the error to appear

<? print $errorMsg; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文