我正在尝试使用PHP显示图像位置。下面的JS代码适用于我。当我尝试回显下面的JS代码时,它停止工作。$dbid
是汽车的id。我可以导入与汽车id相对应的汽车图像。
1)JS代码
var CarImage = document.createElement('img');
CarImage.setAttribute('src', '<?php echo importCarImageLocation($dbid);?>');
document.body.appendChild(CarImage);
结果:代码工作正常,找到并加载了图像。
2)尝试回显js代码
echo
"var CarImage = document.createElement('img');
CarImage.setAttribute('src', '<?php echo importCarImageLocation($dbid);?>');
document.body.appendChild(CarImage);";
结果:
Notice: Undefined variable: dbid in C:'xampp'htdocs'testing'test3.php on line 9
3)使用php解决方案创建元素、设置属性并回显图像位置
$dom = new DOMDocument('1.0');
$test = $dom->createElement("img");
$testattribute = $dom->createAttribute("src");
$testattribute->value = "<?php echo '/images/cars/1.jpg' ?>" ;
$test->appendChild($testattribute);
$dom->appendChild($test);
echo $dom->saveHTML();
结果:元素和属性已创建,但找不到图像位置。浏览器显示损坏的图像,这可能表明它找不到该图像。
4)使用我的获取图像位置的功能作为源
$dom = new DOMDocument('1.0');
$test = $dom->createElement("img");
$testattribute = $dom->createAttribute("src");
$testattribute->value = "<?php echo importCarImageLocation($dbid);>" ;
$test->appendChild($testattribute);
$dom->appendChild($test);
echo $dom->saveHTML();
结果:Notice: Undefined variable: dbid in C:'xampp'htdocs'basel'testing'test3.php on line 12
5)不回显,也不使用我的获取图像位置的功能
$dom = new DOMDocument('1.0');
$test = $dom->createElement("img");
$testattribute = $dom->createAttribute("src");
$testattribute->value = "images/cars/1.jpg" ;
$test->appendChild($testattribute);
$dom->appendChild($test);
echo $dom->saveHTML();
结果:找到图像并加载
你能告诉我为什么数字2、3和4不起作用吗。
我很感激你抽出时间。
从所有$testattribute->value = "<?php echo '/images/cars/1.jpg' ?>" ;
$testattribute->value = "<?php echo importCarImageLocation($dbid);>" ;
的这些行中删除PHP标记