Here is the code I am trying to make work. As you can see, the code as it stands will call a javascript window and that works well. I now want to add the lines that are commented out, in order to make the window display or not display conditionally. I have tried many combinations of escaping quotation marks, or substituting them, but nothing works properly. Usually I lose the link action along with a display of some of the code attached to the link. Could someone have a look and see if they can propose a code solution.
<?php
//if ($review != 0)
//$yes2 =
if ("{$row['passState']}" == 0) {echo "<a href='javascript<b></b>:void(0);'NAME='var basestring='window'+new Date().getTime();' title=' Results of quiz 'onClick=window.open('check/check.php?quizTitle=". urlencode($quizTitle) ."', 'width=1100, height=510, resizable=yes, menubar=no, status=0, scrollbars=1');> <p>Check your answers</p> </a><br />\n";}
//echo $yes2;
//if ($review != 1)
//echo "";
?>
If it is any help, I'm using this code elsewhere on the page and it works fine.
<?php
if ($review != 0)
$yes2 = "REVIEW";
echo $yes2;
if ($review != 1)
echo "";
?>
if ("{$row['passState']}" == 0) {echo "<a href='javascript<b></b>:void(0);' NAME=\"var basestring='window'+new Date().getTime();\" title=' Results of quiz ' onClick=window.open('check/check.php?quizTitle=\". urlencode($quizTitle) .\"', 'width=1100, height=510, resizable=yes, menubar=no, status=0, scrollbars=1');> <p>Check your answers</p> </a><br />\n";}
Copy this as it is....
There's no need for all the escapes, you should embed the PHP code in the HTML, not the other way around. Example:
<?php if ($row['passState'] == 0): ?>
<p>
<a href="javascript:your_code()">
Check your answers
</a>
</p>
<br />
<?php endif; ?>
I had a hard time deciphering what your javascript was trying to actually do, but this will make it a lot easier for you to read and write HTML without worrying about escaping quotes, using \n characters, etc.
http://php.net/manual/en/control-structures.alternative-syntax.php
You can use addslashes method to escape quotes
PHP Manual
Your onClick needs to have a quote(s) after the =:
...f quiz' onClick=\'window.open('check/check.php?quizTitle=". urlencode($quizTitle) ."',
Related
I was wondering how would i go about styling a echo in php? im trying to style the paragraph tag nested inside one of the echos, i'd be grateful if anyone can tell me how to do this. thanks
PHP
if(isset($_SESSION['sess_user_id']) && $_SESSION['sess_user_name'] != "") {
echo '<h1>Welcome '.$_SESSION['sess_user_name'].'</h1>';
echo "<p id="profileText"This is your personal profile page, from here you can edit events</p>";
echo '<h4>Logout</h4>';
}
else {
header('location:index.php');
CSS
#profileText {
top: 40%;
position: absolute;
color: blue;
}
Better yet, don't do presentation from within your php logic. Consider this:
<?php
if(!isset($_SESSION['sess_user_id']) || $_SESSION['sess_user_name'] == "") {
header('location:index.php');
}
// do any other php stuff...
?>
<h1>Welcome <?= $_SESSION['sess_user_name'] ?></h1>
<p id="profileText">This is your personal profile page, from here you can edit events</p>
<h4>Logout</h4>
This way,
Your logic and presentation are clearly separated
If you want to change the presentation, you don't have to mess around with your php code
You don't have to worry about single quote / double quote / escape quote / blah, blah, blah.
You don't have to worry about output before the header
The programmer that comes after you will be amazed at how clean your code is :)
It lets you easily see typos: <p id="profileText"This is your personal profile page, from here you can edit events</p>
You have syntax error.
echo "<p id="profileText"This is your personal profile page, from here you can edit events</p>";
Use single quote in your echo.
echo "<p id='profileText'>This is your personal profile page, from here you can edit events</p>";
If you are use double quote in your echo, you can't use another double quote in your string. You can do on this way:
echo "<p id=\"profileText\"> This is your personal profile page, from here you can edit events</p>";
Or you can use single quote on your string
echo "<p id='profileText'>This is your personal profile page, from here you can edit events</p>";
This code is supposed to help me in limiting the amount of characters seen in the timeline page. If the post is too long, (more than 400 characters), it should display a 'see more...' which should take the reader to a new page (posts.php?post=XXX). When I use this function, the second part after 'else' works fine. However, the first part seems to duplicate all the tags, and the spaces in this page appear twice bigger than the ones in posts.php. I can't trace the problem, someone please help me.
function display_content($long_text, $link, $page='posts', $page_ext = 'post'){
if(strlen($long_text) >= 400){
for($i=0; $i<=400; $i++){
echo nl2br($long_text[$i]);
}
echo "...<br /><a href='{$page}.php?{$page_ext}={$link}'>Read more...</a>";
} else echo nl2br($long_text);
}// I have taken care of all security issues.
Instead of parsing all characters of your text :
for($i=0; $i<=400; $i++){
echo nl2br($long_text[$i]);
}
Why don't you use :
echo nl2br(substr($long_text, 0, 400));
?
I've been trying to make a button that changes into a different image once rolled over with the mouse, but the code being inside of an echo command has caused some complications. This is the php code I tried to use, which otherwise simply displays the image when the onmouse functions are removed:
if ($pg > 0) { echo ('<div class="next"><img src="/images/prevpage.gif" onmouseover="this.src='/images/prevpagehl.gif';" onmouseout="this.src='/images/prevpage.gif';"/></div>'); }
Using this code causes the following error to appear:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/wintblxq/public_html/index.php on line 145
Are there any changes to the syntax I can make to get this to function properly, or do I need to attempt a new approach to this?
You need to escape your quotes (your ticks actually):
if ($pg > 0) {
echo '<div class="next"><img src="/images/prevpage.gif" onmouseover="this.src=\'/images/prevpagehl.gif\';" onmouseout="this.src=\'/images/prevpage.gif\';"/></div>';
}
You need to escape the single quotes in the onmouseover and onmouseout javascript:
if ($pg > 0)
{
echo ('<div class="next"><img src="/images/prevpage.gif" onmouseover="this.src=\'/images/prevpagehl.gif\';" onmouseout="this.src=\'/images/prevpage.gif\';"/></div>');
}
PHP is confusing these with the end of the string.
You first need to escape your single quotes with a Backslash example:
<?php echo '<p onmouseover="alert(\\'SIngle Quotes\\');">YAY</p>';
An easier way to write php and to see it (In my Opinion) is to Seperate HTML from php. With your code here is how i would write it.
<?php if ($pg > 0) { // PHP Scripts ?>
<!-- HTML Tags and text -->
<div class="next"><img src="/images/prevpage.gif" onmouseover="this.src='/images/prevpagehl.gif';" onmouseout="this.src='/images/prevpage.gif';"/></div>
<?php } ?>
This way you don't have to worry about escaping all of your single quotes, and you can see it nicer.
I have been using this code for deleting data from database. What i wan is whenever a user clicks an image link to delete data from the confirm function prompts up and ask for action, i am getting error in this code.
$delurl = "delete_dish.php?dish_id=".$row['id'];
$img = "<img src = 'images/delete.png'>";
echo "<a href=".$delurl.";
echo "onclick='return confirm('Are you sure you want to delete.')'>".$img."</a>";
Maybe the error is in double quotes or single quotes, Any help
Thanks in advance
change
echo "<a href=".$delurl.";
to
echo "<a href=\"".$delurl."\" ";
$delurl = "delete_dish.php?dish_id=".$row['id'];
$img = "<img src = 'images/delete.png'>";
$confirm_box <<<CONFIRM
<a href="$delurl"
onclick="return confirm('Are you sure you want to delete?')">$img</a>
CONFIRM;
// then elsewhere ...
echo $confirm_box
Always tend towards using the HEREDOC syntax to construct HTML/JS output, it will save you a lot of heartache. Just watch out for the major gotcha, DO NOT INDENT THE FIRST/LAST lines of the heredoc declaration.
EDIT The benefit being that you can mix single and double quotes as much as you like, you only have to worry about the JS quoting - PHP variables are interpolated without the quotes. You can further wrap curly quotes around your PHP variables like {$this} to make it easier to read, but also to delineate $this and {$this}tle.
I would us the following instead of escaping, this is more readable to me:
$delurl = "delete_dish.php?dish_id=".$row['id'];
$img = "<img src = 'images/delete.png'>";
?>
<?=$img?>
You can, may and should escape when handling stuff like this:
echo "<a href=\".$delurl.\"";
echo " onclick=\"return confirm('Are you sure you want to delete.')\">".$img."</a>";
lg,
flo
can anybody help me here please?
I am using AJAX for pagination in my application. So I am generating hyperlinks with for loop. as follow:
for($t=1; $t<=$hf; $t++)
{
if($t == $_GET['pageno'])
{
echo $t." ";
}
else
{
echo "<a id ='$t' href='javascript:void(0)' onclick='open_page('ajaxinfo.php','content'); javascript:change('$t');'>$t</a>"." ";
}
}
Above echo statement does not give call to function. But instead of this when i just write html hyperlink it works fine and I get to see page2.html, my HTML code is:
<a id="page2" href="javascript:void(0)" onclick="open_page('ajaxinfo.php','content'); javascript:change('page2');">page2</a>
I don't understand why this is so? But is there any problem in echo's quotes.
Please Help.
that because you have syntax error while building anchors. Try to use double quotes for tag attributes and escape them with backslash.
So, your ECHO should look like this:
echo "<a id =\"{$t}\" href=\"javascript:void(0)\" onclick=\"open_page('ajaxinfo.php','content'); javascript:change('{$t}');\">{$t}</a> ";
You have to have code to add the contents returned by the ajax to the page. I don't see that anywhere.