Generating XML which contains HTML in PHP - Problems with variables [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am sitting on a little problem here:
I have a php file which generates xml data.
$requestXmlBody .= "<Version>$compatabilityLevel</Version>";
Now there are variables pulled from the upper php code and also HTML is generated
$requestXmlBody .=
'<Description>
<![CDATA[
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--some JS-->
</script>
<img src="http://www.myserver.com/pic.jpg" class="etalage_thumb_image" />
</body>
</html>
]]>
</Description>';
Now strangely I cannot mix variables and HTML Code.
As you can see I use CDATA for the HTML. I want to use a variable for the image name rather than a fixed link. So the code would look like this
$requestXmlBody .= '<Description>
<![CDATA[
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--some JS-->
</script>
<img src="$imagelink" class="etalage_thumb_image" />
</body>
</html>
]]>
</Description>';
But this just does not work. I tried this
$requestXmlBody .= '<Description>
<![CDATA[
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--some JS-->
</script>
<img src="]]>$imagelink<![CDATA[" class="etalage_thumb_image" />
</body>
</html>
]]>
</Description>';
But also this will not work. I even tried to hand over the php variable (which I grab from a session btw) to a JS variable and include it with document.write
Still no success.
This one would work
$requestXmlBody .= '<Description>
$imagelink
</Description>';
But not together with the generated HTML code as you can see above.
Any help is appreciated.
Thanks

Separate concerns. Don't do several things at once. If you split your embedded HTML into its own variable, it gets much easier.
As soon as you have 'freed' the HTML string from the XML context, you'll see that the problem still exists. It is caused by quoting the string with single quotes, which prevent interpolation. You have to use string concatenation instead of embedding the variable directly.
$description = '<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--some JS-->
</script>
<img src="' . $imagelink . '" class="etalage_thumb_image" />
</body>
</html>';
$requestXmlBody .= '<Description>
<![CDATA[' . $description . ']]>
</Description>';
Be sure that your HTML string does not contain a CDATA section itself, since CDATA sections cannot be nested.

The best approach will be to use the writeCData method.
$link= 'link goes here';
$imagelink = '<img src="'.$link.'" /> ';
// serve xml doc as xml
header('Content-type: application/xml');
// set up the document
$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('description');
// CData output
$xml->writeCData($imagelink);
$xml->endElement();
// end the document and output
$xml->endElement();
echo $xml->outputMemory(true);

Related

Error in HTML code inside a php variable

I have this variable which contains HTML code and other variables. I am doing something wrong with the syntax but i don't know what. Help is appreciated.
$message = "
<html>
<head>
<title> Statistics</title>
</head>
<body>
<p> The start was: " .$day."
<p> The end was: " .$day."
</body>
</html>
";
Like #Fabian Gr pointed out; You didn't close the <p> Tags... but then; not closing the <p> Tags is not enough to halt the Execution of PHP Script like you mentioned in the Comments. That Part is purely HTML-based and should not affect PHP by any means... In the worst case, your wrong Semantics could simply swallow the Markup but not throw any PHP Warning or Error....Perhaps you could just save yourself the hassles and use PHP's heredoc like so:
<?php
$message =<<<MSG
<html>
<head>
<title>Statistics</title>
</head>
<body>
<p>The start was: {$day}</p>
<p>The end was: {$day}</p>
</body>
</html>
MSG;
You just need to close the Tag.
so the solution of your problem Should be:
$message = "
<html>
<head>
<title> Statistics</title>
</head>
<body>
<p> The start was: " .$day."</p>
<p> The end was: " .$day."</p>
</body>
</html>
";

Using PHP to change HTML elements [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a simple HTML page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p id="demo">Nothing here</p>
<button onclick="?">Button</button>
</body>
</html>
and a separate PHP file:
<?php
// Load and exec craigslist
$site = curl_init("https://www.craigslist.org/about/sites");
curl_setopt($site, CURLOPT_RETURNTRANSFER, true);
$target = curl_exec($site);
$dom = new DOMDocument();
#$dom -> loadHTML($target);
// Save logo text
$title = $dom -> getElementById('logo') -> nodeValue;
?>
Here's what I want to do:
When I click the button on the HTML page, I want the PHP script to run so that I can get the craigslist title and store it a PHP variable ($title). I want to replace the text in <p id="demo> ("Nothing here") with the text stored in $title ("craigslist").
How do I do this?
Change your html to this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p id="demo">Nothing here</p>
<button onclick="myFunc()">Button</button>
<script>
function myFunc() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("demo").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","file.php",true); //Change file.php to the location of your php file
xmlhttp.send();
}
</script>
</body>
</html>
And add this line to your PHP file after setting the title variable
echo $title;
Now of course best case scenario you would use jQuery's built-in AJAX but this works well enough

file_put_contents in head of another file using php

So, Im creating a library for other uses, but how can I make content from a file specificly go within the <head> or <body> html tag attribute etc...
for example, this is what im trying to make.
<html>
<head>
<?php include('content/starter/library.php'); ?>
<!-- From that included file, theres a script that put content in the head.-->
</head>
<body>
<!-- From that included file, theres a script that put content in the body -->
</body>
</html>
Im just trying to find another way instead of making multiple files for specific sections and do
<html>
<head>
<?php include('content/starter/library_head.php'); ?>
</head>
<body>
<?php include('content/starter/library_body.php'); ?>
</body>
</html>
Which I don't really want to do. Im not very good with javascript so, There no hope of me trying to figure out how to do this with javascript. Thanks for the answers in the future.
If you want to use one file (as your questions suggests) then one method is to create variables or functions in your library.php file and then echo them in your template
// contents of the library.php file...
<?php
$head_content = "put your <head> content here";
$body_content = "put your <body> content here";
?>
// your HTML file...
<?php include('content/starter/library.php'); ?>
<html>
<head>
<?php echo $head_content ?>
</head>
<body>
<?php echo $body_content ?>
</body>
</html>
UPDATE
To answer the question in your comment, here's an example using a function. You can put all of your code in a function and then just echo that anywhere in your document.
<?php
// contents of library.php...
function head() {
$return = '<link href="file.css" rel="stylesheet">';
$return .= '<link href="another_file.css" rel="stylesheet">';
return $return;
}
// your HTML file...
<html>
<head>
<?php echo head(); ?>
</head>
PHP functions explained: http://www.w3schools.com/php/php_functions.asp

Generating JavaScript with PHP

I found a tutorial on PHP.net in the manual "PHP and HTML" and there is an example, Generating JavaScript with PHP.
I am trying out a simple demo version with this on my own to learn how to do this so I can later attempt something more complex. Right now, I'm simply trying to declare a string variable in PHP (an address to a JPG file) and then through JavaScript (created in the PHP script) change the src of an IMG element to this new address.
Someone suggested something with JSON, which I have a little experience with, but only with posting to textfile using script in a PHP file. I am not sure if I can use a GET request or something, I honestly have no clue. I just didn't think this would be that complicated.
Here is the link to my page where I am trying to do this.
As you see, I've actually been trying to do the opposite of creating the JavaScript in PHP, instead I was trying to embed the PHP within the JavaScript, which is what someone originally suggested to me, which didn't work. So that is why it is like that.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<?php
$srcmsg = 'http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg';
?>
<script type="text/javascript">
//<![CDATA[
//
var msr = "<?php echo $srcmsg; ?>";
window.onload = document.getElementsByTagName('img').src= msr;
//]]>
</script>
</head>
<body><img src="#" alt="Picture of the world" height="42" width="42" />
</body>
</html>
SOLUTION: this was discovered by Orangepill and Fred....
it turns out that one of the big problems was the way my server was not able to parse the script in the html file so I had to put it in a PHP file instead. then there was an issue with interpreting the short_open tags in the xml declaration. so here is how it ended up for it to get working: keep in mind this is a .php file NOT .htm
<?php echo "<", 'xml version="1.0" encoding="UTF-8" standalone="no" ?'; ">\n"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript">
//<![CDATA[
//
window.onload = function (){
var msr = '<?php $srcmsg = "http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg"; echo $srcmsg; ?>';
var x = document.getElementsByTagName('img')[0];
x.src = msr;
}
//]]>
</script>
</head>
<body><img src="#" alt="Picture of the world" height="42" width="42" />
</body>
</html>
getElementsByTagName returns a NodeList (an array like object) so you have to do
window.onload = document.getElementsByTagName('img')[0].src= msr;
to do the first image.
<?php
$srcmsg = 'http://www.newyorker.com/online/blogs/photobooth/NASAEarth-01.jpg';
echo<<<_HTML
<script type='text/javascript'>
window.onload = document.getElementsByTagName('img').src= $srcmsg;
</script>
_HTML;
?>

Why isn't this EOF javascript code appearing in the html when it is echoed?

In this php code I use the heredoc EOF to insert some javascript:
$room= <<<EOF
<script type="text/javascript" charset="utf-8">
test;
</script>
EOF;
when I try to echo $room it doesn't appear:
echo "<li style=\"text-align: center;\"><img src=\"example.com\" width=\"264\" height=\"198\" alt=\"\" /> $room</li>";
($room doesn't appear in the html).
however if I do the same with:
$room= <<<EOF
test;
EOF;
Then the word test gets echoed in my list html element.
EDIT - to clarify, nothing appears in the source of the html when I do the first echo attempt (the list appears, but no script tags or test inside it).
<script type="text/javascript" charset="utf-8">
test;
</script>
basically means nothing to javascript. I'd guess you want (in a sense of outputting anything at least):
<script type="text/javascript" charset="utf-8">
document.write('test');
</script>
If you literally want to display the script tag, try:
echo '<li style="text-align: center;"><img src="example.com" width="264" height="198" alt="" />', htmlspecialchars($room), '</li>';

Categories