我想制作一个表单,在该表单中,用户输入名称以创建新记录,并希望同时检查(如果该记录存在或不存在),因此该用户无法插入2个具有相同名称的记录(但这不是主键) 。我现在正在使用以下代码,但是每次我需要检查输入的值时都需要重新加载页面
<html>
<?php
require_once('../php/classes/class.add_album.php');
$file_name='';
$file_not_found="NULL";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$file_name = test_input($_POST["file_name"]);
echo $file_name; //for testing
//make new object
$objfile = new add_album();
//call object methid with post method value we got and save result in result
$file_not_found=$objfile->find_album($file_name);
echo $file_not_found; //for testing
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if($file_not_found){
echo '<form>';
echo '<label>File Name </label>';
echo '<input type="text" value='.$file_not_found.' >';
} else {
echo '<form method="POST" action="temp.php">';
echo '<label>File Name</label>';
echo "<input type=text name='file_name' placeholder='New file name plz' >";
echo"<input type=submit name=submit value=submit>";
}
?>
</form>
`
按照我上面提到的javascript ajax和相同的php文件进行操作。
引用此链接获取ajax http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
function validateUser()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
//alert(xmlhttp.responseText);
// do somting here..
document.getElementById("captcha_validation").value=xmlhttp.responseText;
}
}
xmlhttp.open("POST","yourfile.php",true);
xmlhttp.send("username=abc&password=xyz");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句