您可以使用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";
}
?>
希望你得到了你想要...
我需要在我的HTML文件中創建一個文本字段張貼此? – user695653 2011-04-06 21:42:17
不需要的是設置一個標誌,然後像這樣 – 2011-04-06 21:48:00
告訴我,這是否有幫助 – 2011-04-06 21:57:37