PHP表单提交时自动跳转到HTML锚点 [英] Jump to HTML anchor automatically when PHP form submits

查看:151
本文介绍了PHP表单提交时自动跳转到HTML锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将表单添加到我的索引页面中,以便当您单击提交时,它会在页面重新加载时自动返回到表单。现在,如果页面上有任何错误,它会在窗体上方显示它们,并给出一点感谢信息。



我目前有以下index.html页面:

 <?php 
includecheck.php;
?>
< form method =postaction =<?php echo htmlspecialchars($ _ SERVER [PHP_SELF]);?>>
名称:< br /> < input type =textname =namevalue =<?php echo $ _POST ['name'];?> size =30/>< br />< br />
电子邮件地址:< br /> < input type =textname =emailvalue =<?php echo $ _POST ['email'];?>大小= 30/> <峰; br /><峰; br />
公司名称:< br /> < input type =textname =companyNamevalue =<?php echo $ _POST ['companyname'];?> size =30/> <峰; br /><峰; br />
消息:< br />
< textarea style =resize:none; name =messagerows =5cols =30><?php echo $ _POST ['message']; ?>< / textarea的>
< br />
< input type =submitname =Submit/>
< / form>

当我提交页面时,它将运行check.php文件并验证所有数据是否正确。如果满足所有条件,它应该返回以下If / Then语句。

  if(!$ errors){
$ mail_to ='test@test.com';
$ subject ='来自表单提交的新邮件';
$ message ='发件人:'。 $ _POST ['name']。 \\\
;
$ message。='电子邮件:'。 $ _POST ['email']。 \\\
;
$ message。='公司名称:'。 $ _POST ['companyname']。 \\\
;
$ message。=消息:\ n。 $ _POST ['message']。 \\\
\\\
;
邮件($ mail_to,$ subject,$ message);

echo感谢您的电子邮件!< br />< br />;
$ _POST = array(); //提交

之后清除表单else else {
echo'< div style =color:red>'。 $错误。 <峰; br />< / DIV>;
}

是否有在谢谢期间添加自动函数或错误回声会自动将人员带回联系表单吗?



我希望我解释了我想要做的事。如果不是,请让我知道,我会尽力澄清一点。

解决方案

在窗体中添加一个ID并追加到表单动作URL的锚点

< form method =postid =myformaction =<?php echo htmlspecialchars ($ _SERVER [PHP_SELF]);?> #myform>


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.

解决方案

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

这篇关于PHP表单提交时自动跳转到HTML锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆