This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 8 years ago.
I have a div that I want to put one image inside, how could I do something like that:
Javascript:
<script>
document.getElementById("people").innerHTML =
document.getElementById("people").innerHTML + "<img src='farmer.gif'>";
</script>
Html:
<html>
<body>
<div id = "people"></div>
</body>
</html>
in PHP? Im so fustrated I can do it in JS but I need to do it in PHP but I don't know how...
Hope you can help me! :D
Fast VERY DIRTY WAY:
do ob_start(); at the first line of the script
do echo "<div>%imag1flag%</div>";
do $content = ob_get_clean(); at the end of the script
change contnet with $content = str_replace('%imag1flag%','<img ...>',$content);
But wonder why you didnt paste the image in the first place eg:
echo "<div><img ...></div>";
Keep in Mind Javascript is Statefull PHP is NOT Statefull.
When u got that, maybe you will laugth about your Question ;)
Bye!
Related
This question already has answers here:
Why are my php tags converted to html comments?
(12 answers)
How to enable PHP short tags?
(21 answers)
Closed 5 months ago.
Sometimes I see question mark tags in html, like this.
<? /* JavaScript code goes here*/ ?>
The browser turns these question mark tags into html comments, like this.
// javascript
console.log(document.querySelector("div").innerHTML);
<!--html-->
<div>
<? console.log("this is javascript") ?>
</div>
Editors like vscode even syntax highlight the code inside these <? tags. But let's say I have code inside these tags like this.
x.map(function(item){return + item + '<li></li>'}).join('')
This is how it renders:
// javascript
console.log(document.querySelector("div").innerHTML);
<!--html-->
<div>
<?
x.map(function(item) {
return + item + '<li></li>'
}).join("");
?>
</div>
So how do these question mark tags work? Is it even valid HTML?
This question already has answers here:
trimming a string within a div
(2 answers)
How can I get the data attribute from a php variable?
(2 answers)
Closed 2 years ago.
I am attempting to call a comments section through a modal, for this purpose I need to pass a unique id along with #display_comment . It works when I hard code is such as #display_comment1, #display_comment2. I need to know how to pass the value as a variable.
I am using dom-target to pass it as variable in myData1, but it wont work. The console shows #display_comment & 1 on the next line.
<div id="dom-target" style="display: none;">
<?php
echo htmlspecialchars($test['id']);
?>
</div>
var div = document.getElementById("dom-target");
var myData1 = div.textContent;
load_comment();
function load_comment()
{
$.ajax({
url:"fetch_comment.php",
method:"POST",
success:function(data)
{
$('#display_comment'+myData1).html(data);
}
})
}
The DIV includes lots of whitespace around the ID. You need to remove that with trim().
var myData1 = div.textContent.trim();
What #barmar wrote is correct. Although I would suggest a much better approach for this. Whenever you wanna "fetch" some html data to javascript (here the data is set by PHP), you can use the data attribute in html like this:
<div id="dom-target" style="display: none;" data-id="<?php echo htmlspecialchars($test['id']);?>">
<?php
echo htmlspecialchars($test['id']);
?>
</div>
See that I've set the data attribute to the div above with the id.
Now when you wanna get this id in jQuery, you can do it with $("#dom-target").data("id"); like
var myData1 = $("#dom-target").data("id");
This gives you a much better and cleaner way to fetch data from html to javascript.
This question already has an answer here:
Convert XML to HTML using jquery/javascript
(1 answer)
Closed 2 years ago.
I am a total 0 in JS but I got a task to write a simple function of converting XML to HTML
Example:
<Translation code="200" lang="de-en">
<text>Cat</text>
</Translation>
'Cat' should be in HTML
XML structure doesn't change but the content of text node does
Yeah I can google couple of hours how to get it or ask here and close the task, appreciate any help
The quickest is using jQuery
const xml = `<Translation code="200" lang="de-en">
<text>Cat</text>
</Translation>`
$("#content").text($(xml).find("text").text())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="content"></div>
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I am working on a system that displays a product image connected to an entry in a form, to make it update automatically I'm using Javascript but it prints the literal tags instead of the variable set in javascript.
I've tried using this method to turn my Javascript variable into a php variable, but I haven't found a way to do this properly. I also let it output towards regular HTML text and that worked properly, so I'm just stuck on how to read it in the file name.
HTML/PHP:
<div style="position: relative; left: 0; top: 0;">
<img src="elements/<?php echo 'fronten/' . $serie . '-' . $front . '-Front-' . $formaat; ?>.png" id="ladefront"/>
<img src="elements/<?php echo 'kasten/' . $serie . '-' . '<script>document.writeln("kleur");</script>' . '-' . $formaat ?>.png" id="onderkast"/>
<img src="elements/<?php echo 'bladen/' . $product . '-' . $formaat ?>.png" id="wastafelblad"/>
</div>
JS:
var kleur = document.getElementById("color").value;
document.getElementById("demo").innerHTML = kleur;
}
The error it outputs is the following: GET .../kasten/NEXXT-%3Cscript%3Edocument.writeln( 403 (Forbidden) - so it puts the entire content in the file name instead of what the script is supposed to echo. My expected output is supposed to be .../kasten/NEXXT-(kleur)-60.png.
PHP is utterly irrelevant here.
You can't put an element (and that includes a <script> element) inside an attribute value.
You'd need to either generate the entire <img> element with JavaScript, or dynamically change the src attribute with JavaScript after completing the element.
This question already has answers here:
Can I add a custom attribute to an HTML tag?
(18 answers)
Closed 3 years ago.
Is there any way to save a javascript tag in an HTML object? What I would like to do is something like this:
<div id="htmlTag">some text</div>
<script type="text/javascript">
htmlTag.something = {s:"a string",n:1,b:false,a:[3,2,1],f:function(){alert("hello")}}
</script>
I think your only problem is you're not getting the node.
document.getElementById("htmlObject").something = {hi: 1};
You can convert JS objects to JSON string with this plugin, if that is what you want to achieve.
var json_data = JSON.stringify(yourObj);
You can do this using the javascript function JSON.stringify()
Javascript
<div id="htmlTag">some text</div>
<script type="text/javascript">
document.getElementById('htmlTag').something = JSON.stringify({s:"a string",n:1,b:false,a:[3,2,1],f:function(){alert("hello")}});
</script>
Here is a JSFiddle : https://jsfiddle.net/LcrLeg8a/1/