How to use quotes in nested PHP->JS->HTML - javascript

I'm trying to figure out how i can use quotes in a third "layer".
Do i need to "escape" the quotes somehow?
PHP/JS/HTML
echo "<script type='text/javascript'>";
echo "$('#rImgObjNr').text('Bilder: ".$imgDirs[$randomDir]."<i class='fa'></i>');";
echo "</script>";
It's my icon element that has problems with it's quotes:
<i class='fa'></i>
When i use 'I "close" JS text.. and if i use " i close the PHP.
How can i solve this?
Can i use my HTML element inside of this PHP/JS text?

You need to escape your double quotes by prepending backslash.
Replace your code like this below.
echo "<script type='text/javascript'>";
echo "$('#rImgObjNr').text('Bilder: ".$imgDirs[$randomDir]."<i class=\"fa\"></i>');";
echo "</script>";

May be it work :
<script type='text/javascript'>
$('#rImgObjNr').text("Bilder: <?php echo $imgDirs[$randomDir];?><i class='fa'></i>");
</script>

The solution was to change .textto .html and then escape the quotes like \".
CODE
echo "<script type='text/javascript'>";
echo "$('#rImgObjNr').html('Bilder: ".$imgDirs[$randomDir]."<i class=\"fa\"></i>');";
echo "</script>";

Related

How to echo php string with special characters inside javascript function parameter

I have a button on my website that triggers an onclick event with the following function
onclick="
updatePage('<?php echo $page['title']; ?>','<?php echo $page['id']; ?>','<?php echo $page['attachedfiles']; ?>','<?php echo $page['attachmentprefix']; ?>');
"
this usually works fine but if the $page['title'] contains a single quote it breaks the rest for example if the string was "What's your name?" it would break after the "what '"
Is there a fix for this, i have tried using
echo htmlspecialchars($page['title'])
but it doesnt work.
I am still a beginner so i am not too sure on how to solve this.
I am sorry if this is a duplicate question but from other solutions ive seen this seems like more of a javascript issue rather than php
You can try the addslashes function. Example:
<div class="thediv"></div>
<?php
$title = "This isn't the title";
?>
<script>
var div = document.querySelector('.thediv');
div.innerHTML = '<?= addslashes($title) ?>';
</script>

including html tag in a php code, specifically dealing with href function inside a php block

enter the html code once a condition is satisfied but then i aim to use php values and html tags together.
tried tag etc and got most of the part in running just unable to deal with href beacuse it is referencing to some values.
not sure where to use "" or ``.
<?php.....
echo `Yes`;
?>
"yes" should work as a hyperlink but at the moment the php values are not processed that`s why no result.
You are using wrong sequence of quote
<?php
...
?>
<?php
echo '<a href="limitdatabase.php?Dropdown=' .
$_GET['Dropdown'].
';&search='.
$search_name .
';&wise='.
$_GET['wise'].
';">Yes</a>';
?>
and you should use single quote and not backtics for string
You are using incorrect concatenation. Whenever you want to add any PHP variable then you need to close the braces and need to start after PHP variable as below:
<?php
echo 'Yes';
?>
Also you should wrap Dropdown and wise in braces as it will not work directly in get as you have used. Hope it helps you!
You can use combination of html with php inside:
<a href="limitdatabase.php?Dropdown=<?PHP echo $_GET[Dropdown] . "&search="
. $search_name . "&wise=" . $_GET[wise]; ?>">Yes</a>
Also, you can input whole link in string:
<?php
$mylink = "limitdatabase.php?Dropdown=" . $_GET[Dropdown] . "&search=" . $search_name . "&wise=" . $_GET[wise];
?>
YES

window.open is not working inside echo

I'm trying to add onclick="window.open()" to <? Echo "<a></a>" ?>
My code:
<?php
echo "<a href='$path'
onclick='window.open('" .$path. "',
'newwindow',
'width=300,height=250');
return false;''
>Pop</a>";
I have a problem with quotes outside Echo my code working pretty good but inside it doesn't work because of quotes changes.
Is there any solution for that?
<a href='<?= $path ?>' onclick='window.open("<?= $path ?>", "newwindow", "width=300,height=250"); return false;'>Pop</a>
echo also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. Prior to PHP 5.4.0, this short syntax only works with the short_open_tag configuration setting enabled.
The major differences to print are that echo accepts an argument list and doesn't have a return value.
Reading Material
echo
escape out quotes with \" so it ends like this:
<?php
echo "<a href=\"".$path."\"
onclick=\"window.open('" .$path. "', 'newwindow', 'width=300,height=250');return false;
\">
Pop
</a>";
You already write two single quote after each other in :
... onclick='window.opened(' ....
and it will cause to string that echoed will be broken from the second qoute place,you can use \" when u want to wite quotes.do this instead :
<?php
echo "<a href='$path'<
onclick=\"window.open(' " .$path. " '.....
....`'width=300,height=250')\";

How to add a new line within JS confirm message to be shown on click of a link?

I'm trying to add \n but this breaks the onclick event – no message is popped up at all.
It works OK when I remove the \n character.
<?php
echo "<a href='logs.php?clear=true' onclick='return confirm(\"Are you sure:\n To delete this\");'>Clear</a>";
You need to use double backslashes, the first will be interpreted by PHP. Try this instead:
<?php
echo "<a href='logs.php?clear=true' onclick='return confirm(\"Are you sure:\\n To delete this\");'>Clear</a>";
Though you may want to use a heredoc instead:
<?php
echo <<< HTML
Clear
HTML;
Or, even better, use proper event binding like with jQuery:
<?php
echo <<< HTML
Clear
<script>
$("#clearconfirm").click(function(e) {
if (!confirm("Are you sure:\\n To delete this")) {
e.preventDefault();
}
});
</script>
HTML;

Write onclick method that has parameters in php print function

I'm having problems with writing an onclick method in php print in code below:
<?php print "<p><small> <a href='#' onclick='ajaxMenu('comments.php?news=".$fileContent['id']."');>".$number. " ".$text." </small> </a> </p>"; ?>
Given code doesn't work. I believe it's because of combination of single and double quotes in it. Can anyone help me to solve this?
You can escape " characters by having a forward slash \"
<?php echo( "This is a link" ); ?>

Categories

Resources