通过AJAX加载页面

时间:2012-03-26 16:18:51

标签: php ajax

<form>
        <table>
            <tr>
                <td>
                    <input type="tex" id="arheading" name="arheading" value="Article Heading" onfocus="if (this.value == 'Article Heading') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Article Heading';}" />
            </td>
                <td>
                    <select id="plaetype" name="placetype">
                        <option selected="Place Type">Place Type</option>
                        <?php
                        include 'ini/configs_adm.php';
                        $result_adm = mysql_query("SELECT * FROM places");
                        while($row_adm=mysql_fetch_array($result_adm))
                        {
                           echo '<option value="'.$row_adm['places'].'">'.$row_adm['places'].'</option>'; 
                        }
                        ?>
                    </select>
                </td>
                <td>
                    <select id="country" name="country">
                    <?php
                       include 'ini/configs_adm.php';

                       $result_adm = mysql_query("SELECT * FROM countries");
                       while($row_adm=mysql_fetch_array($result_adm))
                       {
                       echo '<option value="'.$row_adm['country'].'">'.$row_adm['country'].'</option>';
                       }
                    ?>
                    </select>    
                </td>
            </tr>
        </table>       
    </form>

以上是我想要在索引页面上加载而无需刷新或没有通过AJAX导航的代码。

我尝试的ajax代码是.........

var loadedobjects = ""
var rootdomain = "http://" + window.location.hostname

function ajaxpage(url, containerid) {
    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest()
    else if (window.ActiveXObject) { // if IE
        try {
            page_request = new ActiveXObject("Msxml2.XMLHTTP")
        }
        catch (e) {
            try {
                page_request = new ActiveXObject("Microsoft.XMLHTTP")
            }
            catch (e) {
            }
        }
    }
    else
        return false
    page_request.onreadystatechange = function () {
        loadpage(page_request, containerid)
    }
    page_request.open('GET', url, true)
    page_request.send(null)
}

function loadpage(page_request, containerid) {
    if (page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1))
        document.getElementById(containerid).innerHTML = page_request.responseText
}

function loadobjs() {
    if (!document.getElementById)
        return
    for (i = 0; i < arguments.length; i++) {
        var file = arguments[i]
        var fileref = ""
        if (loadedobjects.indexOf(file) == -1) { //Check to see if this object has not already been added to page before proceeding
            if (file.indexOf(".js") != -1) { //If object is a js file
                fileref = document.createElement('script')
                fileref.setAttribute("type", "text/javascript");
                fileref.setAttribute("src", file);
            }
            else if (file.indexOf(".css") != -1) { //If object is a css file
                fileref = document.createElement("link")
                fileref.setAttribute("rel", "stylesheet");
                fileref.setAttribute("type", "text/css");
                fileref.setAttribute("href", file);
            }
        }
        if (fileref != "") {
            document.getElementsByTagName("head").item(0).appendChild(fileref)
            loadedobjects += file + " " //Remember this object as being already added to page
        }
    }
}

并且将调用AJAX函数的代码是.....

<ul id="admin_menu">
    <?php
    include 'ini/configs_adm.php';
    $result_adm = mysql_query("SELECT * FROM adm_tools");
    while ($row_adm = mysql_fetch_array($result_adm))
    {
        echo '<li><a href="javascript:ajaxpage(\'' . $row_adm['adm_menu_lnk'] . '\', \'contentbox\');">' . $row_adm['adm_menu'] . '</a></li>';
    }
    ?>
</ul>

我的ajax代码加载了页面,但php脚本没有从db

中检索数据

1 个答案:

答案 0 :(得分:0)

看起来你可能会错过提取($ row)函数。

这看起来像是:

while($row_adm=mysql_fetch_array($result_adm))
{
 extract($row_adm);
 echo '<option value="'.$row_adm['places'].'">'.$row_adm['places'].'</option>'; 
}