Jump to HTML anchor automatically when PHP form su

2020-04-30 15:57发布

I am trying to add a form into my index page, so that when you click on submit it will automatically return to the form when the page reloads. Right now if there are any errors on the page it will display them right above the form as well as give a little thank you message.

I currently have the following for the index.html page:

<?php
    include "check.php";
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    Name:<br/> <input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="30" /><br/><br/>
    Email Address:<br/> <input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="30"/> <br/><br/>
    Company Name:<br/> <input type="text" name="companyName" value="<?php echo $_POST['companyname']; ?>" size="30" /> <br/><br/>
    Message:<br/>
    <textarea style="resize: none;" name="message" rows="5" cols="30"><?php echo $_POST['message']; ?></textarea>
    <br/>
    <input type="submit" name="Submit" />
</form>

When I submit the page it will run through the check.php file and verify all the data is good. It should then return the following If/Then statement if all the conditions are met.

if (!$errors) {
    $mail_to = 'test@test.com';
    $subject = 'New Mail from Form Submission';
    $message  = 'From: ' . $_POST['name'] . "\n";
    $message .= 'Email: ' . $_POST['email'] . "\n";
    $message .= 'Company Name: ' . $_POST['companyname'] . "\n";
    $message .= "Message:\n" . $_POST['message'] . "\n\n";
    mail($mail_to, $subject, $message);

    echo "Thank you for your email!<br/><br/>";
    $_POST = array(); //Clear form after submit

} else {
    echo '<div style="color: red">' . $errors . '<br/></div>';
}

Is there anyway to add an auto-function during the "thank you" or "error" echos that will automatically bring the person back down to the contact form?

I hope I explained what I wanted to do correctly. If not please let me know and I will try to clarify it a bit better.

1条回答
混吃等死
2楼-- · 2020-04-30 16:14

Add an ID to the form and append the anchor onto the form action URL

<form method="post" id="myform" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>#myform">

查看更多
登录 后发表回答