开发者

PHP form takes back to top

I have PHP included a PHP form in my portfolio, that you can see here http://www.tinybigstudio.com The thing is that after SUBMIT, the page goes to TOP instead of staying at the bottom where the form is.

This is the PHP code I have:

<?php
//If the form is submitted
if(isset($_POST['submit'])) {

    //Check to make sure that the name field is not empty
    if(trim($_POST['contactname']) == '') {
        $hasError = true;
    } else {
        $name = trim($_POST['contactname']);
    }



    //Check to make sure sure that a valid email address is submitted
    if(trim($_POST['email']) == '')  {
        $hasError = true;
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    //Check to make sure comments were entered
    if(trim($_POST['message']) == '') {
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['message']));
        } else {
            $comments = trim($_POST['message']);
        }
    }

    //If there is no error, send the email
    if(!isset($hasError)) {
        $emailTo = 'info@tinybigstudio.com'; //Put your own email address here
        $body = "Name: $name \n\nEmail: $email  \n\nComments:\n $comments";
        $headers = 'From: My Site <'.$emailTo.'>' .$subject = "You have mail, yes!" . "\r\n" . 'Te lo manda: ' . $name;

        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }
}
?>

And this is the form:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
                        <fieldset>
                            <label for="name">Name</label>
                            <input type="text" size="50" name="contactname" id="contactname" value="" class="required">

                            <label for="email">Email</label>
                            <input type="text" size="50" name="email" id="email" value="" class="required">

                            <label for="message">Message</label>
                            <textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>

                        <input type="submit" value="S E N D" class="send" name="submit"  onmouseover="this.className='send_hover';"
                    开发者_StackOverflow                 onmouseout="this.className='send';">
                    </form>



<?php if(isset($hasError)) { //If errors are found ?>
                <p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
            <?php } ?>

            <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
                <p><strong>Message Successfully Sent!</strong></p>
                <p>Thank you <strong><?php echo $name;?></strong> for sending a message!</p>
            <?php }
            ?>


you need to add an anchor to the contact form and use this as the form action.eg:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>#contact" >

and add this 'pseudo-link' right before the contact form

<a name="contact">Contact me</a>

Now after submit user will be send to the form directly and not the top of the page


This is because you are submitting it to the same page that you are on, therefore the browser goes effectively refreshes with the $_POST data. If you want to stop this you need to use AJAX to submit it so that it happens in the background. Take a look at the API - http://api.jquery.com/jQuery.ajax/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜