将值设置为隐藏字段
Set value to a hidden field
我有一个即时搜索程序,它是从教程中收集的。我修改了一些代码行。这是文件:
<!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文件并创建帐户。这里使用了两种形式,这也造成了问题。
请帮我做这项工作。提前谢谢。
您需要从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);
}
}
相关文章:
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- 带有验证和隐藏字段值的提交按钮
- 主干窗体隐藏字段未呈现
- 清除以前的$_GET值或不获取仅隐藏字段的值
- 添加和删除隐藏字段数组中的值,而不提交表单
- 在控制器上使用“$watch”时,为什么不更新此隐藏字段
- 将选项值附加到隐藏字段
- JQuery获取隐藏字段的值
- MVC+访问控制器中的隐藏字段
- ASP.net Javascript函数中的隐藏字段为null
- 在第一页加载时隐藏字段,而不是在php发布之后
- 表单使用js、html,而不是隐藏字段
- 如何根据文本长度立即显示和隐藏字段?-JQuery
- 在选择更改时显示大于/小于的隐藏字段集
- 如何获取多个复选框值并分配给隐藏字段
- 将会话登录页存储在cookie中以进行表单解析(通过隐藏字段)
- 将布尔值存储在隐藏字段中以进行表单处理
- 如何将多个/不同的隐藏字段插入 XML
- 如何通过 JSP 代码将隐藏字段(Javascript)插入 XML
- 如何使用Java在Selenium WebDriver的隐藏字段中键入一些文本