Write onclick method that has parameters in php print function - javascript

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

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

How to add ' to generated link in php

$output .= '<a class="dropdown-toggle has-category" data-toggle="dropdown" href="'.$this->getLink($menu).'" target="'.$menu['target'].'">';
I have php file with this code href="'.$this->getLink($menu).'"
its generates in html this href="https://www.myweb.com/"
Now i need to do it with onClick so that the output turned out like this
onclick="location.href='https://www.myweb.com'"
Changed php code onclick="location.href='.$this->getLink($menu).'"
and result is onclick="location.href=https://www.myweb.com"
Link not working cuz lacks '
Can someone help me?
Try this
onclick="location.href=\''.$this->getLink($menu).'\'">
Example:-
echo '<a onclick="location.href=\''.$this->getLink($menu).'\'">';
Can you add this escaped single quote: \'?
echo '...onclick="location.href='.$this->getLink($menu).'\'"...';
(I added the echo to complete the code example).

Writing HTML code in Javascript inside a PHP code

I need to write in PHP, inside a Javascript portion code, an hidden input tag with an javascript array that I need to pass to another PHP code ..
This is the sample code ...
echo '<script type="text/javascript">';
<other javascript code .... >
echo 'arr_selections_json = JSON.stringify(arr_selections);';
echo 'document.write("<input type="hidden" name="arr_selections_json" value="+arr_selections_json+" />")';
This code doesn't work .... Any suggestions?
Thank you in advance .. .
You need to JS-escape the double quotes inside the argument of document.write()
e.g.
echo 'document.write("<input type=\\"hidden\\" name=\\"arr_selections_json" value=\\"" + arr_selections_json + "\\" />")';
Also, I've had recent weird cases, where document.write wouldn't consider the closing slash (/>), whereas it's HTML-5 compliant. I had to escape with a backslash the closing slash.
What about this ?
<script type="text/javascript">;
<other javascript code .... >
arr_selections_json = <?php echo JSON.stringify(arr_selections); ?>;
document.write('<input type="hidden" name="+arr_selections_json+" value="+arr_selections_json+" />');
</script>
I'm not sure what are your php part, but i think you should be ok like this.

TinyMCE exec Command inside php loop

Trying to get an href with TinyMCE command function working but no luck. I used double and single quotes as well. Here is the original code
echo "<a href="javascript:;" onclick="tinyMCE.activeEditor.setContent('');tinymce.execCommand('mceInsertContent',false, document.getElementById('div_".$i."').innerHTML);
return false;">[Manage Marketing]</a>";
echo "<a href=\"javascript:;\" onclick=\"tinyMCE.activeEditor.setContent('');tinymce.execCommand('mceInsertContent',false, document.getElementById('div_".$i."').innerHTML);
return false;\">[Manage Marketing]</a>";

Categories

Resources