1
<select name="zone"  id="zone" style="width:100px; height:22px; float:left; margin:0px 0px 0px 0px;">


<?php
    $sql33="select * from tb_zone where nation='Domestic'";
    $query33=mysql_query($sql33);
    while($row33=mysql_fetch_array($query33)) {
?>

<option value="<?php echo  $row33['zone_id']; ?>"><?php echo $row33["zone_name"]; ?></option>

<?php  } ?>

</select>

Hello as above there is a simple script in PHP that is fetching values from database and displaying it. My question is can we do the same task in JavaScript. Means can we fetch values from database and display them in <select> in JavaScript?

noobie-php
  • 6,817
  • 15
  • 54
  • 101
user3181292
  • 89
  • 2
  • 12
  • 1
    You can use javascript(AJAX) to access that code and display the result into the form as u said but you cannot do the javascript connect to the database and retrieve the data as it is client side. [Here is a very simple example of what I mentioned using JQuery](http://stackoverflow.com/questions/5298401/basic-php-and-ajax), you can also make the JQuery insert the data directly to the form in question. – Prix Jan 22 '14 at 13:32
  • Javascript can't connect to database as for now – Vikas Arora Jan 22 '14 at 13:34
  • ohk so if i apply ajax..then i have to call other page where this select query have to be written right?? – user3181292 Jan 22 '14 at 13:36
  • @user3181292 yes, that is how it would work. – Prix Jan 22 '14 at 13:36
  • is this for performance issues, is `$sql33` an indication of 33+ querys on the page. If so ajax wont help make a bad thing good. – Lawrence Cherone Jan 22 '14 at 13:39
  • no that's just a name 33 means nothng...its just a name that i have given to the uery – user3181292 Jan 22 '14 at 13:51

1 Answers1

-1

The trick is to use AJAX to do it. If you're using jQuery read up on using $.ajax here http://api.jquery.com/jquery.ajax/

Klemen Tusar
  • 9,261
  • 4
  • 31
  • 28