Jump to HTML anchor automatically when PHP form submits

怎甘沉沦 提交于 2019-12-02 09:25:08

问题


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:


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">



来源:https://stackoverflow.com/questions/25531543/jump-to-html-anchor-automatically-when-php-form-submits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!