当用户更改类别(选择标签更改)时,我正在尝试用数据库信息填写表单。 这是我的代码。
<form onsubmit='return false'>
select<select id="select">
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
</select>
fname<input type="text" id='fname' name="fname" /><br />
lname<input type="text" id='lname' name="lname" /><br />
</form>
<script type="text/javascript">
$(document).ready(function(){
$("#select").change(function(){
selectid = $(this).val();
// using post method to get firstname
$.post("retreive.php", {selectid:selectid}, function(result){
$("#fname").val();
//again using post method here get lastname
$.post("one.php", {selectid:selectid}, function(result){
$("#lname").val();
});
});
});
});
回味.php
<?php
//for firstname
if( isset($_POST['selectid']) ){
//using this selectid i'm getting firstname
echo $firstname;
exit();
}
//again for last name
if( isset($_POST['selectid']) ){
//using this selectid i'm getting firstname
echo $lastname;
exit();
}
?>
最后,此过程会自动填写表单字段,即名字和姓氏。我知道这是愚蠢和耗时的程序。我听说这项工作很容易完成,不需要让我们的 php 在使用 JSON 概念时承担所有负担。我试图理解,但不能。我希望你们能理解我面临的问题,希望有一个解决方案。提前感谢!
你被发出了 ajax 请求,但对结果数据不做任何操作。
试试这个
$(document).ready(function(){
$("#select").change(function(){
var selectid = $(this).val();
// using post method to get firstname
$.post("retreive.php", {selectid:selectid}, function(result){
$("#fname").val(result);
//again using post method here get lastname
$.post("one.php", {selectid:selectid}, function(result){
$("#lname").val(result);
});
});
});
});
您不需要两个 ajax 请求 - 您可以发出一个请求,并获取 json 数据。
阅读有关 jquery ajax 方法和 json 格式的信息
$(function(){
$('#select').change(function(){
var selectid = $(this).val();
$.ajax({
url: 'retreive.php',
data: {selectid: selectid},
type: 'POST',
dataType: 'JSON',
success: function(data)
{
$("#fname").val(result.fname);
$("#lname").val(result.lname);
}
});
});
});
和 php
<?php
if (isset($_POST['selectid'])) {
echo json_encode(array('fname' => $fname, 'lname' => $lname));
exit;
}
如果出现问题,当您发出 ajax 请求时,打开控制台并查找错误。如果您没有错误,请转到网络,看看哪个服务器返回您。