Jump to content

returning to the same point in a page after form submission


terungwa
Go to solution Solved by davidannis,

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>';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.