使用jquery将数据从php发送到javascript

时间:2015-04-07 08:04:16

标签: javascript php jquery html

我正在尝试实现垂直时间轴,但我不知道如何使用php从db获取正确格式的数据。

添加事件的代码如下:

$(function() {
  $("#element").timeline({
    data: [
      {time: new Date(),
        color: '#555',
        css: 'success',
        content: 'jquery.timeline Demo Event 1'},
      {time: new Date(),
        color: '#00ff00',
        css: 'success',
        content: 'jquery.timeline Demo Event 2'},
      {time: new Date(),
        color: '#000',
        css: 'success',
        content: 'jquery.timeline Demo Event 3'},
      {time: new Date(),
        color: '#000',
        css: 'success',
        content: 'jquery.timeline Demo Event 4'}
    ]
  });

我设置了一个get.php,我可以通过这种方式从db获取数据:

<?php
  $servername = "localhost";
  $username = "";
  $password = "";
  $dbname = "";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  } 

  $sql = "SELECT NAME, TEXT, DATE FROM TABLE";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["NAME"]. " - Name: " . $row["TEXT"]. " " . $row["DATE"]. "<br>";

    }
  } else {
    echo "0 results";
  }
  $conn->close();
?> 

如何将所有检索到的内容放入事件处理程序中?

$(function() {
  $("#element").timeline({
    data: [
      {time: date_event_1,
        color: color_event_1,
        css: 'success',
        content: content_event_1},
      ...
                  ...

    ]
  });

提前致谢。

0 个答案:

没有答案