2011-04-06 115 views
1

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

这是form.php的

if ($_POST['q']) == NULL){ 
    echo "Please enter your information" 

代码的伟大工程,但它发送给用户的回声,以form.php的,我希望在主页面index.html中的输入框下方回显 - 基本上它不会离开页面。这是可行的PHP或将我需要一些JavaScript。我会寻找办法做到这一点,但我不知道这种方法被称为。

谢谢!

回答

0

不要在url中设置一个动作,它会提交给它自己,如果仍然不行,你需要重写规则。

0

你可以将它回发给自己,然后将页面重定向到post.php?q = value如果在输入字段下有一个else else回显。

<?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";?> 
+0

我需要在我的HTML文件中创建一个文本字段张贴此? – user695653 2011-04-06 21:42:17

+0

不需要的是设置一个标志,然后像这样 – 2011-04-06 21:48:00

+0

告诉我,这是否有帮助 – 2011-04-06 21:57:37

0

如果您不想离开页面,您将需要使用Javascript。向表单添加onSubmit,然后让您调用的函数返回false,如果输入未完成且不应提交表单。

+0

有没有一个关于如何完成这个在线的好点? – user695653 2011-04-06 21:47:10

+0

@ user695653 http://www.thesitewizard.com/archive/validation.shtml,然后你可以输入一些文本到一个div,p或任何与给定的ID像这样:http://www.tizag.com/javascriptT /javascript-getelementbyid.php(而不是警报) – Tommy 2011-04-06 21:52:41

+0

你不需要javascript主要是因为如果javascript被禁用会怎么样 – 2011-04-06 21:54:24

0

您可以使用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"; 
} 
?> 

希望你得到了你想要...

-1

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

<?php 

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

    ?> 

将此代码要出现错误

<? print $errorMsg; ?> 
相关问题