Javascript 在.js文件中插入php代码

Javascript 在.js文件中插入php代码,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,如何在.js文件中插入php代码块? 这是我的ziphelp.php代码 <html> <head> <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/zipjs/jquery-1.4.2.js"></script> <script type="text/javascript" src="&

如何在.js文件中插入php代码块? 这是我的ziphelp.php代码

<html>
<head>
        <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/zipjs/jquery-1.4.2.js"></script>
        <script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/zipjs/dropdown.js"></script>

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script langauge="javascript">
function post_value(){
opener.document.form1.RFromZip.value = document.zipfrm.zipcode.value;
self.close();
}
</script>

<title>Zip Help |Moving quotes Pro</title>
</head>


<body class="color2 pt30 pb30">
<?php /* Template Name: practice */ ?>
<h2>ZIP HELP|MQP</h2>
<form name="frm" method=post action=''>
<table border=0 cellpadding=0 cellspacing=0 width=200>

<tbody style="margin: 0 auto;position: absolute;">
<tr><td align="center"> <!---Dropdown 2nd Part--->
            <select name="dep">
                <option value="">- Select State -</option>
                <option>Alabama</option>
                <option>Alaska</option>
                <option>Arizona</option>
                <option>Arkansas</option>
                <option>California</option>
                <option>Colorado</option>
                <option>Connecticut</option>
                <option>District of Columbia</option>
                <option>Delaware</option>
                <option>Florida</option>
                <option>Georgia</option>
                <option>Hawaii</option>
                <option>Idaho</option>
                <option>Illinois</option>
                <option>Indiana</option>
                <option>Iowa</option>
                <option>Kansas</option>
                <option>Kentucky</option>
                <option>Louisiana</option>
                <option>Maine</option>
                <option>Maryland</option>
                <option>Massachusetts</option>
                <option>Michigan</option>
                <option>Minnesota</option>
                <option>Mississippi</option>
                <option>Missouri</option>
                <option>Montana</option>
                <option>Nebraska</option>
                <option>Nevada</option>
                <option>New Hampshire</option>
                <option>New Jersey</option>
                <option>New Mexico</option>
                <option>New York</option>
                <option>North Carolina</option>
                <option>North Dakota</option>
                <option>Ohio</option>
                <option>Oklahoma</option>
                <option>Oregon</option>
                <option>Pennsylvania</option>
                <option>Rhode Island</option>
                <option>South Carolina</option>
                <option>South Dakota</option>
                <option>Tennessee</option>
                <option>Texas</option>
                <option>Utah</option>
                <option>Vermont</option>
                <option>Virginia</option>
                <option>Washington</option>
                <option>West Virginia</option>
                <option>Wisconsin</option>
                <option>Wyoming</option>
            </select>
            <select name="cname">
                <option>- Select City -</option>
            </select>
        <!---End Dropdown 2nd Part---> <input type="submit" value='Submit' name="findzip">
</td></tr>
</tbody>
</table></form>
<?php
if(isset($_POST['findzip']))
{
        global $wpdb;
$city = $_POST['cname'];
        $query = "SELECT * FROM zipcode where CITY = '$city'";
        $result = mysql_query($query) or die(mysql_error());

        while($row = mysql_fetch_array($result,MYSQL_ASSOC))
        {
           $zip .= "<option value='".$row['ZIP']."'>".$row['ZIP']."</option>";
        }
    ?>
    <div style="margin-top:100px;">
        <form action="" method="post" name="zipfrm">
                <select name="zipcode">
                    <option value="">Select Zipcode</option>
                    <?php echo $zip; ?>
                </select>
                <input type="button" value="Post Zip" name="passzip" onclick="post_value();">
        </form></div>
<?php } ?>

</body>
</html>

选择Zipcode
我原来的dropdown.js:

$(document).ready(function() {
$department = $("select[name='dep']");
$cname = $("select[name='cname']");



$department.change(function() {

if ($(this).val() == "Alabama") {
$("select[name='cname'] option").remove();
$("<option value=''> -Select City- </option>").appendTo($cname); 
$("<option value='ABBEVILLE'> ABBEVILLE</option>").appendTo($cname); 
$("<option value='ABERNANT'> ABERNANT</option>").appendTo($cname); 
} 
$(文档).ready(函数(){
$department=$(“选择[name='dep']”);
$cname=$(“选择[name='cname']”);
$department.change(职能(){
如果($(this.val()=“阿拉巴马州”){
$(“选择[name='cname']选项”).remove();
$(“-选择城市-”).appendTo($cname);
$(“ABBEVILLE”)。附录($cname);
$(“ABERNANT”)。附录($cname);
} 
代码很好,但这是一个非常长的列表。数千个城市。 所以我决定添加mysql代码,如下所示:

$(document).ready(function() {
$department = $("select[name='dep']");
$cname = $("select[name='cname']");
$choice = "empty";
$department.change(function() {

if ($(this).val() == "Alabama") {
$choice = "AL";
}
if ($(this).val() == "Alaska") {
$choice = "AK";
}
if ($(this).val() == "Arkansas") {
$choice = "AR";
}
if ($(this).val() == "California") {
$choice = "CA";
}
if ($(this).val() == "Colorado") {
$choice = "CO";
}
if ($(this).val() == "Connecticut") {
$choice = CT";
}
if ($(this).val() == "District of Columbia") {
$choice = "DC";
}
if ($(this).val() == "Delaware") {
$choice = "DE";
}
if ($(this).val() == "Florida") {
$choice = "FL";
}
if ($(this).val() == "Georgia") {
$choice = "GA";
}
if ($(this).val() == "Hawaii") {
$choice = "HI";
}
if ($(this).val() == "Iowa") {
$choice = "IA";
}
if ($(this).val() == "Idaho") {
$choice = "ID";
}
if ($(this).val() == "Illinois") {
$choice = "IL";
}
if ($(this).val() == "Indiana") {
$choice = "IN";
}
if ($(this).val() == "Kansas") {
$choice = "KS";
}
if ($(this).val() == "Kentucky") {
$choice = "KY";
}
if ($(this).val() == "Louisiana") {
$choice = "LA";
}
if ($(this).val() == "Massachusetts") {
$choice = "MA";
}
if ($(this).val() == "Maryland") {
$choice = "MD";
}
if ($(this).val() == "Maine") {
$choice = "ME";
}
if ($(this).val() == "Michigan") {
$choice = "MI";
}
if ($(this).val() == "Minnesota") {
$choice = "MN";
}
if ($(this).val() == "Missouri") {
$choice = "MO";
}
if ($(this).val() == "Mississippi") {
$choice = "MS";
}
if ($(this).val() == "Montana") {
$choice = "MT";
}
if ($(this).val() == "North Carolina") {
$choice = "NC";
}
if ($(this).val() == "North Dakota") {
$choice = "ND";
}
if ($(this).val() == "Nebraska") {
$choice = "NE";
}
if ($(this).val() == "New Hampshire") {
$choice = "NH";
}
if ($(this).val() == "New Jersey") {
$choice = "NJ";
}
if ($(this).val() == "New Mexico") {
$choice = "NM";
}
if ($(this).val() == "Nevada") {
$choice = "NV";
}
if ($(this).val() == "New York") {
$choice = "NY";
}
if ($(this).val() == "Ohio") {
$choice = "OH";
}
if ($(this).val() == "Oklahoma") {
$choice = "OK";
}
if ($(this).val() == "Oregon") {
$choice = "OR";
}
if ($(this).val() == "Pennsylvania") {
$choice = "PA";
}
if ($(this).val() == "Rhode Island") {
$choice = "RI";
}
if ($(this).val() == "South Carolina") {
$choice = "SC";
}
if ($(this).val() == "South Dakota") {
$choice = "SD";
}
if ($(this).val() == "Tennessee") {
$choice = "TN";
}
if ($(this).val() == "Texas") {
$choice = "TX";
}
if ($(this).val() == "Utah") {
$choice = "UT";
}
if ($(this).val() == "Virginia") {
$choice = "VA";
}
if ($(this).val() == "Vermont") {
$choice = "VT";
}
if ($(this).val() == "Washington") {
$choice = "WA";
}
if ($(this).val() == "Wisconsin") {
$choice = "WI";
}
if ($(this).val() == "West Virginia") {
$choice = "WV";
}
if ($(this).val() == "Wyoming") {
$choice = "WY";
}
<?php 
global $wpdb;
        $query = "SELECT * FROM zipcode where STATE = '$choice'";
        $result = mysql_query($query) or die(mysql_error());
        $("select [name='cname']option").remove();
        while($row = mysql_fetch_array($result,MYSQL_ASSOC))
        {
           $("<option value='".$row['CITY']."'>".$row['CITY']."</option>").appendTo($cname);
        }
?>

});
});
$(文档).ready(函数(){
$department=$(“选择[name='dep']”);
$cname=$(“选择[name='cname']”);
$choice=“empty”;
$department.change(职能(){
如果($(this.val()=“阿拉巴马州”){
$choice=“AL”;
}
如果($(this.val()=“阿拉斯加”){
$choice=“AK”;
}
如果($(this.val()=“阿肯色州”){
$choice=“AR”;
}
如果($(this.val()=“California”){
$choice=“CA”;
}
if($(this.val()=“Colorado”){
$choice=“CO”;
}
if($(this.val()==“康涅狄格州”){
$choice=CT”;
}
如果($(this.val()=“哥伦比亚特区”){
$choice=“DC”;
}
如果($(this.val()=“特拉华”){
$choice=“DE”;
}
如果($(this.val()=“Florida”){
$choice=“FL”;
}
如果($(this.val()=“格鲁吉亚”){
$choice=“GA”;
}
如果($(this.val()==“夏威夷”){
$choice=“HI”;
}
如果($(this.val()=“Iowa”){
$choice=“IA”;
}
如果($(this.val()=“爱达荷州”){
$choice=“ID”;
}
if($(this.val()=“Illinois”){
$choice=“IL”;
}
如果($(this.val()=“Indiana”){
$choice=“IN”;
}
if($(this.val()=“堪萨斯州”){
$choice=“KS”;
}
if($(this.val()=“肯塔基州”){
$choice=“KY”;
}
如果($(this.val()=“路易斯安那州”){
$choice=“LA”;
}
如果($(this.val()=“Massachusetts”){
$choice=“MA”;
}
如果($(this.val()=“马里兰”){
$choice=“MD”;
}
如果($(this.val()=“缅因州”){
$choice=“我”;
}
如果($(this.val()=“Michigan”){
$choice=“MI”;
}
如果($(this.val()=“明尼苏达州”){
$choice=“MN”;
}
如果($(this.val()=“Missouri”){
$choice=“MO”;
}
如果($(this.val()=“密西西比”){
$choice=“MS”;
}
如果($(this.val()=“蒙大拿”){
$choice=“MT”;
}
如果($(this.val()=“北卡罗来纳州”){
$choice=“NC”;
}
if($(this.val()=“北达科他州”){
$choice=“ND”;
}
如果($(this.val()=“内布拉斯加州”){
$choice=“NE”;
}
如果($(this.val()=“新罕布什尔州”){
$choice=“NH”;
}
如果($(this).val()==“新泽西州”){
$choice=“NJ”;
}
if($(this.val()=“新墨西哥州”){
$choice=“NM”;
}
如果($(this.val()=“Nevada”){
$choice=“NV”;
}
如果($(this.val()==“纽约”){
$choice=“NY”;
}
如果($(this.val()=“Ohio”){
$choice=“噢”;
}
if($(this.val()=“俄克拉荷马州”){
$choice=“OK”;
}
如果($(this.val()=“Oregon”){
$choice=“或”;
}
如果($(this.val()=“Pennsylvania”){
$choice=“PA”;
}
如果($(this.val()=“罗德岛”){
$choice=“RI”;
}
如果($(this.val()=“南卡罗来纳州”){
$choice=“SC”;
}
if($(this.val()=“南达科他州”){
$choice=“SD”;
}
如果($(this.val()=“田纳西州”){
$choice=“TN”;
}
如果($(this.val()=“Texas”){
$choice=“TX”;
}
如果($(this.val()=“犹他”){
$choice=“UT”;
}
如果($(this.val()=“Virginia”){
$choice=“VA”;
}
如果($(this.val()=“佛蒙特州”){
$choice=“VT”;
}
如果($(this.val()=“Washington”){
$choice=“WA”;
}
如果($(this.val()=“威斯康星州”){
$choice=“WI”;
}
if($(this.val()=“西弗吉尼亚州”){
$choice=“WV”;
}
如果($(this.val()=“怀俄明州”){
$choice=“WY”;
}
就这样-

$("<option value=''> -Select City- </option>").appendTo("<?php echo $cname; ?>"); 
$(“-选择城市-”)。附加到(“”);

浏览器处理.js和.html文件。您可以将.js文件的扩展名更改为.php,并使用php导入(包括或需要)

但我必须声明,要使用mysql命令global wpdb和我的mysql查询,我不明白你想说什么。你尝试过解决方案吗?是的,我尝试过你的解决方案,但它不起作用。在上面看到的第三段代码中,文件是.js,位于$department.change(function(){}我必须使用全局wpdb连接到我的数据库以及mysql代码,这就是我使用的原因。在您的解决方案中,您只使用了将.js更改为.php的尝试,但它不起作用。介意检查我的代码吗?