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

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>

Related

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

Passing php variable to button onclick function

I am a newbie working on a screen which has a conditional button which should appear only if $step==2. In this case, the button is shown and on click, redirects me to another page within my project. However, the url to which it should redirect me is not fixed, but has a parameter within it. I am having trouble getting the onclick to work!
I have tried defining a $path variable in which I store the relocation string. I do this in the following way:
<?php $path = '"window.location=\'{{ url("/location/'.$location->id.'/description") }}\'"'?>
When I echo this $path, I get exactly the string I need, which is:
"window.location='{{ url("/location/2/description") }}'"
(Note: The $location->id in my test case is 2)
However, I then try using this $path for the onclick of the button and it's not working! Here is the whole thing:
<?php if ($step == 2) : ?>
<?php $path = '"window.location=\'{{ url("/location/'.$location->id.'/description") }}\'"'?>
<button class="button_s_red" onclick=<?php echo $path; ?>>{{ trans('setup_process.STEP2_Button') }}</button>
<?php endif; ?>
When I inspect this button in chrome, it seems that on the onclick, the / character is not getting through. But this is odd since when I echo the $path I do get this character, and / doesn't seem to be an escape character from what I've googled.
I've tried pretty much everything and googled every possible help but can't seem to make it work! Thanks in advance for your help!
id.'/description") }}\'"'?> >{{
trans('setup_process.STEP2_Button') }}
I think u must use " in Your code like this.
<?php if ($step == 2) : ?>
<?php $path = '"window.location=\'{{ url("/location/'.$location->id.'/description") }}\'"'?>
<button class="button_s_red" onclick=("<?php echo $path; ?>")>{{ trans('setup_process.STEP2_Button') }}</button>
<?php endif; ?>

How can I place a drop-down list made in php in a specific HTML form?

I have some php that connects to a database and creates a drop down list. I have a specific form in the HTML that I'd like to put the list in.
<body>
<form>
// some text inputs
// where i'd like the drop down to go
<?php makeList(parameter1, parameter2); ?>
// submit button
</form>
<?php
// connect to database
function makeList(arg1, arg2) {
echo '<select>';
while ($row = mysqli_fetch_array($result)){
echo "<option">;
echo $row[$column];
echo "</option>";
echo '</select>';
}
</body>
The only languages I'm allowed to use (apart from the sql) are php, html and javascript. As it is right now, makeList() returns an empty list. When I include opening and closing form tags in the function it returns a fully functional list, but then it acts as it's own form and I need to to be a part of the original form. Thanks.
EDIT: Sorry, forgot to mention the makeList function works fine when called within the php tags. It's when I call it in the HTML that it returns an empty list.
Firstly, you have some syntax issues with your script. It's not a valid HTML file, not a valid PHP file, and not a valid JS file.
If it were up to me, I'd define the PHP function at the stop of my script. Be careful to balance your opening and closing PHP tags. Something like this:
<?php
// connect to database
function makeList($arg1, $arg2) {
echo '<select>';
while ($row = mysqli_fetch_array($result)){
echo "<option">;
echo $row[$column];
echo "</option>";
echo '</select>';
}
?>
And only after that would I start to output my HTML.
Now there are a couple of important things to note about that script I just posted:
the database code is not in here...I don't see any connection or query getting run or anything
In your script, this function doesn't look valid. arg1 and arg2 need a $ in front of each to be a valid PHP function. If it's a JS function you want then well, you are very confused and probably need to go back and figure out why this is not a valid JS function.
Your function refers to a variable, $result, that you have not bothered to define. It is not mentioned anywhere else in your script. It is most certainly not mentioned anywhere inside your function. For $result to be defined inside your function, you either need to pass it in as an array or declare it as a global:
global $result
Your function doesn't return anything at all. It just echoes stuff. This doesn't mean you can't use it, but it does mean that the function has no return value. Echoing the result of makeList won't output anything at all
So after that script above, you might have something like this:
<body>
<form>
// some text inputs
<?php makeList($parameter1, $parameter2); ?>
// submit button
</form>
Depending on what your parameters ($parameter1 and $parameter2) are this should work.
<body>
<form>
// some text inputs
<?php echo makeList($parameter1, $parameter2); ?>
// submit button
</form>
<?php
// connect to database
function makeList($arg1, $arg2) {
echo '<select>';
while ($row = mysqli_fetch_array($result)){
echo "<option>";
echo $row[$column];
echo "</option>";
echo '</select>';
}
</body>

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

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

Categories

Resources