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.