I'm trying to call a window.open() javascript code inside a php file, but unfortunately it is not responding.
Here's my code:
<html>
<head>
<script language="JavaScript"></script>
</head>
<body>
<?php .....
$printHeader .= " <div id ='subHeader'><p class = 'alignleft'><font style='color:blue'>User: " . $username . " | Access: " . $level ." | <a href=logout.php>Logout</a></font></p>";
$printHeader .= " <p class='alignright'><a href='refresh.php' onClick='window.open(this.href,'mywin','width=400,height=200')'>Refresh List</a></p></div>";
$printHeader .= "<div style='clear: both;'></div>";
.... ?>
</body> </html>
Unfortunately, when I run it, it doesn't work at all. Can we not call javascript in php? Please advise!
Thanks!
I think the issue is related to the quotes, Your inner single quotes are ending the onClick string, so replacing the outter single quotes to double quotes(will keeping the inner quotes single) will solve your issue. try changing this:
onClick='window.open(this.href,'mywin','width=400,height=200)'
To this:
onClick=\"window.open(this.href,'mywin','width=400,height=200)\"
Here's an example:
On JSFiddle
Another solution is to simply escape your inner quotes by prefixing them with a backslash (\).
One thing that catches my eye right away is that you did not escape your single quotes withing the onclick event. Modify it like so:
onclick=\"window.open(this.href, 'mywin', width=400, height=200);\"
See if this helps you out.
Related
Hello dear programmers,
I have a problem with the echoing of a html phrase with an onclick function that executes a javascript function. I want to build a tabpage, for a image gallery.
The echo:
echo "<div class='albumitem'><a class='tablinks' onclick='openAlbum(event, '".$album."')'><h1 class='galleryheader'>".$album."</h1></a><div id='".$album."' class='tabcontent'>";
Everything goes well, except the passing of the variable in the onclick function, as you can see here. What actually the HTML looks like:
<a class="tablinks" onclick="openAlbum(event, " aubing')'=""><h1 class="galleryheader">Aubing</h1></a>
But this onclick event has to look like this:
onclick="openAlbum(event, 'Aubing')"
Is there a way to actually realise this or do I have to find an other option?
I actually tried switching " with ', didnt go very well....
Thank you for everybody that tries to help
Try this:
echo "<div class='albumitem'><a class='tablinks' onclick='openAlbum(event, \"$album\")'><h1 class='galleryheader'>".$album."</h1></a><div id='".$album."' class='tabcontent'>";
see escaped double quotes in the onclick definition
addslashes is what you are looking exactly.And also you have to remove the single quotes in variable.Try to do the following way.
echo "<div class='albumitem'><a class='tablinks' onclick='openAlbum(event, '".addslashes($album)."')'><h1 class='galleryheader'>".$album."</h1></a><div id=".addslashes($album)." class='tabcontent'>";
Hope this help.
An alternative:
$escapedString = htmlspecialchars('This is a test string: < > & \' " end.', ENT_COMPAT);
echo "<div onclick='alert(this.dataset.name)' data-name=\"$escapedString\">Click Me</div>";
This approach avoid quotes inside function, no quotes nesting.
I have following code in php where I am binding html code:
$my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open(".$base_image_url.$myDoc->my_doc_name.")' value='".$myDoc->my_doc_id."' class='cropcss'/>";
It produces following output:
<img src="mysite.in/8422_1477013411.png" onclick="window.open(mysite.in/8422_1477013411.png)" value="623" class="cropcss">
In window.open(mysite.in/8422_1477013411.png) I have missed single quote inside. How do I add this?
I prefer to use single quotes for outputting variables like HTML to the browser. Simply use \' to escape a single quote, and pass it to the browser. Something like this should work:
$my_doc .= '<img src="mysite.in/' . $myDoc->my_doc_name . '" onclick="window.open(\'' . $base_image_url . $myDoc->my_doc_name . '\')" value="' . $myDoc->my_doc_id . '" class="cropcss" />";
If you insist on using double quotes, a minor quick change can fix this (Just add \" around your text):
$my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open(\"".$base_image_url.$myDoc->my_doc_name."\")' value='".$myDoc->my_doc_id."' class='cropcss'/>";
For more info, see this post on how to escape quotation marks in php
As an alternative - you can use HEREDOC syntax - keeps it nice and neat in the output code, no need to worry about escaping quote marks or breaking/concatenating the string.
$my_doc .= <<<MYDOC
<img src="mysite.in/{$myDoc->my_doc_name}" onclick="window.open('{$base_image_url}{$myDoc->my_doc_name}')" value="{$myDoc->my_doc_id}" class="cropcss" />
MYDOC;
check code replace this
$my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open('".$base_image_url.$myDoc->my_doc_name."')' value='".$myDoc->my_doc_id."' class='cropcss'/>";
I have a HTML achor tag like below:
echo '<a href="javascript:tempBuy('.$res_get_price[0][0].','.$res_get_price[0][1].','.$res_get_price[0][2].','.$dt_str.')">'.$res_get_price[0][0];
And the corresponding javascript function tempBuy() is
function tempBuy(rate,veg_name,market_name,dt)
{
alert(dt);
}
But the problem is it does not alert at all ! May be I need to include the variable names within single quotes in tempBuy() function. I tried tempBuy(\'var1'\,\'var2\'...) but it shows error. How can I able to to that. Thanks .
Source for the part shows like this:
<td width="120px" class="">56.0
</td>
<script>
function tempBuy(rate,veg_name,market_name,dt)
{
alert(rate);
}
</script>
You didn't wrap your javascript arguments in quotes. You need to wrap each variable in single quotes, since you used double quotes for "href" attribute. Another thing is that you didn't close up "a" HTML tag.
echo ''.$res_get_price[0][0].'';
If there is anything in your variables that is not a valid javascript literal you have to make it a string like:
echo '<a href="javascript:tempBuy(\''.$res_get_price[0][0].'\' ...
If there are ' in your variables you have to replace them with \' as well.
As you can see form the rendered output, you need to quote the last 3 arguments which are non-numeric. The correct output should be: javascript:tempBuy(56.0,'Apple','Bangalore','2013-05-18')
The corrected PHP code is:
echo ''.$res_get_price[0][0].'';`
echo "<a href=\"javascript:tempBuy('".$res_get_price[0][0]."','".$res_get_price[0][1]."','".$res_get_price[0][2]."','".$dt_str."')\">".$res_get_price[0][0];
I am using the shortcode execute plugin in wordpress.
This simply allows me to write shortcode like this [email_spamproof]
But I am trying to echo a script. Please see below...
<?php
echo '<script type="text/javascript">
<!--
// spam protected email
emailE=("enquiries#" + "example.co.uk")
document.write('<a title="E-mail Example" href="mailto:' + emailE + '">' + emailE + '</a>')
//-->
</script>
<noscript>
<span class="spam-protected">Email address protected by JavaScript. Please enable JavaScript.</span>
</noscript>';
?>
Now you can probably see my problem already.
Usually when I echo stuff it goes like this... echo 'hello';
And you break the string using the same apostrophes
- like this... echo 'hello ' . php_name() . ' and friends'
My Problem
The script also uses a similar method by adding script variables into the string, but these script apostrophes will get confused as PHP apostrophes, and break the echoed string.
How can I avoid this?
Thanks
The right code:
<?php
echo "<script type='text/javascript'>
<!--
// spam protected email
emailE=('enquiries#' + 'example.co.uk')
document.write('<a title='E-mail Example' href='mailto:' + emailE + ''>' + emailE + '</a>')
//-->
</script>
<noscript>
<span class='spam-protected'>Email address protected by JavaScript. Please enable JavaScript.</span>
</noscript>";
?>
The \ character is the escape character. Prepend one to each internal use of the character used to delimit the string.
Alternatively, use a HEREDOC.
It is easy. Save your javascript code in a new one file and then use this to load it:
include('myjavascript.php');
Then you will use the include option as a echo because the web will understand your code as HTML and you execute it when you want with php (like echo).
You could write " in your javasript code, and then only use single quotes(') in your php echo.
A more suitable method is to use escape characters in your javascript code "\'" (without the double quotes)
You can either escape them with a back slash: echo 'It\'s cold'; or use heredoc:
echo <<<END
lots of text here
END;
You need to escape them and then concatenate the variables :
(Also I dont know if you realize, but you are echoing a commented out section of javascript)
echo '<script type="text/javascript">
<!-- // spam protected email
emailE=("enquiries#" + "example.co.uk")
document.write(\'<a title="E-mail Example" href="mailto:'.emailE.'">'.emailE.'</a>\')
//-->
</script>
<noscript>
<span class="spam-protected">Email address protected by JavaScript. Please enable JavaScript.</span>
</noscript>';
I am trying to echo this jquery function, with php. basically if the script detects a field of a form is not filled in then it will echo this and make the input text box turn red.
It works fine when it is not being echo'd.
echo('
<script type="text/javascript">
$(document).ready(function() {
$(\'input\').animate({backgroundColor:\"#F00\"},200);
});
</script>
');
any ideas?
I don't think you have to escape your quotes when the string is within single quotes. PHP won't parse the string, it will be output literally.
You're over-doing it on the string escape. To keep it simple, just use single quotes around the echoed string, and use double quotes inside it. Something like:
echo('
<script type="text/javascript">
$(document).ready(function() {
$("input").animate({backgroundColor: "#F00"}, 200);
});
</script>
');
When you're echoing stuff, there are indeed some cases when you need to escape the quotes, but most of the times you can simply get away with it by using different types of quotes. For example, I'll never get it why people still do something like:
echo "<input type=\"text\" name=\"username\">";
as opposed to
echo '<input type="text" name="username">';
which makes your life a whole lot easier when you have to modify it.
Hope this helps !
You shouldn't use \" there, just "
Furthermore: a hex-color-value is no numeric value you can use for animate() .
By this, the error is fixed by removing the backslashes from the doublequotes, but your animation wouldn't show any effect.
i didnt test it, but try that:
$nl = "\n";
echo '<script type="text/javascript">'.$nl;
echo ' $(document).ready(function() {'.$nl;
echo ' $("input").animate({backgroundColor:"#F00"},200);'.$nl;
echo ' });'.$nl;
echo '</script>'.$nl;
the $nl="\n" is only for linebreak (I prefer to use singlequotes in echos, so php didn't have to parse the content - just echo out).