来自php页面的Ajax GET内容

时间:2013-07-23 18:01:54

标签: html mysql ajax get

您好我有一个表,我试图通过在单独的php页面中调用MySQL数据库来更新。这个单独的页面循环遍历结果集并通过一系列回声构建表。在主页面中,我试图将回显的内容插入到div中。

这一切都是由用户从下拉框中选择一个选项开始的。

这是单独的php页面。 (当我手动输入GET参数时它工作正常,它是两个页面之间的链接似乎不起作用)

tableGetter.php

<?PHP
$user_name = "rocketeermus_pr";
$password = "zuluhead2";
$database = "rocketeermus_pr";
$server = "pdb1.awardspace.com";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

echo "Bonjour";

if (isset($_GET['composer'])){ 

  echo "Helloooo";

  if ($db_found) {

    echo "SELECT * FROM catalogue WHERE Composer = '".mysql_escape_string($_GET['composer'])."';";
    $SQL = "SELECT * FROM catalogue WHERE Composer = '".mysql_escape_string($_GET['composer'])."';";
    $result = mysql_query($SQL);
    setlocale(LC_MONETARY,"en_GB");

    echo "<table class=\"sortable\" id=\"moder\" width=\"800\">";

    echo "<th>TITLE</th><th>COMPOSER</th><th>VOICING</th><th>PRICE</th><th></th></tr>";
    while ( $db_field = mysql_fetch_assoc($result) ) {
      echo "Hi.";

      echo "<tr><td>{$db_field['Title']}</td><td>{$db_field['Composer']}</td><td>{$db_field['Voicing']}</td><td>";
      echo money_format("%n", $db_field['Price']);
      echo "</td><td> <div class=\"product\"> <input value=\"{$db_field['Title']}\" class=\"product-title\" type=\"hidden\"> <input value=\"0.5\" class=\"product-weight\" type=\"hidden\"> <input value=\"{$db_field['NoVox']}\" class=\"googlecart-quantity\" type=\"hidden\"> <input value=\"{$db_field['Price']}\" class=\"product-price\" type=\"hidden\"> <div title=\"Add to cart\" role=\"button\" tabindex=\"0\" class=\"googlecart-add-button\"> </div> </div> </td></tr>";

    }
    echo "</table>";

    mysql_close($db_handle);

  } else {
    print "Database NOT Found ";
    mysql_close($db_handle);
  }
}

?>

以下是主页中的重要内容:

使用Javascript:

function getdata()
{
    var req = getXMLHTTP();
    if (req)
    {

        //function to be called when state is changed
        var queryString1 = "";
        req.onreadystatechange = function()
        {
            //when state is completed i.e 4
            if (req.readyState == 4)
            {

                var ajaxSearchResults1 = document.getElementById("table");
                ajaxSearchResults1.innerHTML = req.responseText;

                // only if http status is "OK"
                if (req.status == 200)
                {
                    var new1 = document.getElementById('composer').value;
                    queryString1 = "?composer=" + encodeURIComponent(new1);
                    console.log (queryString1);
                }
                else
                {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }
        }
        req.open("GET", "tableGetter.php" + queryString1, true);
        req.send();
    }
}

function getXMLHTTP() {
    var xmlhttp;

    if(window.XMLHttpRequest){ //For Firefox, Mozilla, Opera, and Safari
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject){ //For ie
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        if (!xmlhttp){
            xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
    }
    return xmlhttp;
}

HTML:

<div id="menus">
  <table>
    <tr>
      <td><form action=""">
            <select name="composer" id ="composer" onchange="getdata();"> 
              <?php  
                 $user_name = "***";
                 $password = "****";
                 $database = "****";
                 $server = "****.com";

                 $db_handle = mysql_connect($server, $user_name, $password);
                 $db_found = mysql_select_db($database, $db_handle);

                 if ($db_found) {

                 $SQL = "SELECT DISTINCT Composer FROM catalogue ORDER BY Composer";
                 $result = mysql_query($SQL);
                 setlocale(LC_MONETARY,"en_GB");
                 while ( $db_field = mysql_fetch_assoc($result) ) { 
                 ?> 
              <option id="composer" onchange="getdata();" value="<?php echo $db_field['Composer'];?>"> 
                <?php  
                   echo $db_field['Composer']; 
                   ?> 
              </option> 
              <?php 
                 } 
                 }
                 ?> 
            </select>


      </form></td>
    </tr>
  </table>
</div>

<div id="table"> 
  <?php include("tableGetter.php"); ?>
</div>

主页面上的html工作正常,下拉菜单很好地填充了数据库中所有不同的作曲家名称。当选择菜单中的选项时,“table”div中回显的唯一内容是“Bonjour”。它不会超过if (isset($_GET['composer']))页面中的tableGetter.php。我打印出queryString1函数中请求的getData()变量(获取参数),并报告:?composer=Animuccia%2C%20Paulo在手动加载页面时效果很好。它不会动态地工作!

有人知道这里发生了什么吗?

1 个答案:

答案 0 :(得分:0)

在发送AJAX请求之前,您没有设置queryString1。尝试重写getdata()

function getdata()
{
    var req = getXMLHTTP();
    if (req)
    {
        //function to be called when state is changed
        req.onreadystatechange = function()
        {
            //when state is completed i.e 4
            if (req.readyState == 4)
            {
                // only if http status is "OK"
                if (req.status == 200)
                {
                    var ajaxSearchResults1 = document.getElementById("table");
                    ajaxSearchResults1.innerHTML = req.responseText;
                }
                else
                {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }
        }
        var new1 = document.getElementById('composer').value;
        var queryString1 = "?composer=" + encodeURIComponent(new1);
        req.open("GET", "tableGetter.php" + queryString1, true);
        req.send();
    }
}