简体   繁体   English

如何从通过json_encode传递的mysql db中拆分行以显示在单独的文本框中

[英]How to split the row from mysql db passed through json_encode to display in separate textboxes

I trying to split the result from a mysql statement output from one php page to another page containing javascript. 我试图将结果从一个php页面的mysql语句输出拆分到另一个包含javascript的页面。 I tried below method but something is missing in my code :-( 我尝试了下面的方法,但是我的代码中缺少一些东西:-(

The result I am looking forward is to get the values in each column from the selected row in mySQL table to populate into #eventTitle, #eventDescription and so on.... 我期待的结果是从mySQL表的选定行中获取每一列中的值,以填充到#eventTitle,#eventDescription等中。

Please find my code below 请在下面找到我的代码

lookup_event.php lookup_event.php

<?php
$eid = $_POST['eid']
include '/include/db_connect.php';
$sql="SELECT title, description, start, end FROM evenement where id=".$eid;
$result = $mysqli->query($sql);

while ($row = mysqli_fetch_assoc($result)) {
     $sessions[] = $row;
     }

     echo json_encode($sessions);
     die();
     ?>

script in cal.php cal.php中的脚本

eid = event.id;
$.ajax({
type: 'POST',
url: 'lookup_event.php',
data: eid,
success: function (sessions) {
$('#eventTitle').val(sessions["title"]);
$('#eventDescription').val(sessions["description"]);
}
});

Its coming as a string because its json. 它以字符串形式出现,因为它是json。 You first need to parse it. 您首先需要解析它。

script in cal.php cal.php中的脚本

eid = event.id;
$.ajax({
type: 'POST',
url: 'lookup_event.php',
data: eid,
  success: function (sessions) {
    var sessions = JSON.parse(sessions);

    $('#eventTitle').val(sessions["title"];
    $('#eventDescription').val(sessions["description"];
  }
});

I recommend you to start using PDO: http://php.net/pdo 我建议您开始使用PDO: http : //php.net/pdo

And statement+binds to automatically escape SQL injections. 和statement + binds可以自动转义SQL注入。

<?php
// ... include PDO connection ($conn)

$eid = (int) $_POST['eid'];

$sql = "SELECT title, description, start, end FROM evenement WHERE id = :id";

$stmt = $conn->prepare($sql);
$stmt->bindParam('id', $eid);
$stmt->execute();

$result = $stmt->fetchAll();

echo json_encode($result);

Javascript: 使用Javascript:

$.ajax({
    type: 'POST',
    url: 'lookup_event.php',
    data: { eid: eid },
    dataType: 'json',
    success: function (event) {
        // with dataType specified dont need parse
    }
});

Finally fixed the issue. 最后解决了这个问题。 Thanks everyone for your support. 感谢各位的支持。

This is how now my files look like. 这就是我的文件现在的样子。 However your comments for any modifications on below code are most welcome. 但是,最欢迎您对以下代码进行任何修改。 Thanks once again. 再次感谢。

lookup_event.php
<?php
include '/include/db_connect.php';
$eid = $_POST['eid'];
$sql = "SELECT title, description, start, end FROM evenement where id=" . intval($eid);
$result = $mysqli->query($sql);
if ($result) {
  $array = mysqli_fetch_array($result);
  }

echo json_encode($array);
die();
?>

cal.php cal.php

eid = event.id;
$.ajax({
    type: 'POST',
    url: 'lookup_event.php',
    data: {eid:eid},
    success: function (event) {
        var event = JSON.parse(event);
        $('#eventTitle').val(event.title);
        $('#eventStart').val(moment(event.start).format('YYYY-MM-DD, HH:mm:ss'));
        $('#eventEnd').val(moment(event.end).format('YYYY-MM-DD, HH:mm:ss'));
        $('#eventDescription').val(event.description);
        $('#myModal').modal();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM