我有一个从tutorial收集的即时搜索程序 。我修改了一些代码行。这是文件:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(".search_button").click(function() {
// getting the value that user typed
var searchString = $("#search_box").val();
// forming the queryString
var data = 'search='+ searchString;
// if searchString is not empty
if(searchString) {
// ajax call
$.ajax({
type: "POST",
url: "instant_search.php",
data: data,
beforeSend: function(html) { // this happens before actual call
$(".results").html('');
//$("#uname").value('');
//$("#searchresults").show();
$(".word").html(searchString);
},
success: function(html){ // this happens after we get results
$(".results").show();
$(".results").append(html);
$('#uname').value(html);
//document.getElementById('uname').value(html);
//$("#uname").value(html);
}
});
}
return false;
});
});
</script>
</head>
<body>
<?php echo '<center>';?>
<div class="header_box"><?php echo $f->SYSTEM_NAME; ?></div>
<?php
if($acc_type == 'admin'){ ?>
<h1>Create new admin account</h1>
<table>
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="id" size="20" class="text_box"/></td>
<td><input type="button" value="Check"></td>
</tr>
</table>
<?php
}else if($acc_type == 'student'){ ?>
<h1>.:: Create student's account ::.</h1>
<label style="font-size: 18px"><label style="color: red">*</label> Marked fields are must</label><br/><br/>
<!-- <form action="" method="post">-->
<table border="0">
<tr class="unimportant_text">
<td>Test Username</td>
<td>:</td>
<td>
<form method="post" action="instant_search.php">
<input type="text" name="search" id="search_box" class="unimportant_text"/>
<input type="submit" class="search_button" value="Check" style="background: #808080; color: white; border: none"/><br />
</form>
</td>
</tr>
<tr>
<td>Username<label style="color: red">*</label></td>
<td>:</td>
<td>
<label class="results" style="font-size: 20px; color: green; font-weight: bold"></label>
<input type="hidden" name="uname" id="uname"/>
</td>
</tr>
<tr>
<td>Full Name<label style="color: red">*</label></td>
<td>:</td>
<td><input type="text" name="name" class="text_box"/></td>
</tr>
<tr>
<td>Contact<label style="color: red">*</label></td>
<td>:</td>
<td><input type="text" name="name" class="text_box" /></td>
</tr>
<tr>
<td>Contact (Optional)</td>
<td>:</td>
<td><input type="text" name="name" class="text_box" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input type="text" name="name" class="text_box" /></td>
</tr>
<tr>
<td>Course<label style="color: red">*</label></td>
<td>:</td>
<td>
<select name="course">
<?php
$courses = $f->get_courses();
foreach($courses as $c){ ?>
<option value="<?php echo $c[1];?>"><?php echo $c[1];?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Address</td>
<td>:</td>
<td><input type="text" name="name" class="text_box" /></td>
</tr>
</table>
<input type="submit" value="Submit">
<!-- </form>-->
<?php
}
?>
<?php echo '</center>';?>
</body>
</html>
这是我的instant_search.php:
if (isset($_POST['search'])) {
$word = mysql_real_escape_string($_POST['search']);
$res = $f->select_name($word);
if(mysql_num_rows($res) > 0) {
//echo 'Not available, choose another one';
} else {
echo $word;
}
}
我想要的很简单。
我只想检查$word
是否在数据库中可用。如果没有,则将其设置为隐藏字段(uname)的值。然后将表单提交到另一个php文件并创建帐户。
这里正在使用两种形式,这也是一个问题。
请帮我做这份工作。提前谢谢。
答案 0 :(得分:1)
您需要从PHP返回特定代码并在AJAX调用的成功回调中对其进行测试。
instant_search:
if (isset($_POST['search'])) {
$word = mysql_real_escape_string($_POST['search']);
$res = $f - > select_name($word);
if (mysql_num_rows($res) > 0) {
//The word is not in DB, then specify error in front of it
echo '[error]'.$word;
} else {
echo $word;
}
}
成功回调:
success: function (html) { // this happens after we get results
if(html.search('[error]') >= 0)
{
//Error : set your input field with returned text
$('#uname').val(html.split('[error]')[1]);
//Call your second form here
}
else
{
//No error
$(".results").show();
$(".results").append(html);
}
}