Passing PHP variables into JavaScript

Waybribia

New Member
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. index.html: \[code\]<?php include ("problems.php"); ?>...\[/code\]problems.php\[code\]<?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'];?>\[/code\]functions.js\[code\]function checkAnswer(ans, input) { if (ans == input) { alert("Correct!"); }}\[/code\]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.
 
Back
Top