AJAX代码没有';我不能用PHP工作
JS和HTML:AJAX代码没有';我不能用PHP工作,php,jquery,ajax,Php,Jquery,Ajax,JS和HTML: <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> function load_bkissue(accessid){ //alert(accessid); $.ajax({ type: "POST", url: "2.php
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function load_bkissue(accessid){
//alert(accessid);
$.ajax({
type: "POST",
url: "2.php",
async: false,
dataType: "json",
data: "accession_id=" + accessid,
success: function (response) {
$("#bkid").val(response.accession_id);
$("#txtbnam").val(response.title);
$("#txauth").val(response.author);
$("#txtcat").val(response.category);
$("#txtsub").val(response.subject_type);
//$("#txt_div").val(response.rack_no);
$("#txtrack").val(response.rack_no);
}
});
</script>
</head>
<body>
<table width="260" id="tab" cellpadding="1" cellspacing="10">
<tr>
<td width="20px"> AccessionNo </td>
<td width="400px">
<select name="bkid" onChange="load_bkissue(this.value)" class="txtbox">
<option value="1">1 </option>
<option value="2">2 </option>
<option value="3">3 </option>
<option value="4">4 </option>
<option value="5">5 </option>
</select> </td>
</tr>
<tr>
<td> Title </td>
<td><input name="txtbnam" id="txtbnam" type="text" class="txtbox"></td>
</tr>
<tr>
<td> Author </td>
<td><input name="txauth" id="txauth" type="text" id="txauth" class="txtbox"></td>
</tr>
<tr>
<td> Category </td>
<td><input name="txtcat" id="txtcat" type="text" id="txtcat" class="txtbox"></td>
</tr>
<tr>
<td> Subject </td>
<td><input name="txtsub" id="txtsub" type="text" id="txtsub" class="txtbox"></td>
</tr>
<tr>
<td> Rack </td>
<td><input name="txtrack" id="txtrack" type="text" id="txtrack" class="txtbox"></td>
</tr>
</table>
</body>
</html>
函数加载问题(accessid){
//警报(accessid);
$.ajax({
类型:“POST”,
url:“2.php”,
async:false,
数据类型:“json”,
数据:“访问id=”+accessid,
成功:功能(响应){
$(“#bkid”).val(回复.登录号);
$(“#txtbnam”).val(response.title);
$(“#txauth”).val(response.author);
$(“#txtcat”).val(response.category);
$(“#txtsub”).val(response.subject_type);
//$(“#txt_div”).val(response.rack_no);
$(“#txtrack”).val(response.rack_no);
}
});
附加号
1.
2.
3.
4.
5.
标题
作者
类别
主题
架子
PHP:
当选择访问号时,我需要在每个字段(如标题、作者等)中包含这些值。但这不起作用。您的JSON…很奇怪。为什么不这样做:
<?php
$json = array(
'accession_id' => "a",
'title' => "b",
'author' => "c",
'category' => "d",
'subject_type' => "e",
'rack_no' => "f");
echo json_encode($json);
?>
并使您的javascript更加简单:
$.ajax({
type: "POST",
url: "http://temp.lmfast1/testajax/2.php",
async: false,
dataType: "json",
data: "accession_id=" + accessid,
success: function (response) {
$("#bkid").val(response.accession_id);
$("#txtbnam").val(response.title);
$("#txauth").val(response.author);
$("#txtcat").val(response.category);
$("#txtsub").val(response.subject_type);
//$("#txt_div").val(response.rack_no);
$("#txtrack").val(response.rack_no);
}
});
您的ajax调用缺少一些重要内容。请尝试此调用
function load_bkissue(accessid) {
var myID = { accession_id : accessid };
var DTO = JSON.stringify(myID);
$.ajax({
type: "POST",
url: "2.php", // /testajax/2.php
contentType: "application/json",
dataType: "json",
data: DTO,
success: function(response) //'response' is the output provided by the controller method
{
alert(response);
$.each(response, function(i, item) {
if (item.field == "accession_id") {
$("#bkid").val(item.value);
} else if (item.field == "title") {
$("#txtbnam").val(item.value);
} else if (item.field == "author") {
$("#txauth").val(item.value);
} else if (item.field == "category") {
$("#txtcat").val(item.value);
} else if (item.field == "subject_type") {
$("#txtsub").val(item.value);
} else if (item.field == "rack_no") {
//$("#txt_div").val(item.value);
$("#txtrack").val(item.value);
}
});
}
});
}
这些是我在代码中看到的问题
contentType
数据类型:“json”
,但您的数据不是json类型你真的需要提供比“它不起作用”更多的信息。不起作用,请描述得更详细一点。什么也没有发生。。这意味着文本字段中不会显示值,如标题、与访问相对应的作者NodeId您忘了在1.php中包含jquery?我在1.phpy中包含jquery.js您的意思是response.title、response.author等。@cwallenpole:是的,复制/粘贴错误,我会修复。@Ashitha:do yo你看到你编写的警报框了吗?如果没有,那么它甚至没有成功地执行AJAX调用…我删除了警报框..但是现在没有出现任何changealert。执行2.php时,第4行显示一个错误解析错误:语法错误,意外','in/home/temp/workspace/testajax/2.php
$.ajax({
type: "POST",
url: "http://temp.lmfast1/testajax/2.php",
async: false,
dataType: "json",
data: "accession_id=" + accessid,
success: function (response) {
$("#bkid").val(response.accession_id);
$("#txtbnam").val(response.title);
$("#txauth").val(response.author);
$("#txtcat").val(response.category);
$("#txtsub").val(response.subject_type);
//$("#txt_div").val(response.rack_no);
$("#txtrack").val(response.rack_no);
}
});
function load_bkissue(accessid) {
var myID = { accession_id : accessid };
var DTO = JSON.stringify(myID);
$.ajax({
type: "POST",
url: "2.php", // /testajax/2.php
contentType: "application/json",
dataType: "json",
data: DTO,
success: function(response) //'response' is the output provided by the controller method
{
alert(response);
$.each(response, function(i, item) {
if (item.field == "accession_id") {
$("#bkid").val(item.value);
} else if (item.field == "title") {
$("#txtbnam").val(item.value);
} else if (item.field == "author") {
$("#txauth").val(item.value);
} else if (item.field == "category") {
$("#txtcat").val(item.value);
} else if (item.field == "subject_type") {
$("#txtsub").val(item.value);
} else if (item.field == "rack_no") {
//$("#txt_div").val(item.value);
$("#txtrack").val(item.value);
}
});
}
});
}