我在网格中使用了jquery网格值查找值。值从数据库中检索。我使用了我的Sample静态数据,我以前的设计json格式。看起来像
$(document).ready(function() {
var jqgrid_data = [{
id : "1",
date : "2007-10-01",
name : "test",
note : "note",
amount : "200.00",
tax : "10.00",
total : "210.00"
}]
}
但我希望从数据库Json格式中检索值。我的代码看起来像这样
<div class="form-group">
<label for="pn">Title</label>
<input type="text" class="form-control" id="Title<?php echo $row['ID']; ?>" value="<?php echo $row['Title']; ?>" />
</div>
<div class="form-group">
<label for="al">Tag Line</label>
<input type="text" class="form-control" id="TageLine<?php echo $row['ID']; ?>" value="<?php echo $row['TagLine']; ?>" />
</div>
<div class="form-group">
<label for="al">Banner URL</label>
<input type="text" class="form-control" id="BannerURL<?php echo $row['ID']; ?>" value="<?php echo $row['BannerURL']; ?>" />
</div>
<div class="form-group">
<label for="al">Icon URL</label>
<input type="text" class="form-control" id="IconURL<?php echo $row['ID']; ?>" value="<?php echo $row['IconURL']; ?>" />
</div>
<div class="form-group">
<label for="al">Video Announcement URL</label>
<input type="text" class="form-control" id="VideoAnnouncementURL<?php echo $row['ID']; ?>" value="<?php echo $row['VideoAnnouncementURL']; ?>" />
get Data.php
</div>
<div class="form-group">
<label for="al">Video Description</label>
<input type="text" class="form-control" id="VideoDescription<?php echo $row['ID']; ?>" value="<?php echo $row['VideoDescription']; ?>" />
</div>
<div class="form-group">
<label for="al">Order</label>
<input type="text" class="form-control" id="Order<?php echo $row['ID']; ?>" value="<?php echo $row['Order']; ?>" />
</div>
</form>
这个值我发现我看起来像脚本
function viewdata() {
$.ajax({
type: "GET",
url: "inc/getdata.php"
}).done(function (data) {
$('#viewdata').html(data);
});
}
$('#save').click(function () {
var nm = $('#nm').val();
var gd = $('#gd').val();
var pn = $('#pn').val();
var al = $('#al').val();
var datas = "nm=" + nm + "&gd=" + gd + "&pn=" + pn + "&al=" + al;
$.ajax({
type: "POST",
url: "inc/newdata.php",
data: datas
}).done(function (data) {
$('#info').html(data);
viewdata();
});
});
function updatedata(str) {
var id = str;
var nm = $('#nm' + str).val();
var gd = $('#gd' + str).val();
var pn = $('#pn' + str).val();
var al = $('#al' + str).val();
var datas = "nm=" + nm + "&gd=" + gd + "&pn=" + pn + "&al=" + al;
$.ajax({
type: "POST",
url: "inc/updatedata.php?id=" + id,
data: datas
}).done(function (data) {
$('#info').html(data);
viewdata();
});
}
function deletedata(str) {
var id = str;
$.ajax({
type: "GET",
url: "inc/deletedata.php?id=" + id
}).done(function (data) {
$('#info').html(data);
viewdata();
});
}
update.php
<?php
require("inc/mysqli_connect.php");
if(isset($_GET['ID'])){
$stmt = $conn->prepare("UPDATE video_parent SET Title=?, TagLine=?, BannerURL=?, IconURL=?, VideoAnnouncementURL=?, VideoDescription=?, Order=? WHERE ID=?");
$stmt->bind_param('sssss', $title, $tagLine, $bannerURL, $iconURL, $videoAnnouncementURL, $videoDescription, $order, $id);
$title = $_POST['Title'];
$tagLine = $_POST['TagLine'];
$bannerURL = $_POST['BannerURL'];
$iconURL = $_POST['IconURL'];
$videoAnnouncementURL = $_GET['VideoAnnouncementURL'];
$videoDescription = $_GET['VideoDescription'];
$order = $_GET['Order'];
$id = $_GET['ID'];
if($stmt->execute()){
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>Success!</strong>
Anda berhasil mengubah data.
</div>
<?php
} else{
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>Error!</strong>
Maaf terjadi kesalahan, data error.
</div>
<?php
}
} else{
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>Warning!</strong>
Maaf anda salah alamat.
</div>
<?php
}
?>
我想要价值Json格式?请任何人帮助我?
答案 0 :(得分:0)
如果我理解正确,您希望以jSON格式将数据发送到PHP以及urlencode帖子格式吗?
function viewdata() {
$.ajax({
type: "GET",
url: "inc/getdata.php"
}).done(function (data) {
$('#viewdata').html(data);
});
}
$('#save').click(function () {
var datas ={
nm: $('#nm').val(),
gd: $('#gd').val(),
pn: $('#pn').val(),
al: $('#al').val()
} ;
$.ajax({
type: "POST",
url: "inc/newdata.php",
contentType: 'application/json',
data: datas
}).done(function (data) {
$('#info').html(data);
viewdata();
});
});
function updatedata(str) {
var datas ={
id: str,
nm: $('#nm' + str).val(),
gd: $('#gd' + str).val(),
pn: $('#pn' + str).val(),
al: $('#al' + str).val()
} ;
$.ajax({
type: "POST",
url: "inc/updatedata.php",
contentType: 'application/json',
data: datas
}).done(function (data) {
$('#info').html(data);
viewdata();
});
}
在PHP方面,使用$ _POST正文中的json_decode()
$inputJSON =file_get_contents ('php://input') ;
$input =json_decode ($inputJSON) ;
现在,如果您想将PHP的JSON回复返回到您的javascript代码:
header ('Content-Type: application/json') ;
echo json_encode (mydata) ;
exit ;
并在javascript中
}).done(function (data) {
data =JSON.parse (data) ;
...
}) ;
我还对你的'url:'inc / updatedata.php“,'进行了更改,因为我不明白为什么要在帖子正文和查询参数之间拆分数据。他们可以保持一致,以保持一致性和代码可读性。
最后,当您构建编码参数字符串时(即var datas =“nm =”+ nm +“&amp; gd =”+ gd +“&amp; pn =”+ pn +“&amp; al =”+ al ;),确保使用encodeURIComponent()
编码值