HTML entity in JavaScript (XML and PHP) - javascript

Newbie in troubles again))) Need yr help guys!
I have an XML file with the data like this:
<Page>
<Content><p>Article content</p>
<h1 style="font-style: italic;">HEADER</h1>
<p>&nbsp;</p>
<p><img alt="" src="/upload/images/1.JPG" style="height:51px; width:144px" /></p>
</Content>
</Page>
now I want to extract this data via PHP & replace html by jQ.
<?php
$file = '111.xml';
$XML = simplexml_load_file($file);
$str =$XML->Content;
$str = htmlspecialchars($str, ENT_QUOTES);
JS('var CONV = "'.$str.'"'); //JS function just puts its parametr between <script... tags
echo $str;
?>
<script type="text/javascript">
$("#debug").html().text(CONV);
</script>
so the result is NOTHING... if i do Echo - it works, if i replace CONV value with smth like "bla bla bla" - it works either. But with encoded or decoded HTML it shows none...
UPDATE:
i still dont know where's the problem. I use "buffer" file as a temporary soultion:
file_put_contents('../plugins/'.pl_DIR().'/buffer.db', $str, LOCK_EX);
JS('$("#editor").load("../../plugins/"+PATH+"/buffer.db")');
Via AJAX the content loads perfectly but anyway Im sure that's diffenetley not the best idea...

You should try replace
$str = htmlspecialchars($str, ENT_QUOTES);
with
$str = htmlspecialchars_decode($str, ENT_QUOTES);
see http://php.net/manual/en/function.htmlspecialchars-decode.php
I had similar problem the other day, and that solved it for me.

Related

JavaScript innerHtml not changing to php session

I am trying to change html button's text with php and javascript but it doesn't give errors and i don't know what is wrong.
My code that i am trying to get working.
At start of the file:
<?php
session_start();
$_SESSION["LOGGED"] = "Log in";
?>
Then comes the button code
<button class='button' id='login'>button</button>
<script type="text/javascript">
<?php
echo "var msg = '" .$_SESSION["LOGGED"] . "'";
?>
document.getElementById('login').innerHTML = msg;
</script>
What is wrong? The text just doesn't change.
Edit: The 2 sciprts php part crashes the script? It says unexpected token <. Removed the php part and putted to .innerHTML = "test"; and the button changed to test. So it should be somewhere at the php part?
If you're using the <button> tag,
document.getElementById('login').innerHTML = msg;
should work.
But if you're using <input type="button">, then you will have to use .value instead of .innerHTML
document.getElementById('login').value = msg;
EDIT: Try this...
<script>
var msg = <?php echo $_SESSION["LOGGED"]; ?>; //Don't forget the extra semicolon!
document.getElementById('login').innerHTML = msg;
</script>
Is your file a .php file and does the php work correctly?
try testing your php by putting
<?php echo "Hello world!"; ?>
at the top of your page :)

PhpStorm highlight part of file as JavaScript with custom start and end lines

I have inherited a PHP project that contains several HTML files with the following structure
<?php $this->placeholder('dom.ready')->captureStart(); ?>
var some = javascript.goesHere();
<?php $this->placeholder('dom.ready')->captureEnd(); ?>
<div class="some html goes here">
<h1> <?php echo "with some php in the middle"; ?> </h1>
</div>
Would it be possible to configure Phpstorm to interpret everything between the placeholder lines as JS and the rest of the file as HTML with some PHP?
I know the file structure is very unfortunate, but I can not refactor it for now...
Thanks
There are at least two interesting possibilities...
1. Heredoc notation
https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
For me the most interesting solution was to transform the JavaScript into a PHP string using the heredoc syntax:
<div>
<div>
<?php $this->placeholder('dom.ready')->captureStart(); ?>
<?= <<<JS
var some = javascript.goesHere();
JS
?>
<?php $this->placeholder('dom.ready')->captureEnd(); ?>
</div>
</div>
Ensure to place the closing identifier in the first column of line
2. Change the filename
When you change your file extension from .php into .js.php PhpStorm will also highlight your JavaScript.
I think this solution is nice because you will always be remembered that JavaScript is in your PHP file. And this has to be cleared out...
Both solutions was found on jetbrains.com. Thanks to Andriy Bazanov:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/207046645-Zend-framework-and-code-formating-in-view-files
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000790544-Syntax-Highligth-on-some-special-cases
Probably not really what you want but if you surround the javascript with a script tab phpstorm will interpret it as js. You would need to change your code to look like:
<script>
<?php $this->placeholder('dom.ready')->captureStart(); ?>
var some = javascript.goesHere();
<?php $this->placeholder('dom.ready')->captureEnd(); ?>
</script>
<div class="some html goes here">
<h1> <?php echo "with some php in the middle"; ?> </h1>
</div>
This article says you can use two file extentions to accomplish something similar without using the script tags.
https://confluence.jetbrains.com/display/PhpStorm/Syntax+highlighting+of+PHP+inside+JavaScript+(and+other+languages)

How can I use HTML code to print it in PHP?

I want to use this HTML code and print it in PHP.
In the HTML code I use this:
<button onMouseover="htmlcode('<img src=\'http://www.chinavalue.net/Special/images/20080529/image/top.jpg\'></img>');">View</button>
How should it be used it in PHP?
I've tried this code:
echo '<button onMouseover="htmlcode('<img src=\'http://www.chinavalue.net/Special/images/20080529/image/$test.jpg\'></img>');">View</button>';
But unfortunately, this is not working.
you mix up your quotes
if you use ' quotes in the echo statement, you cannot use them (or have to escape them) in the string
the best solution here is escape the single quotes in your string, so they don't mark the end of the echo command
echo('<button onMouseover="htmlcode(\'<img src=\'http://www.chinavalue.net/Special/images/20080529/image/top.jpg\'></img>\');">View</button>');
If you want to use it in some kind of an if statement then you can go for this:
<?php if( /*your condition*/ ) :?>
<button onMouseover="htmlcode('<img src=\'http://www.chinavalue.net/Special/images/20080529/image/top.jpg\'> </img>');">View</button>
<?php endif; ?>
Note this way you can write long snippets of html easily, which is not the case with echo.
If this is not conditional, then just do something like this:
<?php // some php code
// some more php
?>
<html>
Your html here
</html>
<?php // next section of php
// more php
?>
The parts between the php sections will be outputed just as if they were put into an echo statement.
If you want to see the html in the browser then you can use.
print htmlspecialchars("<button onMouseover="htmlcode('<img src=\'http://www.chinavalue.net/Special/images/20080529/image/top.jpg\'></img>');">View</button>")

Clean way to Template an HTML code block that only takes title/content parameters, where content may be large and littered with new-lines

I am using Accordion (jQuery) on my school webserver. Currently, my coding-scheme uses PHP/HTML/CSS/Javascript. I started noticing an opportunity for automation/templating when writing the entries for the Accordion modules. I write the following code:
<h3>Title</h3>
<div class="nobg">
<p class="nobg">
<!-- Entry text -->
</p>
</div>
so I am looking for pointers for the best way to template that code based on the following needs:
Adjustable parameters: Title, Content
When making new modules with a large content 'parameter', the creation of that parameter should maintain readability.
Since I am already on PHP, I was thinking maybe some sort of template function:
<? php accordion_entry("Title", "Entry Text" ?>
But the text is usually a lot of HTML: like the following:
PDF
<p>
The release date is 2007 but the pinout seems to check out (I did some small verifications with my PCB). Also, the reference documents are all valid!
</p>
I would like to write that HTML myself in the designated spot where the module will eventually manifest as a whole. Perhaps even cooler would be something like this:
<accordion-entry title="Title">
PDF
<p>
The release date is 2007 but the pinout seems to check out (I did some small verifications with my PCB). Also, the reference documents are all valid!
</p>
</accordion-entry>
I have no idea how to get started creating such a mechanism, or if it's too much trouble to bother.
I found my temporary solution, until someone comes along with something better! Please review! I am no PHP Expert!! :D
The PHP Function:
<?php
function accordionEntry($title, $entry)
{
echo '<h3>' . $title . '</h3>';
echo '<div class="nobg">';
echo ' <p class="nobg">';
echo $entry; // <!-- Entry text -->
echo ' </p>';
echo '</div>';
}
?>
The PHP function call:
<?php accordionEntry(
"GSM0107IG001 - Integration Manual",
'PDF
<p>
The release date is 2007 but the pinout seems to check out (I did some small verifications with my PCB). Also, the reference documents are all valid!
</p>');
?>
Create a partial, and load your content into it along with settings
accordian.phtml (just use .html if you want, doesn't really matter)
<accordion-entry title="<?php $title ?>">
<?php $content ?>
</accordion-entry>
page.html
<div><?= renderPartial('accordian.phtml',array(
'title'=>'GSM0107IG001 - Integration Manual',
'content' => '<p>your html</p>'
)); ?>
partial.php
function partial($partial, $settings){
//will load html from indicated file, and merge passed settings and content into place before returning all $html
// this allows the reuse of the 'partial()' function for other snippets
$template = file_get_contents($partial);
//$settings should be an array, and then your keys can be extracted as variables that match the $settings variables (such as $title) that exist in the .html partial file
extract($settings); //will assign any keys in your array, such as 'title' to php variables of the same name... so in this case $title, and $content
echo $template;
}

Output PHP Code in HTML, literally

My question is very simple. All I want to do is write actual PHP code in HTML, without executing it. For example, I want to write
<div id = "code"><?php
echo 'Bla Bla Bla';
?></div>
I DO NOT want to execute this PHP. I want to literally write the PHP code on the html document, by somehow escaping the <?php tags (I don't want to remove them).
Unfortunately, when I echo it to the browser by doing this:
<div id = "code"><?php echo '<?php
echo "Bla Bla Bla";
?>' ?></div>
I have also tried to put the code into variables, and even that hasn't worked.
My PHP code gets commented out by the browser. I can see it in the View Page Source option in chrome, but the browser thinks it's a comment.
Please provide a solution to my problem.
Thanks in advance.
PS: I am using CodeMirror to output embedded PHP/HTML code using the 'application/x-httpd-php'.
EDIT #1
I can use lt and rt for tags, but I actually can't because CodeMirror starts highlighting code only when it finds <? tags.
Encode your PHP tags by using <?php echo "test"; ?>.
Escape your code with htmlspecialchars():
<div id="code">
<?php echo htmlspecialchars("<?php echo 'Bla Bla Bla' ?>") ?>
</div>
Take a look here: http://www.php.net/manual/en/function.htmlentities.php
<div id = "code"><?php echo htmlentities('<?php
echo "Bla Bla Bla";
?>'); ?></div>
Try this
<div id = "code">
<?php echo htmlentities('<?php echo "Bla Bla Bla"; ?>'); ?>
</div>
try using htmlspecialchars
htmlspecialchars()
if i understood your question properly then you can achive it by following..
<div class="test"><?php $a='<?php hii echo "hey there"?>';?></div>
Solution 1: Your solution would probably be to not name the file .php ... Try .txt in stead. This way the web server would not pass the file to the PHP parser.
Solution 2: Other solution would be to read out the file from another php script that does file() in combination with htmlspecialchars() to the file with the desired code.
Solution 3: If you already have the code in a variable and it outputs in a way that you can see the code in "source view", then you suffice by only using htmlspecialchars()

Categories