doutuan9357 2016-07-17 10:12 采纳率: 100%
浏览 54
已采纳

使用PHP时未加载图像位置

I am trying to show the image location using PHP. The JS code below works for me. When I try to echo the JS code below it stops working. $dbid is the id of the car. I am able to import the car image which corresponds to the id of the car.

1) JS code

var CarImage = document.createElement('img');
CarImage.setAttribute('src', '<?php echo importCarImageLocation($dbid);?>');
document.body.appendChild(CarImage);

Result: The code works and the image is found and loaded.

2) Trying to echo the js code.

echo
"var CarImage = document.createElement('img');
CarImage.setAttribute('src', '<?php echo importCarImageLocation($dbid);?>');
document.body.appendChild(CarImage);";

Result:

Notice: Undefined variable: dbid in C:\xampp\htdocs\testing\test3.php on line 9

3) Using php solution of creating the elements and setting the attributes and echoing the image location

$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();

Result: The element and the attributed are created, but the image location is not found. The browser is showing a broken image, which probably indicates that it cannot find the image.

4) Using my function of getting the image location as the source

$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();

Result: Notice: Undefined variable: dbid in C:\xampp\htdocs\basel\testing\test3.php on line 12

5) Not echoing nor using my function of getting the image location

$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();

Result: The image is found and loaded

Can you tell me why number 2, 3 and 4 do not work.

I appreciate your time.

  • 写回答

1条回答 默认 最新

  • duangou1868 2016-07-17 10:15
    关注

    remove PHP tag from these lines from all $testattribute->value = "<?php echo '/images/cars/1.jpg' ?>" ; $testattribute->value = "<?php echo importCarImageLocation($dbid);>" ;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上