Help - Search - Members - Calendar
Full Version: [resolved]Comments Almost working
Codegrrl.com Forums > Script Help > Tutorial Help
tjs
I have the blog comments working, but when a user posts a comment, the process.php shows up. How can I make it so that when a user posts a comment, they are taken back to the entry page they commented on?
Amelie
It should do that automatically... Can you post your code?
tjs
QUOTE(Amelie @ Feb 25 2007, 02:56 PM) *

It should do that automatically... Can you post your code?


Here's my process.php code:

CODE

<?php
if (isset($_POST['submit_comment'])) {
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comment'])) {
die("You have forgotten to fill in one of the required fields! Please make sure you submit a name, e-mail address and comment.");
}
$entry = htmlspecialchars(strip_tags($_POST['entry']));
$timestamp = htmlspecialchars(strip_tags($_POST['timestamp']));
$name = htmlspecialchars(strip_tags($_POST['name']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$url = htmlspecialchars(strip_tags($_POST['url']));
$comment = htmlspecialchars(strip_tags($_POST['comment']));
$comment = nl2br($comment);
if (!get_magic_quotes_gpc()) {
$name = addslashes($name);
$url = addslashes($url);
$comment = addslashes($comment);
}
if (!eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
die("The e-mail address you submitted does not appear to be valid. Please go back and correct it.");
}
mysql_connect ('******', '******', '******');
mysql_select_db ('******');
$result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");
header("Location: blog_entry.php?id=" . $entry);
}
else {
die("Error: you cannot access this page directly.");
}
?>


Any help would be appreciated! smile.gif

EDIT: Sorry about that! I fixed the code above!
Amelie
I meant process.php - that's the part that should redirect tongue.gif
tjs
QUOTE(Amelie @ Feb 25 2007, 09:45 PM) *

I meant process.php - that's the part that should redirect tongue.gif


Whoops! Ok, I fixed the code above. blush.gif
Amelie
Yes, it definitely should redirect...

CODE
header("Location: blog_entry.php?id=" . $entry);


^ That part should do it...
tjs
QUOTE(Amelie @ Feb 25 2007, 09:55 PM) *

Yes, it definitely should redirect...

CODE
header("Location: blog_entry.php?id=" . $entry);


^ That part should do it...


But I already have that code in the process.php and it's not redirecting. Now what? blink.gif
Amelie
I'm not sure why it isn't doing it... I just tested out your coding on my site and it redirects me just fine... :\ Do you get an error or anything, or is it a blank page?

Can you link us to the site (if possible)?

tjs
QUOTE(Amelie @ Feb 25 2007, 11:13 PM) *

I'm not sure why it isn't doing it... I just tested out your coding on my site and it redirects me just fine... :\ Do you get an error or anything, or is it a blank page?

Can you link us to the site (if possible)?


Here's the link to my blog to see what's wrong:
http://www.thejesussite.net/blog.php

Try to leave a comment without typing anything, and you'll see the process.php page that says "Whoops" and the error message.
Then leave a comment and all you will see is "whoops" and no error message, but the comment will be posted.
Amelie
It's not working because you have included your header at the top of process.php, it would seem. You can't include your header there, or it won't redirect...

Can you post blog_process.php exactly as it is on your site?
tjs
QUOTE(Amelie @ Feb 26 2007, 04:57 PM) *

It's not working because you have included your header at the top of process.php, it would seem. You can't include your header there, or it won't redirect...

Can you post blog_process.php exactly as it is on your site?


Here's blog_process.php:

CODE
<head></head>
<body id="blog">    
<div id="container">
<?php include('header.php');?>
<div id="wrapper">
<div id="content-wrapper">
<div id="content">
<dl>
<h1>Whoops!</h1>
<dd>
<?php
if (isset($_POST['submit_comment'])) {
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comment'])) {
die("You have forgotten to fill in one of the required fields! Please make sure you submit a name, e-mail address and comment.");
}
$entry = htmlspecialchars(strip_tags($_POST['entry']));
$timestamp = htmlspecialchars(strip_tags($_POST['timestamp']));
$name = htmlspecialchars(strip_tags($_POST['name']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$url = htmlspecialchars(strip_tags($_POST['url']));
$comment = htmlspecialchars(strip_tags($_POST['comment']));
$comment = nl2br($comment);
if (!get_magic_quotes_gpc()) {
$name = addslashes($name);
$url = addslashes($url);
$comment = addslashes($comment);
}
if (!eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
die("The e-mail address you submitted does not appear to be valid. Please go back and correct it.");
}
mysql_connect ('****', '****', '****');
mysql_select_db ('****');
$result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");
header("Location: blog_entry.php?id=" . $entry);
}
else {
die("Error: you cannot access this page directly.");
}
?>
</dd></dl></div></div>
<div id="sidebar-wrapper">
<div id="sidebar">
<ul>
<?php include('menu.php');?>
</ul></div></div></div>
<?php include('footer.php');?>
</div>
</body>
</html>


I keep tinkering with the code but I still can't get it to work properly. sad.gif
Amelie
You can't have any HTML at the top of process.php file or it won't redirect.

Try this:

CODE
<?php
if (isset($_POST['submit_comment'])) {
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comment'])) { ?>
<head></head>
<body id="blog">    
<div id="container">
<?php include('header.php');?>
<div id="wrapper">
<div id="content-wrapper">
<div id="content">
<dl>
<h1>Whoops!</h1>
<dd>You have forgotten to fill in one of the required fields! Please make sure you submit a name, e-mail address and comment.
</dd></dl></div></div>
<div id="sidebar-wrapper">
<div id="sidebar">
<ul>
<?php include('menu.php');?>
</ul></div></div></div>
<?php include('footer.php');?>
</div>
</body>
</html>
<?php exit;
}
$entry = htmlspecialchars(strip_tags($_POST['entry']));
$timestamp = htmlspecialchars(strip_tags($_POST['timestamp']));
$name = htmlspecialchars(strip_tags($_POST['name']));
$email = htmlspecialchars(strip_tags($_POST['email']));
$url = htmlspecialchars(strip_tags($_POST['url']));
$comment = htmlspecialchars(strip_tags($_POST['comment']));
$comment = nl2br($comment);
if (!get_magic_quotes_gpc()) {
$name = addslashes($name);
$url = addslashes($url);
$comment = addslashes($comment);
}
if (!eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
die("The e-mail address you submitted does not appear to be valid. Please go back and correct it.");
}
mysql_connect ('****', '****', '****');
mysql_select_db ('****');
$result = mysql_query("INSERT INTO php_blog_comments (entry, timestamp, name, email, url, comment) VALUES ('$entry','$timestamp','$name','$email','$url','$comment')");
header("Location: blog_entry.php?id=" . $entry);
}
else {
die("Error: you cannot access this page directly.");
}
?>


I have moved your HTML code to appear only when a person hasn't filled in the required fields. That way, if they have filled everything in correctly, it should redirect.
tjs
YES!!! Thank you that did it! clap.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.