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);>" ;

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀