如何在第一个ajax页面中显示第二个ajax数据?

时间:2013-12-08 18:23:45

标签: jquery sql ajax

在我的代码中,产品名称将随品牌名称而变化,我将显示产品信息。 但信息显示不正确...如果我只从url运行getdetails.php页面,我可以看到产品的信息bt我无法从用户页面看到它,我需要在用户页面显示数据... 。
我的用户页面代码:

    <?php
    session_start();
    if( $_SESSION['type']!='user')
    {
        header("Location: index.php");
        return;
    }
    require_once("dbconnect.php");
    $sql = "SELECT Brand FROM info";
    $rslt = mysql_query($sql);
    $options = "";
    while ($row=mysql_fetch_array($rslt))
    {
        $options .= "<option>$row[Brand]<option>";
    }
    mysql_close();
   ?>

<script type="text/javascript">
            function loadDetails()
            {
                var xmlhttp;
                var pname = document.getElementById("list").value;
                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("details").innerHTML=xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","getdetails.php?name="+pname,true);
                xmlhttp.send();
            }
        </script>
    </head>
    <body onload="loadDetails();">
    <center>
        <h1>Product</h1>
        <fieldset>
        <legend><label><b>Choose Your Product</b></label></legend></br>
            <label><b>Brand Name</b></label>
            <select id="list" onchange="loadDetails();"><?php echo $options; ?></select>
            <div id="details"></div>
        </fieldset>

my getdetails页面:

<?php
    session_start();
    if( $_SESSION['type']!='user')
    {
        header("Location: index.php");
        return;
    }
    require_once("dbconnect.php");
    $name = $_REQUEST['name'];
    $sql = "SELECT Name FROM info WHERE Brand='$name' AND quantity > 0";
    $rslt = mysql_query($sql);
    $options ="";
    while ($row=mysql_fetch_array($rslt))
    {
        $options.="<option>$row[Name]<option>";
    }
    mysql_close();
?>

<script type="text/javascript">
            function Details()
            {
                var xmlhttp;
                var lname = document.getElementById("plist").value;
                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)
                    {
                        document.getElementById("dtil").innerHTML=xmlhttp.responseText;

                    }
                }
                xmlhttp.open("GET","details.php?pnam="+lname,true);
                xmlhttp.send();
            }
        </script>
<body onload="Details();">
            <label><b>Product Name</b></label>
            <select id="plist" onchange="Details();"><?php echo $options; ?></select>
            <?php echo "<table id='dtil' border=3></table>";


            ?>
    </body>

如何在“dtil”中获取表的数据?是否有任何有效的方法在一个页面中执行这些代码?

1 个答案:

答案 0 :(得分:0)

您正在动态生成的选项行没有附加value字段,因此var pname = document.getElementById("list").value;不返回任何内容。您还在HTML代码中将其命名为plist

我敦促您寻求轻松和安全的其他评论:

  • 查看mysqliPDOmysql既不安全又可能在不久的将来被弃用。
  • 查看jQuery,它比纯javascript容易得多。了解CSS选择器可以更加轻松地理解jQuery
  • 为了安全起见,请查看SQL injection.