2015-12-10 73 views
-1

我可以在一個javascript文件之間使用php嗎? 我的JavaScript文件還有更多的功能從PHP腳本 這是我的JS文件我可以在.js文件中包含一個php腳本嗎?鏈接下拉

var hide_empty_list=true; 

addListGroup("vehicles", "Area"); 

addList("Area", "Select a Area", "", "dummy-maker"); 
addList("Area", "Alappakkam", "Alappakkam", "Alappakkam"); 
addList("Area", "Porur", "Porur", "Porur"); 
addList("Area", "Vanagaram", "Vanagaram", "Vanagaram", 1); 

addList("Alappakkam", "Select Volunteer", "", "dummy-Alappakkam"); 
addList("Alappakkam", "Monish", "car", "Alappakkam-Cars"); 
addList("Alappakkam", "Kala", "suv", "Alappakkam-SUVs/Van"); 
addList("Alappakkam", "Akil", "truck", "Alappakkam-Trucks", 1); 

addList("Porur", "Select Volunteer", "", "dummy-honda"); 
addList("Porur", "Srinivasan", "car", "Honda-Cars"); 
addList("Porur", "Lingesh", "suv", "Honda-SUVs/Van", 1); 
addList("Porur", "Akash", "suv", "Honda-SUVs/Van", 1); 
addList("Porur", "Prakash", "suv", "Honda-SUVs/Van", 1); 

addList("Vanagaram", "Select Volunteer", "", "dummy-chrysler"); 
addList("Vanagaram", "Sudharshan", "car", "Chrysler-Cars", 1); 
addList("Vanagaram", "Sarath", "suv", "Chrysler-SUVs/Van"); 

而且我的PHP腳本是

<?php 
# here database details  
@mysql_connect('mysql.hostinger.in', 'u467215xxx_xxxx', 'xxxxxxx'); 
@mysql_select_db('u467215728_chnai'); 

$sql = "SELECT address FROM member "; 
$result = mysql_query($sql); 

echo "<select name='address'>"; 
while ($row = mysql_fetch_array($result)) { 
    echo "<option value='" . $row['address'] ."'>" . $row['address'] ."</option>"; 
} 
echo "</select>"; 
?> 

其實我想在這裏做的是從獲取數據我數據庫並進入下拉菜單。我完成了一個單一的下拉腳本,但實際上鍊接下拉。

+1

您是否嘗試過改變從'.js'文件類型爲'.php'? (和更改'src'屬性指向新的文件擴展名) – Terminus

+0

我想你不明白我的問題兄弟..我想合併該PHP腳本與JS腳本.. – Monish

+0

我想我沒有。有什麼你認爲你可以添加澄清或這是儘可能多的解釋,你可以給? – Terminus

回答

0

你不能在js文件中添加php代碼。 您可以重命名你的.js文件作爲.php然後添加你的js代碼的腳本標記內,如:

<?php 
//your php code ..... 
?> 
<script> 
//your javascript code 
</script> 
+0

'

0

可以解析PHP中的JS文件,並在瀏覽器中呈現之前添加動態數據,但不是這樣做的方式。使用ajax調用,從服務器獲取信息,並在您的JS中使用它來修改DOM。像這樣的東西,如果你使用JQuery將幫助

$.get('super-site.com/info', function(data) { 
    data.each(function(item){ 
    $('#theFormSelect').append('<option>' + item.text + '</option>'); 
    }); 
}); 

將幫助你入門。服務器必須返回一個JSON對象纔有意義,換句話說,這是未經測試的,不可靠的,它只是提示你需要做什麼,它會失敗,YMMV,如果它打破了你的咖啡機,我不承擔任何責任。

相關問題