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'/>";
Related
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 have this php line which works fine:
echo "<p>" . $post['message']. "</p>";
But I want to change it so it will link to my page (not to a single post). So it should look like that.
echo "<p>" . $post['message']. "</p>";
I have tried a lot many proposition gathered on different website, but each time I am getting an error.
Any idea ?
Thanks a lot!
Using single and double quotes, you avoid escaping issues. Try this:
echo '<p>'. $post['message']. '</p>';
i see that you didn't escaped from double quote that closes href attribute:
echo "<p><a href=\"https://www.facebook.com/rscmovement\" target=\"_blank\">"
I guess You have missed the back slash () before " after www.facebook.com/rscmovement.
"https://www.facebook.com/rscmovement\" "\"target=\"_blank\">" will
Oh boy! I cant get this to work. Any ideas on what the heck I'm doing wrong? Here's the code.
I'm trying to echo the script but use a php function to get the directory of the js file!!
Any help would be appreicated!!
echo '<script src="<?php get_some_function();?> . /js/main.js"></script>';
I've tried dif scenerios with escaping but cant get this to output correctly.
Since you're already in the PHP context, you can simply concatenate the strings, like so:
echo '<script src="' . get_some_function() . '/js/main.js"></script>';
Using sprintf() looks more cleaner, though:
echo sprintf('<script src="%s/js/main.js"></script>', get_some_function());
Instead of opening another script tag inside the string, concat the string and echo. The <?php within your string will not be evaluated.
echo '<script src="'. get_some_function() . '/js/main.js"></script>';
Simple string concatenation:
echo '<script src="' . get_some_function() . '/js/main.js"></script>';
Don't forget to properly escape the output of your function!
try doing this:
echo '<script src="'.get_some_function().' /js/main.js"></script>';
or this:
$value = get_some_function();
echo '<script src="'.$value.' /js/main.js"></script>';
Remember that any variable echoed in single quotes ( ' ' ), the value of that variable will be not printed, and if a variable is echoed in double quotes ( " " ) it will be printed.
Similar is true for returned data from a function stored in a varaible. If you are using single quotes, then every php code (variable, or a method call of a class) should be concatenated using dot operator ( . , :P ) . If you are using double quotes, then no need to use . .
Like in all above answers, they have used . to append the php function call, your code may be fine as below also (not tested by me, so you will need to do adjustment) :
$src = get_some_function();
echo "<script src=$src/js/main.js></script>";
But please note that it is a best practice to use single quotes for any kind of html etc echoed in php code, because HTML attributes are using double quotes.
Hope this will help...
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];