在.js文件中插入php代码

时间:2014-03-14 07:44:30

标签: 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="<?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>

我原来的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); 
} 

代码很好,但它是一个非常长的列表。成千上万的城市。 所以我决定添加看起来像这样的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);
        }
?>

});
});

我确信这些代码不起作用,因为php块没有在notepad ++中突出显示。任何帮助都非常感谢。谢谢 编辑:我正在使用wordpress这个

2 个答案:

答案 0 :(得分:0)

就像这样 -

$("<option value=''> -Select City- </option>").appendTo("<?php echo $cname; ?>"); 

答案 1 :(得分:0)

浏览器处理.js和.html文件。您可以将.js文件的扩展名更改为.php,并使用php(include或require)

导入它