RobiZzT 5 days ago

How can I pass data from database that I uploaded on PHP to JavaScrpit?

I have to make a quiz that will upload from database available choices for answers. I know how to upload from database using PHP. But I made quiz using JavaScript. Is there anyway to to solve this? I tried something like this in .php file:

echo 'new Question("Find correct word.", ["'. $row['right'] .'",
"'. $row['wrong1'] .'", "'. $row['wrong2'] .'", 
"'. $row['wrong3'] .'"], "'. $row['right'] .'"),';

And this is where I want to upload words from database in .js file:

var questions = [
    new Question("Find correct word.", 
           ["right", "wrong1", "wrong2", "wrong3"], "right"),

Can you someone help me?


Jonas w 5 days ago

What about:

<script src="js.php"></script>

With the following code:

var questions=[<?php
echo 'new Question("Find correct word.", ["'. $row['right'] .'", "'. $row['wrong1'] .'", "'. $row['wrong2'] .'", "'. $row['wrong3'] .'"], "'. $row['right'] .'"),';

Ahmad Mobaraki 5 days ago

You can echo any php variable in javascript code!

for example :

// in php : 

$val = 23 ;

// in js :

var val = <?php echo $val ?>

With this way , you can use any php variable in Javascript code, it's because php in compiled in sever before javascript compiles in browser

Post Status

Asked in 5 days ago
Viewed 2,648 times
Voted 4
Answered 2 times


Leave an answer