Putting php in javascript

I have the following piece of javascript and php codes. I want to connect them together. I will further explain what I would like to do after showing the codes.

Javascript Code:

var group_select = '<select id="group_select" style="margin-bottom:0px;">';
         var i;
         var customer_group = <?php echo json_encode($customer_group);?>;
          for (i = 0; i < customer_group.length; ++i) {
            if (customer_group[i].group_id == table_column_3){
              group_select = group_select+'<option value='+customer_group[i].group_id+' selected>'+customer_group[i].group_name+'</option>';
              group_select = group_select+'<option value='+customer_group[i].group_id+'>'+customer_group[i].group_name+'</option>';
          group_select = group_select+'

This piece of javascript code creates a drop-down list and there are 10 options to select from – the options are from a table called group in the database.

$result = mysqli_query($db, "SELECT group_id FROM customer WHERE customer_id='$customer_id'");
$obj = mysqli_fetch_object($result);
$result2= mysqli_query($db, "SELECT group_name FROM group WHERE group_id='$obj'");
$rowdata = mysqli_fetch_object($result2);
for($i = 0;$i => 9;$i++){
    if($i == $rowdata->group_name){
        echo "<option selected='selected' value='$i'>$i</option>";
        echo "<option value='$i'>$i</option>";

This piece of php code aims to show by default the current group type of the customer by obtaining it from the database and make it the default selected option in the drop-down list.

Please show me how to combine the php code into the javascript code. It is a really important task.

I have just started web development. Please try to help me as much as you can. All your help will be appreciated. Thank you very much in advance.

Source: php

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.