Can I use php in between a javascript file? My javascript file has more functions from the php script This is my js file
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");
And my php script is
<?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>";
?>
Actually what I am trying to do here is fetch the data from my database and enter into the drop down. I completed the script for a single drop down but here it is actually chained drop down.
You can't add php code inside a js file. You can rename your .js file as .php and add your js code inside a script tags, like:
<?php
//your php code .....
?>
<script>
//your javascript code
</script>
You can parse a JS file in PHP and add dynamic data before it is rendered in the browser, but that is not the way to do this. Use an ajax call, GET information from the server, and use it in your JS to modify the DOM. Something like this if you're using JQuery will help
$.get('super-site.com/info', function(data) {
data.each(function(item){
$('#theFormSelect').append('<option>' + item.text + '</option>');
});
});
will get you started. The Server must return a JSON object for this to make sense, in other words, this is untested, unreliable, its only a hint to what you need to do, it will fail, YMMV, I take no responsibility if it breaks your coffee maker.