Jump to content

returning to the same point in a page after form submission


terungwa

Recommended Posts

On my current blog development project,when the user clicks post reply, is there a way I can have the page retain it's  location or  to auto scroll the user back down to where they clicked post reply at the buttom of the page? This is most useful when the reply or comment thread is long.

 

Also when their is an error when a user posts a reply, he also needs to be auto scrolled down to the reply/comment form and not the top of the page.

 

Kindly advice steps i may take to address this.

 

Point to note:

  • The reply form posts to itselt, that is the action attribute is blank.

Find below an excerpt of my reply.php script.

if (isset($_POST['post_reply']))
{
	$missing = array();//Declare An Array to store any error message
	if (empty($reply))
		{
			$missing[] = 'Please Enter a reply.';
		} 
	if (empty($missing))
		{
			// initialize flag
			$OK = false;
			// initialize prepared statement
			$stmt = $mysqli->stmt_init();
			// create SQL
			$sql = 'INSERT INTO comments (post_content, post_date, post_topic, post_by)
			  VALUES(?, NOW(), ?, ?)';
			if ($stmt->prepare($sql)) 
				{
					// bind parameters and execute statement
					$stmt->bind_param('sis', $reply, $id, $Memberid);
					// execute and get number of affected rows
					$stmt->execute();
					if ($stmt->affected_rows > 0) {
					 $OK = true;} 
				}
		}
}

if(isset($missing))
{
	echo '<div class="panel callout error"> <ul>';
			foreach ($missing as $key => $values)
				{								
					echo '<li>'.$values.'</li>';       
				}
					echo '</ul></div>';
}
//show reply box
echo '<form method="post" action="">
		<textarea name="reply-content" rows="9"></textarea><br /><br />
		<input type="submit" value="Submit reply" name="post_reply" />
	</form>';

I've never tried it but seems to me you can add an anchor to the blank post.

echo '<form method="post" action="#myanchorname">

then you need to have

<a name="myanchorname"></a>

on the page where you want to come back to

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.