Jump to content

PHP Scroll to bottom of page


mat420

Recommended Posts

Is there a code I can put with each of these errors so that, on error, the page reloads back to the bottom of the page?  When you enter the wrong info, the page already reloads but it reloads to the top of the page.  People who didnt correctly information into the webpage may not see that they're receiving an error if it doesnt scroll back down.

I attempted to put the error at the to of the page but I couldnt find a good spot. I thought about doing an error message box but I'm not sure thats the best method

Anyway heres the code, thanks a lot.

 


<?php
if ($_POST['send']) {
$errors = array();
if ($_POST['captcha'] != $_SESSION['captchacode']) {
	$errors[] = "You didn't enter the correct letters!";


<-------------ID LIKE TO PUT A CODE HERE AND
}
if (empty($_POST['email']))  {
	$errors[] = "Please enter an e-mail address"; 


<-----------------HERE TO MAKE THE PAGE GO TO THE BOTTOM WHEN IT REFRESHED (IT REFRESHES UPON EVERY IMPROPERLY FILLED OUT FORM


} 
else if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) 
{ 
$errors[] = 'Please enter a VALID e-mail address'; 
} 
if (!count($errors)) {
	// IMPORTANT: If you don't call this the 
	// user will keep getting the SAME code!
	captchaDone();

Link to comment
Share on other sites

Hi,

 

I have not looked at your code but the way i do it is like this:

 

Where your form is place this html bookmark rite above it:

 

<a id="form"></a>
<!-- YOUR FORM HERE -->

 

Now where you have action in your form tag like:

 

<form method="post" action="<?php echo basename(__file__); ?>">

 

Simply add after file name (above i done it dynamically) ; simply add:

 

#form

 

It would now look like this:

 

<form method="post" action="<?php echo basename(__file__); ?>#form">

 

What happens then is when the form is submitted to the same page it will keep the form in same place as you have now added a bookmark to the action in the form tag. This will save the user from having to re-scroll back down the page. I do however also ensure form errors are placed rite below the bookmark tag so obviously a user can see the errors and not have to scroll up the page to see them etc as it may annoy and confuse people.

 

You should also consider using preg_match() and not eregi() as from php 5.30 eregi has been depreciated.

 

http://php.net/manual/en/function.eregi.php

http://php.net/manual/en/function.preg-match.php

 

Hope this helps.

Link to comment
Share on other sites

sorry i know nothing about php you lost me at #form

 

Hi,

 

Well where you have your form tag:

 

<form method="post" action="myform.php">

- myform.php being name of file

 

simply add #form after myform.php so it looks like:

 

<form method="post" action="myform.php#form">

 

Basically form can be named anything you like it's just corrosponds to the id given in the bookmark tag so when the form submits it will keep the form in in view so no more scrolling. if you change form to something else you must of course also change it in the bookmark tag so they match otherwise it won't work.

 

I'm sorry i cannot explain it easier than that unless someone else can.

 

 

Link to comment
Share on other sites

i love you. worked perfect.

 

correct me if im wrong but all i did was add #form to the end of the form action (but within the quotes like u did) and then put <a id="form"></a>

 

at the very end/bottom of the page.

 

Link to comment
Share on other sites

i love you. worked perfect.

 

correct me if im wrong but all i did was add #form to the end of the form action (but within the quotes like u did) and then put <a id="form"></a>

 

at the very end/bottom of the page.

 

That's correct :)

 

like i said you can change the name form to anything you want in the form action but if you do just change it in the bookmark tag to.

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.