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

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

Related

Cannot echo .load function

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

bug php variable to javascript

Ive struggled with a bug which I can simplify to this:-
if I set a variable $test in php and try to access it in javascript it sometimes works, but often doesnt
Im using alert to display the data in javascript... alert( php tag echo $test; close php tag ) if it doesn't show below
<script>
alert(<?php echo $test; ?>);
</script>
example, if in php I set $test="hello"; it does not work. Nothing!
but if I set $test=time(); it does work
why cant I set a simple string and access it in javascript? Its strange it can access a more complicated timestamp but not a string!!
You need to tell JavaScript that you want to alert a string.
alert('<?php echo $test; ?>');
Notice the quotes wrapped around the PHP statement.
You're missing out "".Try this instead.
<script>
alert("<?=$test?>");
</script>
You need to add quotes
alert('<?php echo $test; ?>');

Targeting dynamic id's within a javascript file

In a php file I have tags with dynamic id's.
Example:
<ul id="<?php echo 'ul_'.$xx; ?>">
<li id="test_li">...</li>
</ul>
In a separate JavaScript file I am wanting to use the load function and target that div.
$('<?php echo 'ul_'.$xx; ?>').load('../includes/myphpfile.inc.php #test_li');
My question is, how could I load that php within a JavaScript file?
Notes:- The reason I have it set up this way is due to looping multiple ul's. I just want my ajax file to refresh the li's within the specific ul I am inside of.
Edit: If I use classes instead of Id's it adds my newly inserted rows into all of the ul's with the same class. If I use a plain Id it only works on the first ul. Further explaining why I need to target the way I am.
Unable to find a solution using the path I was on, I looked for alternate methods. Worked out I could add the event in the php file on the submit form button click.
The reason I wasn't using this method at first was because the code was running before it submitted to the DB and therefore was always behind in relation to the new data being submitted.
To my surprise I fixed this by just duplicating the line of code.
<script>
$("<?php echo '#enter_submit_button'.$linkid ?>").click(function(){
/*1*/
$('#<?php echo 'ul__'.$linkid; ?>').load('../includes/pursuit.inc.php <?php echo '#li__'.$linkid; ?>');
$("<?php echo '#insert_new'.$linkid; ?>").fadeOut();
/*2 with delay*/
$('#<?php echo 'ul__'.$linkid; ?>').delay( 800 ).load('../includes/pursuit.inc.php <?php echo '#li__'.$linkid; ?>');
});
</script>
I honestly couldn't say exactly why this works but the logic behind trying the method came from hoping that the double load would pick up the latest submission.
The simplest answer would be to put your JavaScript code into the PHP file inside
<script></script> tags, i.e, just before closing your <body> tag, do this:
<script type="text/javascript">
$('<?php echo 'ul_'.$xx; ?>').load('../includes/myphpfile.inc.php #test_li');
</script>
But if you must use an external js file, then you could do this:
Create a (.php) file and write:
<?php
header("Content-type: application/javascript");
?>
// your js code with php inside (as you would do in HTML)
var othervar='<?php echo $phpVar; ?>';
//<?php //some php ;?>
Then, in your main (html/php) file, put in:
<script src='myjs.php' type='application/javascript'></script>
Hope that solves it.

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)

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