我一直在尝试使用Jquery和下拉框来填写输入表单,但没有成功,我正在努力找到解决这个问题的方法,直到我最终找到它。我想知道的是,如果可以用sql填充下拉列表,那么一旦选择,就取该值并运行sql命令,将数据行的其余部分和$_get输入到输入字段中。
我一直在查看示例,但大多数示例都输出表数据,找不到任何可以拉到输入字段的内容,所以我可以将表单处理到数据库中。
我只是在寻找一个想法或方向,我应该在哪里寻找这样的任务,任何帮助都将不胜感激。
好吧,通过使用JQuery库并编写自己的PHP功能,您可以轻松做到这一点。
您需要和HTML选择框:
<select id="myselectbox">
<option>Select</option>
</select>
<input type="text" id="myinputbox">
JQueryAjax请求获取选择框的数据,当您的Web文档准备就绪时将触发该请求,并将选择框的Onchange事件与POST类型为的其他Ajax请求一起触发
<script type="text/javascript">
$( document ).ready(function() {
$.get( "myfunctions/get_records_for_select.php", function( data ) {
//Look we will change the HTML content for our select-box on success
$( "#myselectbox" ).html( data );
});
$( "#myselectbox" ).change(function() {
$.post( "myfunctions/get_records_by_id.php", { selectedId: $(this).val() })
.done(function( data ) {
$( "#myinputbox" ).val( data );
});
});
});
</script>
你的。将接收AJAX请求的PHP文件:
// your get_records_for_select.php file
$yourSql = "select ...";
$results = run_sql($yourSql);
$html = "<option>Select</option>";
foreach($results as $record){
$html .= "<option value='".$record['id']."'>".$record['title']."</option>";
}
echo $html;exit;
你的第二个。将接收POST TYPE AJAX请求的PHP文件:
// your get_records_by_id.php file
$post_data $_POST['selectedId'];
$yourSql = "select ... WHERE id=".$post_data;
$results = run_sql($yourSql);
$row = $results->row();
echo $row['yourvalue'];exit;
我希望现在你能有一些想法:)
TEST.php
<?
if(isset($_GET['name'])) {
/*Some queries if need*/
echo '{"name":"yiu send name ['.$_GET['name'].'] and may be some other data'.'"}';
}
else {
$bd_host='localhost';
$bd_user='root';
$bd_password='';
$bd_base='db_name';
/*connect to db*/
$con=mysql_connect($bd_host, $bd_user, $bd_password);
/*control connection*/
if(!con) {
echo 'Error.';
exit;
}
/*select databese name*/
mysql_select_db($bd_base, $con);
/*set encode to utf-8*/
mysql_query('SET NAMES utf8');
$sql="Select name from users limit 10"
$res=mysql_query($sql);
?>
<head>
<style>
div{background-color:red;width:150px;color:white;}
ul{display:none;position:absolute;width:150px;margin:0;padding:0;background-color:yellow;color:black;}
div:hover ul{display:block;}
.filled{background-color:blue;}
</style>
</head>
<div>button<ul>
<?
While($row=mysql_fetch_assoc($res)) { ?>
<li onclick="ajax('<? echo $row['name']; ?>');return false;"><? echo $row['name']; ?></li>
<?
}
?>
</ul></div>
<input type="text" value="none" id="id1">
<?
}
?>
<script>
function ajax(name) {
var url='/test.php?name='+name;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
myfunction(xmlhttp.responseText);
}
}
xmlhttp.send(null );
function myfunction(response) {
var= arr;
arr = JSON.parse(response);
document.getElementById('id1').value=arr.name;
document.getElementById('id1').className='filled';
alert ('The input was filled with '+arr.name);
}
}
</script>
/*END of test.php*/