在php中将Ajax响应设置为null
我有一个表单,我正在尝试使用AJAX调用进行内联编辑和添加 首先,我在HTML表中显示数据。然后,若在文本框中输入数据并单击“添加”按钮,则记录在HTML表格中添加显示的数据。单击编辑按钮后,文本框中显示的数据会很好 但是我得到的ajax响应是在php中将Ajax响应设置为null,php,jquery,ajax,Php,Jquery,Ajax,我有一个表单,我正在尝试使用AJAX调用进行内联编辑和添加 首先,我在HTML表中显示数据。然后,若在文本框中输入数据并单击“添加”按钮,则记录在HTML表格中添加显示的数据。单击编辑按钮后,文本框中显示的数据会很好 但是我得到的ajax响应是null 我想不出来 这是我的AJAX代码PHP文件: $(function() { $(".scrollingTable tbody a").click(function() { //debugger; var link = $(
null
我想不出来
这是我的AJAX代码PHP文件:
$(function() {
$(".scrollingTable tbody a").click(function() {
//debugger;
var link = $(this).attr('href');
var arr = link.split('=');
var id = arr[1];
//alert(id);
$.ajax({
url: "insertgr.php",
type: "POST",
data: {
cntid: id
},
success: function(datas) {
var data = $.parseJSON(datas);
$("#num").val(data.id);
$("#namegr").val(data.vndr_cntname);
$("#designation").val(data.designation);
$("#mobilegr").val(data.vndr_cntmobile);
$("#maildgr").val(data.vndr_cntmail);
}
});
});
});
$(function() {
$('.txtcbt a').click(function() {
debugger;
var cntname, designation, mobile, email, vndrid, id, cid;
cid = $("#num").val();
cntname = $("#namegr").val();
designation = $("#designation").val();
mobile = $("#mobilegr").val();
email = $("#maildgr").val();
vndrid = "<?php echo $selectid; ?>";
//alert(cid);
if (cntname == "" || designation == "" || mobile == "" || email == "") {
alert("fields should not be empty");
} else {
$.ajax({
url: "insertgr.php",
type: "POST",
data: {
id: cid,
name: cntname,
dgnation: designation,
mobileno: mobile,
emailid: email,
vid: vndrid
},
success: function(html) {
var dat = $.parseJSON(html);
alert(html);
alert("it came to success");
$("#num").val("");
$('#namegr').val("");
$('#designation').val("");
$('#mobilegr').val("");
$('#maildgr').val("");
}
});
}
});
});
$(函数(){
$(“.scrollingTable tbody a”)。单击(函数(){
//调试器;
var link=$(this.attr('href');
var arr=link.split('=');
var id=arr[1];
//警报(id);
$.ajax({
url:“insertgr.php”,
类型:“POST”,
数据:{
cntid:id
},
成功:功能(数据){
var data=$.parseJSON(数据);
$(“#num”).val(data.id);
$(“#namegr”).val(data.vndr#cntname);
$(“#名称”).val(数据名称);
$(“#mobilegr”).val(data.vndr#cntmobile);
$(“#maildgr”).val(data.vndr#cntmail);
}
});
});
});
$(函数(){
$('.txtcbt a')。单击(函数(){
调试器;
变量名称、名称、手机、电子邮件、vndrid、id、cid;
cid=$(“#num”).val();
cntname=$(“#namegr”).val();
名称=$(“#名称”).val();
mobile=$(“#mobilegr”).val();
email=$(“#maildgr”).val();
vndrid=“”;
//警报(cid);
如果(cntname==“”| | designation==“”| | mobile==“”| | email==“”){
警报(“字段不应为空”);
}否则{
$.ajax({
url:“insertgr.php”,
类型:“POST”,
数据:{
id:cid,
姓名:姓名,,
国家:名称,
mobileno:mobile,
emailid:email,
视频:vndrid
},
成功:函数(html){
var dat=$.parseJSON(html);
警报(html);
警惕(“它成功了”);
$(“#num”).val(“”);
$('#namegr').val(“”);
$('名称').val(“”);
$('mobilegr').val(“”);
$('maildgr').val(“”);
}
});
}
});
});
这是AJAX正在调用的文件:
<?php
require('Assests/connection/connection.php');
error_reporting(0);
$vcntlist = "";
if (!empty($_POST['cntid'])) {
$id = $_POST['cntid'];
$result = mysqli_query($conn, "SELECT `id`, `vndr_cntname`, `designation`, `vndr_cntmobile`, `vndr_cntmail`,`vndr_id` FROM `vndr_cntdtls`where id=$id");
$rowcount = mysqli_num_rows($result);
if ($rowcount > 0) {
$row = mysqli_fetch_array($result);
$vcntid = $row['id'];
$cntname = $row['vndr_cntname'];
$cntdesignation = $row['designation'];
$cntmobile = $row['vndr_cntmobile'];
$cntmail = $row['vndr_cntmail'];
}
}
if (!empty($_POST['name']) && !empty($_POST['dgnation']) &&
!empty($_POST['mobileno']) && !empty($_POST['emailid']) &&
!empty($_POST['vid'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$degination = $_POST['dgnation'];
$mobile = $_POST['mobileno'];
$email = $_POST['emailid'];
$vndrid = $_POST['vid'];
if (empty($_POST['id'])) {
$query = mysqli_query($conn, "INSERT INTO `vndr_cntdtls`(`vndr_cntname`, `designation`, `vndr_cntmobile`, `vndr_cntmail`, `vndr_id`) VALUES ('$name','$degination','$mobile','$email',$vndrid)");
} else {
$update1 = mysqli_query($conn, "UPDATE `vndr_cntdtls` SET `vndr_cntname`='$name',`designation`='$degination',`vndr_cntmobile`='$mobile',`vndr_cntmail`='$email' WHERE id=$id") or die(mysqli_error($conn));
}
$result = mysqli_query($conn, "SELECT DISTINCT `id`, `vndr_cntname`, `designation`, `vndr_cntmobile`, `vndr_cntmail`,vc.vndr_id FROM `vndr_cntdtls` vc INNER JOIN vendors v ON vc.vndr_id=$vndrid") or die(mysqli_error($conn));
$rowcount = mysqli_num_rows($result);
if ($rowcount > 0) {
while ($row = mysqli_fetch_array($result)) {
$vcntid = $row['id'];
$cntname = $row['vndr_cntname'];
$cntdesignation = $row['designation'];
$cntmobile = $row['vndr_cntmobile'];
$cntmail = $row['vndr_cntmail'];
}
}
}
echo json_encode($row);
?>
它可能有很多不起作用的方式 第一名: 您需要在php中放置一个
应用程序/json
,以便它与您正在使用的浏览器兼容
header('Content-Type: application/json');
echo json_encode($row);
秒:
为什么要执行while循环并将其分配到未使用的变量中
您可以通过执行以下操作来简化它:
$row = mysqli_fetch_array($result);
header('Content-Type: application/json');
echo json_encode($row);
除非是多行,否则:
$rowcount = mysqli_num_rows($result);
$rows = array();
if ($rowcount > 0) {
while ($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
}
header('Content-Type: application/json');
echo json_encode($row);
第三
null
值通常在您尝试使用的变量未初始化时出现。关闭错误报告后,不会显示错误
错误报告(true)代码>
第四
还要检查日志中的数据库错误,我认为这与MySQL查询不必到达$row
初始化有关
第五名
我相信您需要将其作为关联数组获取,这样它才有用
$row=mysqli\u fetch\u assoc($result)代码>没有看到完整的PHP文件。由于某种原因,它在本地PHP服务器或格式化程序中无法运行。两者都崩溃了。你介意自己检查一下吗?我相信代码有一个很大的问题。。。试图在一个无关的注释上调试……,你用SQL查询字符串来插入PHP变量,这会在你的应用程序中产生一个不需要的攻击向量,称为SQL注入,为了防止这种情况发生,我建议使用预先准备好的语句:如果你也想要html文件。我能做些什么来解决这个问题。@我只是试图清除PHP中的混乱,而不修改代码。检查并批准。@itsme控制台中的输出是什么?