【问题标题】:Ajax to load content of another php pageAjax 加载另一个 php 页面的内容
【发布时间】:2013-07-13 02:31:25
【问题描述】:

我有两个单独的代码: 1. 一种在按钮单击时显示数据库内容的表单(form.php) 2. 另一个 php 页面显示图表到 div 在加载时从 mysql 数据库中检索内容。 (graph.php)

我是 ajax 新手。我试图在另一个 php 页面的(form.php)div 上显示图表,其代码存在于 graph.php 中。

问题是 graph.php 显示加载事件图表使用同一页面本身 (graph.php) 中存在的 div 的 id 来显示图表。如何在 form.php 中的 div 中加载此页面

form.php

<html><head><script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","graph.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="submit"onclick="showUser(this.value)"/>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>

另一个从 db 加载数据并使用 google chart API 显示图表的页面是 图.php:

<?php
$q=$_GET["q"];
  $mysqli =mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: ".mysqli_connect_error();
}

  $result = $mysqli->query('SELECT * FROM new_temp');
  $rows = array();
  $table = array();
  $table['cols'] = array(

    array('label' => 'ind_type', 'type' => 'string'),
    array('label' => 'sum', 'type' => 'number')

);
    /* Extract the information from $result */
    foreach($result as $r) {

      $temp = array();
      $temp[] = array('v' => (string) $r['ind_type']); 
      $temp[] = array('v' => (int) $r['sum']); 
      $rows[] = array('c' => $temp);
    }

$table['rows'] = $rows;
$jsonTable = json_encode($table);
?>
<html>
  <head>
    <!--Load the Ajax API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">

    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {
var data = new google.visualization.DataTable(<?=$jsonTable?>);
      var options = {
           title: 'Index analysis',
          is3D: 'true',
          width: 800,
          height: 600
        };
      var chart = new google.visualization.PieChart(document.getElementById('txtHint'));
      chart.draw(data, options);
    }
    </script>
  </head>

  <body>
    <!--this is the div that will hold the pie chart-->
    <div id="txtHint"></div>
  </body>
</html>

【问题讨论】:

  • 如果我没有正确理解这一点,请原谅我,但是您是否有理由不能为每个 div 使用两个不同的 ID?
  • 您的最后一个 ID“txxtHint”中还有一个额外的“x”
  • @A.O.:我使用了两个不同的 ID。在这种情况下,graph 出现在 graph.php 页面的 div 中。
  • @A.O.:我认为相同或不同的 ID 在这里不是问题。问题是不同的,我无法解决!

标签: php ajax cakephp-ajaxhelper


【解决方案1】:

首先,您必须在 php 文件中使用 json 制作所有数据,在 html 中包含 jquery,然后使用以下代码: 希望对你有帮助

HTML

<form action="graph.php" method="get">
  <input type="text" name="q" />
  <input type="submit"/>
</form>

js

$("form").on("submit", function(){
 $.get($('form').attr('action'), $('form').serialize(), 
     function(result) {
         $("#txtHint").html(result);
      },'json');
 });

【讨论】:

  • 我的目的不仅仅是解决这个问题。我正在努力学习ajax。不使用json可以吗?在其他情况下,您能告诉我应该在哪里嵌入 json 代码吗?
  • 您想从数据库中获取它的日期是什么时候?向我发送 out 但不带 js 的数据示例
  • 我正在尝试获取 column - index_type and counts. Index type is pcount and ncount and both have int value 的 -ve 和 +ve 索引值。我知道没有 javascript 是不可能的,但是你给出的代码,我不知道在哪里放置以及如何使用。我还在学习js,json!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2017-03-14
  • 2019-02-19
  • 2011-06-01
相关资源
最近更新 更多