Newbie
Newbie

Reputation: 53

PHP dropdown populated by database to be use javascript

I have this php code that get items from the database and i have to use this drop-down in my JavaScript code to duplicate the same drop-down when a button is clicked

function dup(){  
     }

 <?php 
  include('view/connect.php');

  $sql="SELECT ss_code FROM ss WHERE sn_id = 1";

  $result =mysql_query($sql);
  $dropdown = "<select name='users'>";

  while($row = mysql_fetch_assoc($result)) {
     $b = $row['ss_code'];
     $dropdown .= "\r\n<option value='{$b}'>{$b}</option>";

  }

  $dropdown .= "\r\n</select>";

  echo $dropdown;

 ?>

Upvotes: 1

Views: 209

Answers (2)

techie_28
techie_28

Reputation: 2133

What do you mean same drop down?If you want same dropdown without any change on a button click you can use jQuery .clone().

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

Put all your items in an array and then set a JavaScript variable to the value returned by encoding it as JSON.

var iarray = <?php echo json_encode($items); ?>;

Then iterate over the resultant array and create the pulldown from that.

Upvotes: 1

Related Questions