javascript - 在 PHP 和 Ajax 中使用 Json 传递值
问题描述
我是初学者Json
。中包含一些值HTML table
,需要使用json
到prescription.php
页面传递这些值。我试过了,但ajax
成功后它会在错误日志中显示一条错误消息:
e = {readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
这是我的HTML
代码段:
<form method="post" id="prescriptionn" enctype="multipart/form-data">
<div class="table-responsive">
<table class="table table-bordered mb-0" id="medical">
<thead>
<tr>
<th>Medicine Name</th>
<th>Morning</th>
<th>Noon</th>
<th>Night</th>
<th> <button type="button" name="add" id="add"
class="btn btn-success btn-xs"> + </button> </th>
</tr>
</thead>
<tbody id="rows">
</tbody>
</table>
<br><br>
<div align="center">
<input type="hidden" value="<?php echo $row['apt_id'] ?>"
id="getapt" name="getapt" class="btn btn-primary">
<input type="hidden" value="<?php echo $row['p_id'] ?>"
id="getpid" name="getpid" class="btn btn-primary">
<input type="button" name="submit"
id="submit" class="btn btn-primary" value="Enter Prescription">
</div>
</div>
</form>
这是我的ajax
电话
<script>
$(document).ready(function(){
var count=0;
$(document).on('click','#add',function() {
count++;
var html= '';
html += '<tr>';
html += '<td id="medicinename"> <select name="med_name[]" id="med_name[]" class="form-control med_name" ><option value=""> Select Medicine</option> <?php echo fill_select_box($conn, "0"); ?></select></td>';
html += '<td id="mor"> <input type="text" name="morning[]" id="morning[]" class="form-control morning" /> </td>';
html += '<td id="noo"> <input type="text" name="noon[]" id="noon[]" class="form-control noon" /> </td>';
html += '<td id="nigh"> <input type="text" name="night[]" id="night[]" class="form-control night" /> </td>';
// html += '<td class="charge"> </td>';
html += '<td> <button type="button" name="remove" id="remove" class="btn btn-danger btn-xs remove" > - </button> </td>';
html += '</tr>';
$('#rows').append(html);
});
});
</script>
<script>
$(document).ready(function () {
$(document).on('click', '#submit', function () {
var getapt = $('#getapt').val();
var getpid = $('#getpid').val();
var ids={
'getapt': getapt,
'getpid': getpid,
}
var modess = $('#rows tr').map(function() {
let $tr = $(this);
return [ {
"medname": $(this).find('.med_name').val(),
"morning":$(this).find('.morning').val(),
"noon":$(this).find('.noon').val(),
"night": $(this).find('.night').val(),
} ]
console.log(modess);
});
var ids = JSON.stringify(ids);
var medical = JSON.stringify(modess);
$.ajax({
url: "adminquery/prescription.php", // Url to which the request is send
method: "POST", // Type of request to be send, called as method
data:{
index1: medical,
index2: ids
},
dataType:'json',
cache: false,
success: function(data){
alert('Items added');
},
error: function(e){
console.log(e);
}
})
});
});
</script>
这是页面中的php
代码prescription.php
<?php
session_start();
require_once "../auth/dbconnection.php";
// if (isset(json_decode($_POST["data"])) {
$jsondata = json_decode($_POST["index1"]);
$jsondata1 = json_decode($_POST["index2"]);
echo $id= $jsondata1->getpid;
echo $apt= $jsondata1->getapt;
if($stmt = mysqli_prepare($conn,"INSERT INTO prescription (apt_id,user_id,p_id, med_records,date) VALUES (?, ?, ?, ?, ?)")){
$cur_date = date('Y-m-d H:i:s');
$user_id= $_SESSION['user_id'];
// $p_id= $_POST['getpid'];
// $apt_id= $_POST['getapt'];
mysqli_stmt_bind_param($stmt, "sssss",$apt,$user_id, $id,$jsondata,$cur_date);
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not prepare query: $sql. " . mysqli_error($conn);
}
mysqli_stmt_close($stmt);
?>
我不知道我哪里错了。非常感谢任何帮助。
解决方案
根据我在您的 PHP 代码中看到的内容,我了解您希望存储 JSON 的文本, index1
因为它直接位于 sql 元素下,并且您希望在尝试解码并提取两个值med_records
时将 PARSE 作为对象发送:index2
$jsondata1 = json_decode($_POST["index2"]);
echo $id= $jsondata1->getpid;
echo $apt= $jsondata1->getapt;
因此,您应该只解码其中的一个并将第二个保留为字符串:
$jsondata = $_POST["index1"];
因为$jsondata
你想在数据库中存储 json 字符串,对吧?如果不是,请提供一些有关预期数据的信息(表格的模式)。
一定要知道你的代码不是很安全:首先它应该验证是否发送了正确的值(解码解析的数据,检查预期的密钥,再次编码),还应该为没有用户的情况做好准备会话中的 id 了。
推荐阅读
- html - 为什么 IE 会切断文字宽度,但在其他浏览器中却没有
- c++ - 排序函数在 C++ 中无法按预期工作
- java - 当用户单击它时如何从 WebView 内的链接中提供 url
- android - BLE 通知冻结所有其他活动读取特性
- c# - 在c#中设置选项卡内的文本值
- spring - Spring Boot + JPA + JAR 文件失败,并在类路径资源中定义名称为“entityManagerFactory”的 bean 创建错误
- caching - Lauterbach trace32:如何在缓存区域使用断点
- php - 来自静态视频的假直播 m3u8 流
- java - 无法使用 java 连接到 Azure 服务总线主题
- java - 比较时间戳时的 Firestore 查询问题