Php 使用wordpress和mysql进行动态下拉

Php 使用wordpress和mysql进行动态下拉,php,jquery,html,mysql,wordpress,Php,Jquery,Html,Mysql,Wordpress,我在wordpress页面上有两个下拉列表,名为zipcodehelp.php。一个代表州,另一个代表城市。我在主题文件夹中使用filezila上传了它,还有header.php、footer.php等。我的数据库由带有字段的表zipcode组成:CITY、STATE、FULLSTATE、zipcode 下面是我在header.php中找到的代码 <script language="javascript" type="text/javascript"> function getXML

我在wordpress页面上有两个下拉列表,名为zipcodehelp.php。一个代表州,另一个代表城市。我在主题文件夹中使用filezila上传了它,还有header.php、footer.php等。我的数据库由带有字段的表zipcode组成:CITY、STATE、FULLSTATE、zipcode

下面是我在header.php中找到的代码

<script language="javascript" type="text/javascript">
function getXMLHTTP() { //function to return the xml http object
        var xmlhttp=false;  
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {       
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
    }

    function getCity(FULLSTATE) {       

        var strURL="findCity.php?FULLSTATE="+FULLSTATE;
        var req = getXMLHTTP();

        if (req) {

            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('citydiv').innerHTML=req.responseText;                      
                    } else {
                        alert("Problem while using XMLHTTP:\n" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);
        }       
    }
</script>
接下来,我创建了一个自定义模板,在wordpress中创建了一个页面,并将该模板分配给该页面。zipcodehelp.php

<?php

/* Template Name: Zipcode Help */
?>

<?php

get_header();
global $wpdb;

$query = "SELECT DISTINCT(FULLSTATE) AS FULLSTATE FROM zipcode ORDER BY FULLSTATE ASC";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
   $dd .= "<option value='".$row['FULLSTATE']."'>".$row['FULLSTATE']."</option>";
}

?>
<form action="zipcodehelp.php" name="form1" method="POST" id="zipcode">
<center>
<table cellspacing="0" cellpadding="0">
<tr>
<td width="75">State</td>
<td width="50">:</td>
<td>
<select name="FULLSTATE" id="dropdown1" onchange="getCity(this.value)">
<option value="">--Select STATE--</option>
<?php echo $dd; ?>
</select>
</td>
</tr>

<tr style="">
    <td>City</td>
    <td width="50">:</td>
    <td ><div style="height:300px;" id="citydiv"><select name="CITY" >
    <option>--Select CITY--</option>
        </select></div></td>
</tr>
</table>
</center>

<input type="submit" id="findzip" name="findzip" value="Submit"></input>
</form>
<!--content of the page-->
<div class="container-wrapper clearfix" id="main-content-container">
        <div class="container">
        <?php if (have_posts()) : while (have_posts()) : the_post();?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
        </div>
</div> 

<?php get_footer(); ?>
这是我的参考页面findCity.php。我的第一个问题是,如果我不将其设置为自定义模板,访问www.site.com/findcity.php会给我找不到的页面,所以我所做的是在wordpress中创建另一个页面,并将此模板分配给该页面。代码如下:

<?php

/* Template Name: Find City */
?>
<?php 
get_header();

$FULLSTATE=intval($_GET['FULLSTATE']);
global $wpdb;
$query = "SELECT CITY FROM zipcode WHERE FULLSTATE = '$FULLSTATE'";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
   $yy .= "<option value='".$row['CITY']."'>".$row['CITY']."</option>";
}
?>

<select name="CITY">
<option>--Select CITY--</option>
<?php echo $yy; ?>
</select>

<?php get_footer(); ?>

运行页面时,我发现错误:使用XMLHTTP时出现问题:未找到。我真的需要帮助。我已经被这个问题困扰了两周:

使用ajax的第一个也是最主要的一个问题是,在wordpress中不能像这样使用ajax。您必须为ajax请求调用admin-ajax.php文件,然后必须在ajax的数据参数中定义一个操作,然后将其挂接到wp_ajax_your_action_name注意,DISTINCT不是一个函数,我认为简单的方法是将findCity.php放在wordpress的根目录中,并使用require_oncewp-load.php包含wordpress;从代码中删除主题名部分,然后我想你的东西会工作的…,如果你这样做,修复当前代码中的任何语法错误。,