Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将php字符串传递到javascript有时不起作用_Php_String - Fatal编程技术网

将php字符串传递到javascript有时不起作用

将php字符串传递到javascript有时不起作用,php,string,Php,String,我如何了解它的工作原理: 目标:在不重新加载页面的情况下创建一个相册查看器,使用img html元素,使用javascript动态更改img SRC,但图像url存储在php变量中,并复制到javascript数组中。 问题:例如有相册,其中有17张图片,如果我在php中回音,那么$i从1-17计数,但在javascript中计数为2-12和15-17,所以忽略1,13,14等。不知道为什么,因为数据没有差异,所以DataPicId[13]未定义,但第15个索引或第12个索引正常获得值 whil

我如何了解它的工作原理: 目标:在不重新加载页面的情况下创建一个相册查看器,使用img html元素,使用javascript动态更改img SRC,但图像url存储在php变量中,并复制到javascript数组中。 问题:例如有相册,其中有17张图片,如果我在php中回音,那么$i从1-17计数,但在javascript中计数为2-12和15-17,所以忽略1,13,14等。不知道为什么,因为数据没有差异,所以DataPicId[13]未定义,但第15个索引或第12个索引正常获得值

while($row = mysql_fetch_array($SQLpic))
{
$i=$i+1;
$PicId=$row['id'];
$Url=$row['url'];
$Desc=" ".$row['description'];
$UpUser=$row['uploader'];
$Update=$row['udate'];
echo"<script>
nr=Number('".$i."');alert(nr);
DataPicId[nr]=Number('".$PicId."');
DataPicUrl[nr]='".$Url."';
DataPicDesc[nr]='".$Desc."';
DataPicUser[nr]=Number('".$UpUser."');
DataPicDate[nr]='".$Update."';
alert(DataPicUrl[nr]);
</script>";
$ThumbUrl=GetNameOnly($Url);
echo "<div id='PicBoxOut'>
<div id='PicBoxIn' onclick=SelectSrc('".$i."');>
<a href='javascript:void(0);'>
<img src='".$ThumbUrl."' border='0'></a>
</div></div>";
while($row=mysql\u fetch\u数组($SQLpic))
{
$i=$i+1;
$PicId=$row['id'];
$Url=$row['Url'];
$Desc=”“.$row['description'];
$UpUser=$row['uploader'];
$Update=$row['udate'];
回声“
nr=编号(“$i.”);警报(nr);
DataPicId[nr]=编号(“$PicId.”);
DataPicUrl[nr]=“$Url.”;
DataPicDesc[nr]=“$Desc.”;
DataPicUser[nr]=编号(“$UpUser.”);
DataPicDate[nr]=“$Update.”;
警报(DataPicUrl[nr]);
";
$ThumbUrl=GetNameOnly($Url);
回声“
";

在没有看到生成的html/javascript的情况下,我猜您的变量正在破坏您的javascript。例如,照片描述中的一个引用

为了确保输出有效的javascript,最好将完整的行集作为json发送到javascript,并使用该json在javascript中构建html

比如:

<?php
// Note that normally you would have done this already in a controller
// and not when you are outputting html
$rows = array();
while($row = mysql_fetch_array($SQLpic))
{
  $rows[] = $row;
}
?>

<script>
var json = <?php echo json_encode($rows); ?>,
    object = JSON.parse(json);

// now `object` will contain all your rows in a javascript object / array

</script>

var json=,
object=JSON.parse(JSON);
//现在,“object”将包含javascript对象/数组中的所有行

很可能是由于缺少字符引号而导致代码中断。请使用浏览器开发控制台,检查1.运行时是否有任何输出,2.生成的脚本代码。可能无效。能否(1)将代码格式化为可读性较高,(2)显示完整循环,(3)删除不相关的代码,(4)显示生成的客户端JavaScript?我们无法真正执行它并为您调试它。