0

我有一个 php 页面,上面有一个 ajax 函数 makerequest(serverPage, objID) ,它工作正常它有三个链接它们也工作正常我的内容出现在<div id=pageId>. 在此页面中,第三页有一个由 mysql 填充的下拉列表,其中包含 on change 事件。在这个页面中,每个选项都有很多行,所以我必须决定数据通过 php 分页类显示在多个页面上,它也可以正常工作,但是当我点击下一页时,由于重新加载页面而丢失了所有数据并获取下拉列表中的默认值。

部分代码内容如下:

<div id="menu">
    <!-- <a href="?id=AllIndiaengineringCollege" What part we want to link to onClick="makerequest('page.php?id=AllIndiaengineringCollege', 'content'); return false;" For AJAX - Load page into the div>AllIndiaengineringCollege</a>-->
    <a href="?id=AllIndiaengineringCollege" onClick="makerequest('id=AllIndiaengineringCollege', 'content'); return false;" style="text-decoration:none;color: #3E5AAB" >All India Engineering College</a>
    <a href="?id=Deemed" onClick="makerequest('page.php?id=Deemed', 'content'); return false;" style="text-decoration:none;color: #3E5AAB">Deemed Universities</a>
    <a href="?id=state" onClick="makerequest('id=state', 'content'); return false;" style="text-decoration:none;color: #3E5AAB">State Engineering College</a>
</div>

</font></div>
<div id='content'style="text-align:left;width:897px; height:auto; background-color:#99CCFF;padding:1px">
    <?PHP
    if (isset($_GET['id'])) {
        $pageId = $_GET['id'];
        if ($pageId == 'AllIndiaengineringCollege') {
            include("/pagination/index.php");
        }
        if ($pageId == 'Deemed') {
            include("/pagination/state.php");
        }
        if ($pageId == 'state') {
            include("/pagination/state.php");
        }
    }
    ?>
</div>

它工作正常我的第三页是

<?php
$result = mysql_query("SELECT Id,state FROM state");
echo "<select name=State id='Id' class='select' onchange='showUser(this.value)'>";
while ($nt = mysql_fetch_array($result)) {
    echo ('<option value="' . $nt['Id'] . '" if($State===' . $nt['Id'] . ') echo $sel; >' . $nt['state'] . '</option>');
}
echo "</select>";
?> 
</div>
        <div id="state"><b><center>xxxxxxxx.</center></b>

showUser 函数就像这里

function showUser(str) {
    if (str == "") {
        document.getElementById("state").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("state").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "pagination/statelist.php?state=" + str, false);
    xmlhttp.send();
}

分页代码如下:

<?php
include('db.php');
include('function.php');

$q          = $_GET["state"];
$page       = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit      = 2;
$statement  = "college where State= '" . $q . "'";
$url        = "BTech.php?id=state&State=" . $q . "&";
$startpoint = ($page * $limit) - $limit;
?>
4

1 回答 1

0

我想如果你改正这个就好了

while ($nt = mysql_fetch_array($result)) {
    echo ('<option value="' . $nt['Id'] . '" if($State===' . $nt['Id'] . ') echo $sel; >' . $nt['state'] . '</option>');
}

像这样的东西,

while ($nt = mysql_fetch_array($result)) {
    echo "<option value=\"{$nt['Id']}\"".(($State===$nt['Id'])?'selected="selected"':"") .">{$nt['state']}</option>";
}

您不能以这种方式评估条件,嵌套echo也不起作用。

看看这个以前的SO问题

于 2012-08-13T05:10:50.823 回答