简体   繁体   中英

reference error in javascript array

I need to store the value of php variable $Q_ID in javascript array at index 0. Here is my code for storing it in javascript array record[0].

var record= [];
var choice= [];
var correct=[];

 record[0]=<?php echo $Q_ID ?>;/* Showing refernce error (ReferenceError: CSE6014 is not defined record[0]=CSE6014) */;

 correct[0]=<?php echo $corr ?>;

And this is the php code to get the value of Q_ID. I have placed the php code before tag in the page and the javascript code is in the body at the last position.

$sql= mysql_query( "select * from questions where Q_ID like '{$code}%' order by RAND() limit 1" ) or die(mysql_error());

$rows = mysql_fetch_array($sql);
$Q_ID = $rows['Q_ID'];
$question= $rows['Question'];
$opt1=$rows['Option_1'];
$opt2=$rows['Option_2'];

I have placed the php code before tag in the page and the javascript code is in the body at the last position. But everytime I execute this code it shows reference error in firebug console window.

ReferenceError: CSE6014 is not defined record[0]=CSE6014;/* Showing refernce error (ReferenceError: CSE6014 is not defi...

I don't know what I am doing wrong. Please help me. Thanks in advance.

As $Q_ID and $corr are strings , you need to surround string with quotes . You can use either single quote ' or double quote " .

Use following code(Notice the quotes around the PHP tags):

record[0] = '<?php echo $Q_ID ?>';
correct[0] = '<?php echo $corr ?>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM