数据库值在php中查找Json格式


Database value Find Json format in php

我使用了jquery网格值在网格中查找值。从数据库检索值。我使用了我以前设计的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">&times;</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">&times;</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">&times;</span>
    </button>
    <strong>Warning!</strong>
    Maaf anda salah alamat.
</div>
<?php
}
?>

我想要值Json格式?有人帮我吗?

如果我理解正确,你想用JSON格式和urlencode post格式将数据发送到PHP吗?

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向javascript代码返回JSON回复:

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+"&gd="+gd+"&aamp;pn="+pn+"&ap;al="+al;)时,请确保使用encodeURIComponent()对值进行编码