我试图用jQuery ajax发布数据,但是它只将一半的数据发布到下一页。
我可以看到所有数据都被发送到chrome开发人员工具中的下一页,但是其发送到的页面的响应不成功。
这是我的JS代码:
var id = $(this).parents('tr').attr('data-id');
var time = $('#time').val();
var mon = $('#mon').val();
var tue = $('#tue').val();
var wed = $('#wed').val();
var thu = $('#thu').val();
var fri = $('#fri').val();
var sat = $('#sat').val();
$.ajax({
type: "POST",
url: "update.php",
data: {
"id": id,
"time": time,
"mon": mon,
"tue": tue,
"wed": wed,
"thu": thu,
"fri": fri,
"sat": sat
},
dataType: "json",
success: function(response)
{
if(!response.error) {
console.log(response.msg);
} else {
console.log(response.msg);
}
}
});
和update.php代码:
if ( isset($_POST) ) {
if( isset($_POST['id'] ) && $_POST['id'] > 0 && !$error) {
$id = $_POST['id'];
$error = false;
} else {
$error = true;
}
if( isset($_POST['time'] ) && $_POST['time'] > 0 && !$error) {
$time = $_POST['time'];
$error = false;
} else {
$error = true;
}
if( isset($_POST['mon'] ) && $_POST['mon'] > 0 && !$error) {
$mon = $_POST['mon'];
$error = false;
} else {
$error = true;
}
if( isset($_POST['tue'] ) && $_POST['tue'] > 0 && !$error) {
$tue = $_POST['tue'];
$error = false;
} else {
$error = true;
}
if( isset($_POST['wed'] ) && $_POST['wed'] > 0 && !$error) {
$wed = $_POST['wed'];
$error = false;
} else {
$error = true;
}
if( isset($_POST['thu'] ) && $_POST['thu'] > 0 && !$error) {
$thu = $_POST['thu'];
$error = false;
} else {
$error = true;
}
if( isset($_POST['fri'] ) && $_POST['fri'] > 0 && !$error) {
$fri = $_POST['fri'];
$error = false;
} else {
$error = true;
}
if( isset($_POST['sat'] ) && $_POST['sat'] > 0 && !$error) {
$sat = $_POST['sat'];
$error = false;
} else {
$error = true;
}
if( !$error ) {
$query = "UPDATE table
SET time = '$time',
monday = '$mon',
tuesday = '$tue',
wednesday = '$wed',
thursday = '$thu',
friday = '$fri',
saturday = '$sat'
WHERE scheduleID = '$id'";
$result = mysqli_query($conn, $query) or die("database error:".
mysqli_error($conn));
$msg = array('error' => $error, 'msg' => 'Success! updation in mysql');
} else {
$msg = array('error' => $error, 'msg' => 'Failed! updation in mysql');
}
}
echo json_encode($msg);
和html(使用Metronic管理主题中的可编辑表格)
<div class="table-toolbar">
<div class="row">
<div class="col-md-12">
<div class="btn-group">
<button id="sample_editable_1_new" class="btn green"> Add New
<i class="fa fa-plus"></i>
</button>
</div>
</div>
</div>
</div>
<table class="table table-striped table-hover table-bordered" id="sample_editable_1">
<thead>
<tr>
<th>Time</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr data-id="5">
<td class="time">05:30</td>
<td class="mon">Program Name</td>
<td class="tue">Program Name</td>
<td class="wed">Program Name</td>
<td class="thu">Program Name</td>
<td class="fri">Program Name</td>
<td class="sat"></td>
<td>
<a class="edit" href="javascript:;"> Edit </a>
</td>
<td>
<a class="delete" href="javascript:;"> Delete </a>
</td>
</tr>
</tbody>
只有&#34; id&#34;而且&#34;时间&#34;在update.php文件中收到。
我尝试过添加contentType和JSON stringify但是在update.php没有收到任何数据。
我做错了什么?任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
答案 1 :(得分:0)
您也可以尝试
<?php
if ( isset($_POST) ) {
extract($_POST);
if ( !isset($id) || empty($id) ) {
echo json_encode(array('error' => true, 'msg' => 'missing [id]'));exit;
}
if ( !isset($time) || empty($time) ) {
echo json_encode(array('error' => true, 'msg' => 'missing [time]'));exit;
}
if ( !isset($mon) || empty($mon) ) {
echo json_encode(array('error' => true, 'msg' => 'missing [mon]'));exit;
}
if ( !isset($tue) || empty($tue) ) {
echo json_encode(array('error' => true, 'msg' => 'missing [tue]'));exit;
}
if ( !isset($wed) || empty($wed) ) {
echo json_encode(array('error' => true, 'msg' => 'missing [wed]'));exit;
}
if ( !isset($thu) || empty($thu) ) {
echo json_encode(array('error' => true, 'msg' => 'missing [thu]'));exit;
}
if ( !isset($fri) || empty($fri) ) {
echo json_encode(array('error' => true, 'msg' => 'missing [fri]'));exit;
}
if ( !isset($sat) || empty($sat) ) {
echo json_encode(array('error' => true, 'msg' => 'missing [sat]'));exit;
}
$query = "UPDATE table
SET time = '$time',
monday = '$mon',
tuesday = '$tue',
wednesday = '$wed',
thursday = '$thu',
friday = '$fri',
saturday = '$sat'
WHERE scheduleID = '$id'";
$result = mysqli_query($conn, $query) or die("database error:". mysqli_error($conn));
echo json_encode(array('error' => false, 'msg' => 'Update success'));exit;
}else{
echo json_encode(array('error' => true, 'msg' => 'missing [POST]data'));exit;
}
?>
...在提交数据后捕获确切的错误。
正如 tima_t 已经说过,请检查 #mon , #tue ...输入的值。