Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing PHP variables into JavaScript
    primarykey
    data
    text
    <p>I am an amateur web developer and I am developing an application that mainly uses JavaScript but needs to use PHP/MySQL because it is a quiz application and I don't want people to see all the quiz answers by going to "View Source." The pages of relevance are: index.html, problems.php, functions.js. The index.html is the main quiz page. The problems.php is the page used to connect to the server and get the answers to the questions on the quiz page. And the functions.js is used to check whether the answers are correct among other things that prompt the app to do something. </p> <p>index.html: </p> <pre><code>&lt;?php include ("problems.php"); ?&gt; ... </code></pre> <p>problems.php</p> <pre><code>&lt;?php // Connect to server $con = mysql_connect("XXXXXXXXXXXXX", "XXXXXXXXXXX", "XXXXXXXXXXXX"); mysql_select_db("problems", $con); if (!$con) { die("Didn't connect"); } $question_num = $_GET["num"]; $sql = "SELECT question FROM questions WHERE num='" . $question_num . "';"; $answer = mysql_fetch_array(mysql_query($sql))['question']; ?&gt; </code></pre> <p>functions.js</p> <pre><code>function checkAnswer(ans, input) { if (ans == input) { alert("Correct!"); } } </code></pre> <p>I am not a PHP developer and know very little PHP (which is why I'm choosing to use JavaScript for the application). But I would like to use the GET method in getting the answers to the quiz questions (when the user clicks "Submit" pass in ?num=1,2,3,4,5). Then pass in the quiz answers obtained from the database to the functions.js checkAnswer function to check the answer. My problem is the pass in the answers to the questions to the functions in JavaScript. Also, it doesn't seem my server is even connecting to the database in my code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload