-1

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.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
Monish
  • 105
  • 1
  • 3
  • 1
    Have you tried changing the file type from `.js` to `.php`? (And changing the `src` attribute to point at the new file extension) –  Dec 10 '15 at 12:57
  • i think u dnt understand my question bro.. i want to merge that php script with js script .. – Monish Dec 10 '15 at 13:04
  • I guess I didn't. Is there anything you think you could add to clarify or is this as much explanation as you can give? –  Dec 10 '15 at 13:23
  • Bro actually i dont know anything about php or java ... im just a basic beginner... actually i want to add that php inside the js file .. how can i do that ? – Monish Dec 10 '15 at 13:47
  • 1
    **Warning**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Dec 10 '15 at 13:51

2 Answers2

0

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.

Purefan
  • 1,498
  • 24
  • 44
0

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>
  • ` – Gerald Schneider Dec 10 '15 at 13:57
  • after I do that u want me to save it as php file? – Monish Dec 10 '15 at 14:03
  • Actually can u help me with something else ? – Monish Dec 11 '15 at 10:12
  • I want to that chain dropdown automatically get the data from by database and list it For eg;- In my database it is listed as USERNAME AREA MOBILE NUMBER aaa Porur 9999006600 bbb alapkkam 9999990000 ccc Porur 8888554477 .... now i want the drip down to be listed if a user selects porur i want both the name aaa and ccc listed in the next dropdown and I have two text box next to it .. and all the data which is selected and entered should be sent to the username as SMS .. i have the SMS script also ..and the sms should go only to the selected USERNAME – Monish Dec 11 '15 at 10:13
  • Can u help me with the previous question i asked bro? – Monish Dec 11 '15 at 13:18
  • @Monish This is a little bit more complicated... To do this, I think the best approach is to use jquery, so you can use async requests to do this. Another way (not much recommended) is to put your dropdown inside a HTML form and submit to a PHP file, updating the information and, in the end, sending the SMS. – Everton Mendonça Dec 11 '15 at 18:31