Hi guys,
In this code, which is on my server here.:
<!Doctype HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hide post</title>
</head>
<body>
<?php if (isset($_GET['toggle_post']) && $_GET['toggle_post'] == true && isset($_GET['post_id'])) {
$post_id = trim(strip_tags($_GET['post_id']));
// Check if a cookie exists already, if one does delete it
// and restore the post
if (isset($_COOKIE['hide_post_' . $post_id])) {
setcookie('hide_post_' . $post_id, false, time()-80000000);
}
else {
// Cookie will expire in about 900+ days time
setcookie('hide_post_' . $post_id, true, time()+80000000);
}
}
echo '<div id="post-1"' . (isset($_COOKIE['hide_post_1']) ? ' style="display: none;"' : ) . '>';
echo ' Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
echo '</div>';
echo '<br /><a href="' . $_SERVER['PHP_SELF'] . '/?toggle_post=true&post_id=1">' . (isset($_COOKIE['hide_post_1']) ? 'Show' : 'Hide') . ' Post</a>';
?>
</body>
</html>
I keep on getting an unexpected right parentheses error on line 22.
That makes sense because I can not see why the ) is there and it must be a typo on my part.
So I removed it, but then it tells me to remove the period, ‘.’, but that adds the closing to the div tag.
But, I still removed it and then it wants me to remove the semi-colon by telling me that is an unexpected error…:mad:
Can someone please help me see why it keeps on having me remove characters, even if they are supposed to be there?
Ah okay. Thank you for explaining it to me ti.
It
seems to get rid of the syntax error and parse the html, but now when I click hide post I am getting another error regarding headers…
You can’t set cookies when you’ve already outputted some information.
The should rearrange the code such that the setcookie gets moved to the top of the php file.
Does that make sense?
echo '<div id="post-1"' . (isset($_COOKIE['hide_post_1']) ? ' style="display: none;"' : ) . '>';
<?php if (isset($_GET['toggle_post']) && $_GET['toggle_post'] == true && isset($_GET['post_id'])) {
$post_id = trim(strip_tags($_GET['post_id']));
// Check if a cookie exists already, if one does delete it
// and restore the post
if (isset($_COOKIE['hide_post_' . $post_id])) {
setcookie('hide_post_' . $post_id, false, time()-80000000);
}
else {
// Cookie will expire in about 900+ days time
setcookie('hide_post_' . $post_id, true, time()+80000000);
}
}
echo ' Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
echo '</div>';
echo '<br /><a href="' . $_SERVER['PHP_SELF'] . '/?toggle_post=true&post_id=1">' . (isset($_COOKIE['hide_post_1']) ? 'Show' : 'Hide') . ' Post</a>';
?>
What I am trying to do is that I have an introduction that I think would be convient if the user old read once. That is why I am offering them the option to hide it.
If they click hide, then it is hidden.
And a cookie is stored so that everytime they visit the page, it is still hidden
However with this code, I couldn’t figure out how to write if statements for “if the cookie exists then keep it hidden, and if one doesn’t exist and the user has clicked hide, then write a cookie.”
Another thing that I wanted was if a cookie exists, and therefore the intro is hidden, then to show text which has the option to show it, in case they was to un-hide it.
So when this text, the word show, is clicked, the introduction will be shown with the hide text, in case they want to hide it again, and the hidden cookie is deleted so that the introduction remain shown until they click hide and set a cookie.
The thing is you can’t set a cookie halfway down a page, that’s just how HTTP was designed.
When you request a webpage, the webserver will first serve the headers (this happens transparently with PHP for the normal -non cookie, session, header()- headers, you have to do anything about this yourself) and after that it will serve the content.
Once the content starts it’s impossible to send any more headers, and cookies need to be sent as headers, hence, you cannot send any cookies to the browsers once you’ve started sending content.
So, your code should be something along the lines of
<?php
if( isset($_COOKIE['hide_post_' . $post_id]) ) {
setcookie('hide_post_' . $post_id, false, time()-80000000);
} else {
// Cookie will expire in about 900+ days time
setcookie('hide_post_' . $post_id, true, time()+80000000);
}
?
All html stuff and php stuff that doesn't involve cookies goes here
I am sorry if I am sounding stupid, but I just want to make sure…
Is this proper:
<?php if (isset($_GET['toggle_post']) && $_GET['toggle_post'] == true && isset($_GET['post_id'])) {
$post_id = trim(strip_tags($_GET['post_id']));
// Check if a cookie exists already, if one does delete it
// and restore the post
if (isset($_COOKIE['hide_post_' . $post_id])) {
setcookie('hide_post_' . $post_id, false, time()-80000000);
}
else {
// Cookie will expire in about 900+ days time
setcookie('hide_post_' . $post_id, true, time()+80000000);
}
}
?>
<!Doctype HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hide post</title>
</head>
<body>
<?php
echo '<div id="post-1"' . (isset($_COOKIE['hide_post_1']) ? ' style="display: none;"' : '') . '>';
echo ' Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
echo '</div>';
echo '<br /><a href="' . $_SERVER['PHP_SELF'] . '/?toggle_post=true&post_id=1">' . (isset($_COOKIE['hide_post_1']) ? 'Show' : 'Hide') . ' Post</a>';
?>
</body>
</html>
I think I really messed up because I do not even understand the code now
Starting from fresh, here is what I want to do is pseudo-php:
if (cookie that hides div with id #introduction does not exist )
{
set cookieExists varible to false;
}
else Cookie that hides div with id #introduction does exist
{
set cookieExists variable to true
add style=“display: none; visability:none;” inside the div tag with id #introduction
}
if ( p with id #option is clicked and cookieExists is false)
{
add style=“display: none; visability:none;” inside the div #introduction
write a cookie that hides div with id #introduction for the next time the page is loaded
write the text inside p with id #introduction to Show introduction
}
else p with id #introduction is clicked and cookieExists is true
{
add style=“display: block; visability:viasble;” inside the div #introduction tag
delete the cookie that hides div with id #introduction
write the text inside p with id #introduction to Hide
}
And then the html will look like this:
<body>
<div id="content">
<div id="option">I either say Hide or Show introduction when the php above writes me</div>
<br />
<div id="introduction">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris
</div>
</div>
</body>
Can you help me convert this intro real code?
Here is what I could write by myself
<?php
$cookieExists;
if (/*cookie that hides div with id #introduction does not exist*/)
{
$cookieExists===false;
}
else /* Cookie that hides div with id #introduction does exist */
{
$cookieExists===true;
/*add style="display: none; visability:none;" inside the div tag with id #introduction*/
}
If ( p with id #option is clicked && $cookieExists===false)
{
/*add style="display: none; visability:none;" inside the div #introduction
write a cookie that hides div with id #introduction for the next time the page is loaded*/
setcookie('hide_post_' . $post_id, true, time()+80000000);
/*write the text inside p with id #introduction to Show introduction */
}
else /* p with id #introduction is clicked && $cookieExists===true)
{
/*add style="display: block; visability:viasble;" inside the div #introduction tag*/
setcookie('hide_post_' . $post_id, false, time()-80000000);
/*write the text inside p with id #introduction to Hide*/
}