Click to See Complete Forum and Search --> : Return to place on page


StraightShooter
12-05-2006, 01:42 AM
I have a page that has an article on it and at the end I have a comment form (it's not online right now). It's not a comment form that displays comments on the page. It's a form that someone uses to send comments to me. Instead of having them redirected to another page, I just have the "thank you" displayed at the bottom. When the form is filled out and submitted, the page is refreshed and a small thank you message is printed out with the person's name. However, when they submit the page, it returns back to the top. They would have to know to scroll down to the bottom to see the message.

How can I have the page refresh to the bottom with the message displayed?

bradgrafelman
12-05-2006, 02:36 AM
Try using named anchors in the thank you message: <p><a name="thankyou"></a>Thank you!</p> Then you would add "#thankyou" onto the end of the form action.

StraightShooter
12-05-2006, 10:09 AM
I tried that. I didn't work. Any other ideas?

stolzyboy
12-05-2006, 10:20 AM
well, that should work as it is basic html... what is the code you used to create the anchor tag and to point to it?

StraightShooter
12-05-2006, 10:36 AM
I tried using the format below:


header("Location: http://www.shamar.org/_lee/form-test.php#FORM");

However, I also get an error code:

Warning: Cannot modify header information - headers already sent by (output started at /home/shamaror/public_html/_lee/form-test.php:12) in /home/shamaror/public_html/_lee/form-test.php on line 88

The full code is below (probably a bit of a mish-mash - just a learner at this point):


<?php
$name=$_POST['name'];
$email=strtolower($_POST['email']);
$comments=$_POST['comments'];

$tried=($_POST['tried']=='yes');
if($tried) {
$validated=(!empty($name) && !empty($email) && !empty($comments));
if (!$validated) {
?>
<p style="color: red;">
The name, email, and comments are required fields.<br>Please fill them out to continue.</p>

<?php
}
header("Location: http://www.shamar.org/_lee/form-test.php#FORM");
}

if (!$email) {
?>
<form action="<?= $PHP_SELF ?>" method="POST">
Name: <input type=text name="name" value="<?= $name ?>"><br>
Email: <input type=text name="email" value="<?= $email ?>"><br>
Comments: <textarea name="comments"><?= $comments ?></textarea><br>

<input type="hidden" name="tried" value="yes">
<input type="submit" value="<?php echo $tried ? 'Re-Submit' : 'Submit' ; ?>">
</form>
<?php
}

/* else
{ */

if ($tried && $validated) {
$myemail = "leeu@cfl.rr.com";
$mailheaders = "From: $myemail \n";
if ($email) { mail ( $myemail, "ENS Comments", "ENS Comment Posting:\n\nFrom: $name\nEmail: $email\n\n$comments\n\nhttp://www.shamar.org$QUERY_STRING", $mailheaders);
}
echo "<div style='color: blue;'><br>Thank you, $name. I appreciate your comments.<br>I will respond as soon as I am able. Blessings...";
}
?>

stolzyboy
12-05-2006, 10:51 AM
you have a header redirect


header('Location: http://www.google.com');


you can't have any html or whitespace BEFORE a header(), you can either fix that issue or place ob_start(); as the FIRST line of your code before the html


<?php
ob_start();
?>

StraightShooter
12-05-2006, 11:02 AM
I'm not sure where you mean. The php stops and starts several times on the page. When I placed it above the redirect, I still received the error message.

BTW, I edit the code above and included the redirect where I had it (it's in red).

stolzyboy
12-05-2006, 11:33 AM
put the ob_start() in there, see if that helps... you likely have some whitespace showing up... you HAD html before the header but you edited it since i posted

stolzyboy

StraightShooter
12-05-2006, 05:20 PM
O.k., I placed the ob_start() at the start, before the DOCTYPE. It sends the e-mail and ends up at the same place. Thanks! However, I don't get the "thank you" message or the error message if I miss an entry. I just need a bit more tweaking ...

If I use the following instead of a direct URL, the thank you and error messages display but I don't return to the lower part of the page:

header("Location: $PHP_SELF");

stolzyboy
12-06-2006, 08:51 AM
what does your full code look like for the code right now?

rincewind456
12-06-2006, 10:54 AM
I take it from your code that you want the page to go to this section echo "<div style='color: blue;'><br>Thank you, $name. I appreciate your comments.<br>I will respond as soon as I am able. Blessings...";
}
?>

In which case you haven't added an anchor for it to go to. You need to do what brad suggested in your case something like this:
echo "<div style='color: blue;'><br><a name="FORM">Thank you, $name.</a> I appreciate your comments.<br>I will respond as soon as I am able. Blessings...";
}
?>

stolzyboy
12-06-2006, 11:05 AM
that's kind of what i'm getting at... i've yet to see the ANCHOR

StraightShooter
12-06-2006, 06:02 PM
Sorry, the anchor was located just above the code:

<a name="FORM"></a>
If I use it as rincewind45 said, it still doesn't work. See my last post above.

Here is the complete page ...

<?php
ob_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>#########</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="">
<meta name="keywords" content="">

<link rel="stylesheet" type="text/css" href="style/cssNews.css">
<script type="text/javascript" src="javascript/common.js"></script>

</head>

<body>

<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>


<br><br>
<a name="FORM"></a>
<?php
$name=$_POST['name'];
$email=strtolower($_POST['email']);
$comments=$_POST['comments'];

$tried=($_POST['tried']=='yes');
if($tried) {
$validated=(!empty($name) && !empty($email) && !empty($comments));
if (!$validated) {
?>
<p style="color: red;">
The name, email, and comments are required fields.<br>Please fill them out to continue.</p>

<?php
}
header("Location: $PHP_SELF#FORM");
}

if (!$email) {
?>
<form action="<?= $PHP_SELF ?>" method="POST">
Name: <input type=text name="name" value="<?= $name ?>"><br>
Email: <input type=text name="email" value="<?= $email ?>"><br>
Comments: <textarea name="comments"><?= $comments ?></textarea><br>

<input type="hidden" name="tried" value="yes">
<input type="submit" value="<?php echo $tried ? 'Re-Submit' : 'Submit' ; ?>">
</form>
<?php
}


if ($tried && $validated) {
$myemail = "leeu@cfl.rr.com";
$mailheaders = "From: $myemail \n";
if ($email) { mail ( $myemail, "ENS Comments", "ENS Comment Posting:\n\nFrom: $name\nEmail: $email\n\n$comments\n\nhttp://www.shamar.org$QUERY_STRING", $mailheaders);
}
echo "<div style='color: blue;'><br>Thank you, $name. I appreciate your comments.<br>I will respond as soon as I am able. Blessings...";}
?>


</div>

<div id="footer">
<?php include("footerTab.inc"); ?>
</div>

</div>

</body>
</html>

rincewind456
12-06-2006, 08:15 PM
I hate to say this but your form is a mess. That said this works as you want it to <?php
//ob_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>#########</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="">
<meta name="keywords" content="">

<link rel="stylesheet" type="text/css" href="style/cssNews.css">
<script type="text/javascript" src="javascript/common.js"></script>

</head>

<body>

<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
<br>
<br>

<?php
$name=$_POST['name'];
$email=strtolower($_POST['email']);
$comments=$_POST['comments'];

$tried=($_POST['tried']=='yes');
if($tried) {
$validated=(!empty($name) && !empty($email) && !empty($comments));
if (!$validated) {
?>
<p style="color: red;"><a name="FORM"></a>
The name, email, and comments are required fields.<br>Please fill them out to continue.</p>

<?php
}
header("Location:".$_SERVER['PHP_SELF']."#FORM");
}

if (!$email) {
?>
<form action="<?php echo $_SERVER['PHP_SELF'].'#FORM' ?>" method="POST">
Name: <input type=text name="name" value="<?php if (isset($_POST['name'])) {echo $_POST['name'] ; }?>"><br>
Email: <input type=text name="email" value="<?php if(isset($_POST['email'])) {echo $_POST['email'];} ?>"><br>
Comments: <textarea name="comments"><?php if (isset($_POST['comments'])) {
echo $_POST['comments'];
} ?></textarea><br>

<input type="hidden" name="tried" value="yes">
<input type="submit" name="submit" value="<?php echo $tried ? 'Re-Submit' : 'Submit' ; ?>">
</form>
<?php
}


if ($tried && $validated) {
$myemail = "leeu@cfl.rr.com";
$mailheaders = "From: $myemail \n";
if ($email) { mail ( $myemail, "ENS Comments", "ENS Comment Posting:\n\nFrom: $name\nEmail: $email\n\n$comments\n\nhttp://www.shamar.org$QUERY_STRING", $mailheaders);
}
echo "<div style='color: blue;'><a name='FORM'></a><br>Thank you, $name. I appreciate your comments.<br>I will respond as soon as I am able. Blessings...";}

?>


</div>

<div id="footer">
<?php include("footerTab.inc"); ?>
</div>

</div>

</body>
</html>

I could only get it to work without the ob_start(), something I don't use if I can help it anyway.

Your script has lots of undefined variable and index errors

Notice: Undefined index: name in *** on line 45

Notice: Undefined index: email in *** on line 46

Notice: Undefined index: comments in *** on line 47

Notice: Undefined index: tried in *** on line 49

Notice: Undefined variable: QUERY_STRING in *** on line 81


The reason it wasn't working before is that if the form was filled in correctly then the header function was not being called.

It is only used when there is an error, so when the form submits it goes to the address defined in the action attribute of the form. Hence by changing that attribute it works as you want.

However I don't think that this is the best way to go about it, and I woukd not recommend it for general usage.

StraightShooter
12-06-2006, 09:08 PM
As I said in the beginning, I am learning. It is a combination of two different scripts. Why would you say "I woukd not recommend it for general usage"? I am trying to learn so it would be great if you could tell me why it is a bad idea. I think the idea, submitting a form and having it show the "thank you" on the same page, is an excellent idea as it eliminates the need for JavaScript. (I know, there other methods to allow for those who don't have JS.) How would you write it, if you don't mind me asking?

rincewind456
12-07-2006, 06:49 AM
It's not the general concept of using one script for doing all that you are doing, there's nothing wrong with that.

It's all the hacks that have to be made to "get it to work" that could well cause problems if you ever have to change anything on the form at a later date.

I understand that you're learning at this point, and I'm not criticising you, because it's only through doing things that you learn. Some; no most of my early scripts where hideous to behold. All I am doing is offering my opinion, and I think that you would be better off rethinking the flow of the form.

If I have time over the weekend I'll put something together to show you what I mean. In the meantime did those changes work for you?

Weedpacket
12-08-2006, 01:39 AM
It's all the hacks that have to be made to "get it to work" that could well cause problems if you ever have to change anything on the form at a later date.

I understand that you're learning at this point, and I'm not criticising you, because it's only through doing things that you learn. Some; no most of my early scripts where hideous to behold. All I am doing is offering my opinion, and I think that you would be better off rethinking the flow of the form.

As Fred Brooks put it: "Where a new system concept or new technology is used, one has to build a system to throw away, for even the best planning is not so omniscient as to get it right the first time. Hence plan to throw one away; you will, anyhow."

One corollary of that is, plan to build one to throw away and you'll end up throwing away the second as well.

StraightShooter
12-08-2006, 01:53 PM
Thanks rincewind456, I'll have to work on it this weekend. Busy here at work , you know ...

I appreciate yours and everybody else's help. I 'll get back to you.