简体   繁体   English

javascript数组中的引用错误

[英]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]. 我需要将php变量$ Q_ID的值存储在索引0的javascript数组中。这是我将其存储在javascript数组记录[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. 这是获取Q_ID值的php代码。 I have placed the php code before tag in the page and the javascript code is in the body at the last position. 我已经在页面中的标记之前放置了php代码,并且javascript代码位于最后位置的正文中。

$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. 我已经在页面中的标记之前放置了php代码,并且javascript代码位于最后位置的正文中。 But everytime I execute this code it shows reference error in firebug console window. 但每次执行此代码时,它都会在firebug控制台窗口中显示引用错误。

ReferenceError: CSE6014 is not defined record[0]=CSE6014;/* Showing refernce error (ReferenceError: CSE6014 is not defi... ReferenceError:CSE6014未定义record [0] = CSE6014; / *显示引用错误(ReferenceError:CSE6014不是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 . 由于$Q_ID$corrstrings ,您需要用quotes string You can use either single quote ' or double quote " . 您可以使用单引号'或双引号"

Use following code(Notice the quotes around the PHP tags): 使用以下代码(注意PHP标记周围的引号):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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