jaspher chloe
jaspher chloe

Reputation: 529

Passing php database result to javascript array

I have to create a dropdown list and a button that will add another dropdownlist once click. i used this javascript code:

var array = ["Volvo","Saab","Mercades","Audi"];

        var cell2 = row.insertCell(1);
        var element2 = document.createElement("select");
        element2.name="activity_dropdown"
        element2.id="activity_dropdown"
        cell2.appendChild(element2);

        for (var i = 0; i < array.length; i++) {
            var option = document.createElement("option");
            option.setAttribute("value", array[i]);
            option.text = array[i];
            element2.appendChild(option);
        }

but i want that my var array will come from the database result query. what can i do to pass php array to javascript array?

by the way.. i have tried to used json_encode but it seems not working.. here is the my code:

for data.php

    <?php
    include('connection/dbConn.php');


// get data and store in a json array
$activity=$conn->query("SELECT activity_name FROM rehab_activities");


while ($row =mysqli_fetch_array($activity)) {
    $activities[] = array(
    'actvityName' => $row['activity_name']
    );
}

echo json_encode($activities);
 ?>

and try it here, but nothing comes out:

<script>
 jQuery(document).ready(function(){
    jQuery("#add").click(function(){
    var res = new Array(); 
        jQuery.getJSON("data.php", function(data) {
    var i= 0;
    while(i<data.myarray.length){
                res[i]=data.myarray[i];
             i++;
    }
  jQuery("#result").html(res[0]);
            });
        });

    });
</script>
<body>
    <input type="button" id="add">
    <div id="result"></div>
</body>
</html>

Upvotes: 0

Views: 9791

Answers (2)

Felix Runye
Felix Runye

Reputation: 2521

I had a similar problem in vue js, renamed my 'app.js' file to 'app.php', where i added all my php functions and changed the reference script tag to

    <script> type='text/javascript' src='app.php' </script>

Worked For me!

Upvotes: -1

Dante
Dante

Reputation: 273

Make php write that part of your script while it loops through your query. Like so:

var array = [
    <?php
        $query = mysql_query("SELECT * FROM cars");
        while ($car = mysql_fetch_assoc($query)) {
            $car_name = $car["name"];
            echo "'$car_name',";
        }
    ?>
];

For this to work, your file must be a php file and not a javascript file, since inside a php file you can also write html and therefore, javascript.

I personally solve this by making a file called db-to-js.php that only contains javascript arrays created by php. Whenever the database is modified, one or more of these javascript arrays are also modified since they are created every time this file is executed.

Lastly, you have to include the file where you need it as a script:

<script type="text/javascript" src="db-to-js.php"></script>

I hope this helps.

Upvotes: 5

Related Questions