I currently have a button when clicked it should open up a new window but I am getting this error: Uncaught SyntaxError: Unexpected token :. I am using window.open()
$url = "http://www.google.com";
$button .=
'<input type="button" value="Print Timetable" class="printButton" onclick="window.open('.$url.');"/>';
I have noticed that when I use window.open() with no parameters it works but when I pass in the $url variable all hell breaks loose. I get syntax errors. I've tried http://www.google.com, www.google.com and google.com to no avail!
Thanks for your help in advance!
Because you are missing the single quotes needed to encapsulate the url string.
$url = "http://www.google.com";
$button .=
'<input type="button" value="Print Timetable" class="printButton" onclick="window.open(\''.$url.'\');"/>';
You're adding a layer of string encapsulation. When you pass a string value to a function it must be in quotes, as it is a string.
doSomething('http://www.ibm.com/');
When you are doing this inline in your html, you need to encapsulate the javascript in double quotes so it becomes
onclick="doSomething('http://www.ibm.com/');";
Then, if you want PHP to echo that, or to assign it as variable, you need to enclose all of that in quotes, so you can do
<?php
//encapsulate in double quotes and escape double quotes
echo " onclick=\"doSomething('http://www.ibm.com/');\" ";
//encapsulate in single quotes and escape single quotes
echo ' onclick="doSomething(\'http://www.ibm.com/\'); ';
?>
Any way you break it down you need have 3 string encapsulations embedded in one another, so you must find a way to differentiate between the quotes used in JS and the quotes used in PHP.
Otherwise you would have a problem.
<?php
//Houston We have a problem!
echo " onclick="doSomething('http://www.ibm.com/');\" ";
^ ^ ^ ^
Open Quote Close Quote Open Quote Close Quote
?>
There are a couple of things wrong here:
You have a period character before the equals, eg
$button .=
Should be
$button =
And you need to escape your single quotes:
$url = "http://www.google.com";
$button = '<input type="button" value="Print Timetable" class="printButton" onclick="window.open(''.$url.'');"/>';
I think it suppose to be like this:
$url = "http://www.google.com";
$button .=
'<input type="button" value="Print Timetable" class="printButton" onclick="window.open("'.$url.'");"/>';
window.open() require quote or double quote
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 want to add custom link that is stored in a value inside a php button. How do I do it?
I am trying to get this sorted how ever its not doing it, it just returns nothing or sometimes returns the current page link instead. I am looking towards adding my $lnk to this code in href section.
This is what I have:
echo '<input class="btnnn-class" type=btnnn-class onClick=location.href="" .$lnk value="Contact Buyer">';
Where $lnk is my link inside of it.
Al Fonce told correct answer and described wel
He forget one double quotes after $lnk . '\'
echo '<input class="btnnn-class" type="btnnn-class" onClick="window.location.href=\'' . $lnk . '\'" value="Contact Buyer">' ;
There is several typos in your code : you did not open and/or close quotes when needed. You have too surround your classes with double quotes and your onClick value too. Inside it, the location href is a string too that should be inside quotes.
Both php and javascript allow single and double quotes to delimitate a string, but you should use only double quotes for html attributes.
So:
Use single quotes ' for the whole echo statement (but varaiables are not interpreted inside single quotes),
Use double quotes " for the html attributes.
Use single quotes ' for the javascript value, and escape it (becasue they are inside another php single quoted string).
The code:
echo '<input class="btnnn-class" type="btnnn-class" onClick="window.location.href=\'' . $url . '\'" value="Contact Buyer">' ;
Or use double quotes for php and espace the html attributes ones, the variable will be interpreted:
echo "<input class=\"btnnn-class\" type=\"btnnn-class\" onClick=\"window.location.href=$url\" value=\"Contact Buyer\">" ;
Try this:
echo '<input class="btnnn-class" type="btnnn-class" onClick="window.location.href=\'$lnk\'" value="Contact Buyer">' ;
Because of the quotes, you are facing the problem.
I'm trying to define $display as a html line. This is what I have so far.
$display = '<input type="image" name="5more" value="5more" src="./pictures/fm/5more2.png" onmouseover="this.src='./pictures/fm/5more1.png'" onmouseout="this.src='./pictures/fm/5more2.png'">';
I keep getting this error: Parse error: syntax error, unexpected '/' in results.php on line 15.
Please help me.
$display = <<<EOF
<input type="image" name="5more" value="5more" src="./pictures/fm/5more2.png" onmouseover="this.src='./pictures/fm/5more1.png'" onmouseout="this.src='./pictures/fm/5more2.png'">
EOF;
Use the Heredoc syntax and don't even worry about escaping (double) quotes.
You need to escape the ' in the string.
$display = '<input type="image" name="5more" value="5more"
src="./pictures/fm/5more2.png"
onmouseover="this.src=\'./pictures/fm/5more1.png\'"
onmouseout="this.src=\'./pictures/fm/5more2.png\'">';
Or use only " inside the string when you quote it with '.
You need to escape the single quotes within. Try using this:
$display = '<input type="image" name="5more" value="5more" src="./pictures/fm/5more2.png" onmouseover="this.src=\'./pictures/fm/5more1.png\'" onmouseout="this.src=\'./pictures/fm/5more2.png\'">';
A string declaration is encased with '.
$display = '...content...';
This means that the beginning of the string starts with a ' and ends with a '.
Your string includes a selection of double quotes " which is fine, however, your variable also includes...
onmouseover="this.src='./pictures/fm/5more1.png'"
onmouseout="this.src='./pictures/fm/5more2.png'"
The single quotes ' before and after the image declarations are what is causing the problem. The PHP compiler sees the first single quote ' (just before the first image declaration) as the end of the string.
The solution to this problem is to escape the single quotes within the string. Just add a \ just before each single quote. the single quotes will look like this \'.
Here is a correct declaration
$display = '<input type="image" name="5more" value="5more" src="./pictures/fm/5more2.png" onmouseover="this.src=\'./pictures/fm/5more1.png\'" onmouseout="this.src=\'./pictures/fm/5more2.png\'">';
You need to escape the quotes as mentionned above. But in terms of readability this design approach will save a few pains even if it's more wordy. Check if supported by your hosting configuration.
$urlOver = "./pictures/fm/5more1.png";
$urlOut = "./pictures/fm/5more2.png";
$onMouseover = "this.src='$urlOver'";
$onMouseout = "this.src='$urlOut'";
$display =
"<input type='image' name='5more' value='5more'
src='$urlOut'
onmouseover='$onMouseout'
onmouseout='$onMouseout'>";
P.S: Curly brackets can be added as follows when between quotes ("..."): {$onMouseout}
$display = '<input type="image" name="5more" value="5more"
src="./pictures/fm/5more2.png" onmouseover="this.src=\'./pictures/fm/5more1.png\'"
onmouseout="this.src=\'./pictures/fm/5more2.png\'">';
You can't frame an echo with single quotes and also use single quotes inside it. That breaks the frame and the output. However, if you escape the interior single quotes with \, you can get away with it. You could have also gotten away with using addslashes when echoing the $display variable, like this, IF and ONLY IF you change the interior single quotes to double quotes.
echo addslashes($display);
And your interior quotes would have automagically been escaped for you.
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];