如何在.js文件中插入一段php代碼? 這裏是我的ziphelp.php在.js文件中插入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);
}
的代碼是不錯,但它是一個very2x長長的名單。成千上萬的城市。 所以我決定要加入哪個是這個樣子的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塊沒有在記事本中突出++這些代碼不起作用。任何幫助真的很感激。感謝 編輯:我使用WordPress的這一個
但我必須聲明以使用mysql命令全局wpdb和我的mysql查詢 – user3391847
我不是得到你想說的話。你嘗試過這個解決方案嗎? –
是的,我試過你的解決方案,但它不工作。在我上面看到的第三塊代碼中,文件是.js,在$ department.change(function(){}中我必須使用全局wpdb連接到我的數據庫以及mysql代碼,這就是爲什麼我使用。你的解決方案,你只在下拉菜單中使用了<?php。那麼我之前的所有其他的PHP代碼如何?對不起,這很難解釋:( – user3391847