PDA

View Full Version : redirecting thingy in a script



Briggs
03-17-2008, 05:28 AM
Hey, I'm using the script found here
http://www.zimmertech.com/tutorials/php/25/comment-form-script-tutorial.php

down in the third part



<?php
//Please set the following variables for your mysql database:
$db_hostname = "localhost"; //usually "localhost be default"
$db_username = ""; //your user name
$db_pass = ""; //the password for your user
$db_name = ""; //the name of the database


/*MYSQL DATABASE CONNECTION/ TRACKING FUNCTIONS
--------------------------------------*/
// connect to database
$dbh = mysql_connect ($db_hostname, $db_username, $db_pass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($db_name);


$tuturl = $_POST["tuturl"];
$tutid2 = $_POST["tutid2"];
$name = $_POST["name"];
$url = $_POST["url"];
$email = $_POST["email"];
$message = $_POST["message"];

$sendcomment = mysql_query("INSERT INTO comments SET tutorialid='$tutid2', name='$name', url='$url', email='$email', comment='$message', date=now()");
if($sendcomment){
//header("Location: $tuturl");
echo "<h1>Submission Successful</h1>";
echo "Your comment has been submitted. You will now be redirected back to the last page you visited. Thanks!";
echo "<meta http-equiv='refresh' content='2;URL=$tuturl'>";
} else {
echo "There was an error with the submission. ";
}

?>

the redirect is kinda weird, it isn't working :p
I know absolutely almost nothing about php

how do I change the redirect to be the page you placed the comment?

Thanks,
Briggs

city_coder
03-17-2008, 08:59 AM
Hmmm it works for me. Make sure that the URL that is being passed through has 'http://' on the front of it and if it doesnt you'll need to append to it.

Also make sure that the variable is actually set and passed through, which brings me to if($sendcomment) {

Do you want to see if it has worked? that the insert was successful?
if u want to see if its been completed then do something like this



if($sendcomment == "1") {
echo "<h1>Submission Successful</h1>";
echo "Your comment has been submitted. You will now be redirected back to the last page you visited. Thanks!";
echo "<meta http-equiv='refresh' content='2;URL=$tuturl'>";
} else {
echo "There was an error with the submission. ";
}


I think it is 1 that it gets set to. Only one way to find out and thats to take out the redirect for a minute and just echo out the variable to see what it prints and then you know :D

Boxer
03-17-2008, 01:27 PM
Browsers expect the <META> element to be in the head of the page. I mean between the <head> and </head> tags. If you will place it there, it will work in all browsers.

You can do that if you put all the code at the beginning of the page, and leave only echo statements in the body:


<HTML>
<HEAD>
<TITLE>My great PHP page :)</TITLE>

<?php
//Please set the following variables for your mysql database:
$db_hostname = "localhost"; //usually "localhost be default"

//..........ALL THE STUFF FROM ABOVE....................

$sendcomment = mysql_query("INSERT INTO comments SET tutorialid='$tutid2', name='$name', url='$url', email='$email', comment='$message', date=now()");

if($sendcomment) echo "<META http-equiv='refresh' content='2;URL=$tuturl'>";
?>

</HEAD>
<BODY>

<?php
if($sendcomment){
echo "<h1>Submission Successful</h1>";
echo "<p>Your comment has been submitted. You will now be redirected back to the last page you visited. Thanks!</p>";
} else {
echo "<p>There was an error with the submission.</p>";
}
?>

</BODY>
</HTML>

Briggs
03-17-2008, 03:25 PM
I probably should mention that I'm using a php template :p

edit - Ok, I think I figured out a solution, I just added a "Go back" link in stead of it automatically taking you back

boogyman
03-17-2008, 03:52 PM
try



$message = $_POST["message"];

$sendcomment = mysql_query("INSERT INTO comments SET tutorialid='$tutid2', name='$name', url='$url', email='$email', comment='$message', date=now()");

if($sendcomment){
// N is the number of seconds to redirection
header("Location: $tuturl",N);
echo "<h1>Submission Successful</h1>";
echo "<p>Your comment has been submitted. Thanks!</p>";
echo '<p>You will now be redirected in N seconds. <a href="'.$tuturl.'">Click Here if you are not redirected</a>';
exit();
}
else {
echo "<p>There was an error with the submission.</p>";
}

?>

that will keep with your automated redirect, but it will give the user a chance to redirect themselves manually if there is an error