用wordpress和mysql动态下拉

时间:2014-03-11 07:52:55

标签: php jquery html mysql wordpress

我在一个名为zipcodehelp.php的wordpress页面中有两个下拉列表。 1是国家,另一个是城市。我使用filezila在主题文件夹中将其与header.php,footer.php等一起上传。我的数据库由表格邮政编码组成,包括字段: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时出现问题:找不到。我真的需要帮助。我已经坚持了两个星期了:(

0 个答案:

没有答案