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()
Related
I'm having issue when echoing alert() in php. It show everything in the string.
echo "<script type='text/javascript'>alert('Thank you for your enquiry, we will contact you as soon as possible, have a nice day!');</script>";
Output
You can use javascript code between ending and starting php tags like below,
?>
<script type="text/javascript">
alert('Thank you for your enquiry, we will contact you as soon as possible, have a nice day!');
</script>
<?php
// rest of the php code will go here.
?>
OR try this please,
<?php
echo '<script type="text/javascript">';
echo 'alert("Thank you for your enquiry, we will contact you as soon as possible, have a nice day!");';
echo '</script>';
?>
Solution: I fixed it by removing everything. and just echo the message directly without the alert(). I'm not sure why it behave like this because this is my custom plugin. I will update if I found the source of this behavior. Thank you for all the replies.
echo 'Email Successfully Sent. We will reply soon!';
For some reason this code doesn't work. Adding \ to the front of the $ doesn't change anything. Can anyone see the problem?
$commentid = 2; //for example
echo "<script>";
echo "<div id = 'commentinput".$commentid."'>";
echo "</div>";
echo "$('#commentinput".$commentid."').load('editcomments2.php')";
echo "</script>";
Do it like this:
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
I'd suggest not using echo to output HTML (in most cases anyway), but closing the PHP tag instead.
This becomes (edited with a loop example, see comments below):
<?php while ($commentid = $dbObject->dbMethod()): ?>
<div id="commentinput<?= $commentid ?>">
</div>
<script>
$('#commentinput<?= $commentid ?>').load('editcomments2.php');
</script>
<?php endwhile ?>
Note that in this case, the <script> portion should probably be outside of the loop, with a jQuery selector matching all the comment inputs.
So besides all of the other answers, I found another solution to!
When echoing out js code, like
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
//and
echo "alert('stuff')";
it might then print it out like: $("#commentinput2").load("editcomments2.php")alert('stuff') and the code will get confused. So lastly I need to added a semicolon to prevent the program from reading all my code as just 1 line:
echo '$("#commentinput'.$commentid.'").load("editcomments2.php")';
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> </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.
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)
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>")