2011-04-06 119 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; ?> 
相關問題