How can I make a form submit button also redirect to a specific div in the page ?(如何使表单提交按钮也重定向到页面中的特定 div?)
问题描述
我正在做的网站是由一个全屏滑块组成的,有 4 个不同的面板.
The website i'm doing is made of a fullscreen slider, with 4 different panels.
第四个面板包含一个 php 联系表.当我提交表单时,它会从第一个滑块重新加载页面,因此用户无法看到第四个面板中的结果(发送的消息或任何错误)以及表单.
The fourth panel contains a php contact form. When I submit the form, it reloads the page from the first slider, so the user can't see the results (message sent, or any error) which are in the fourth panel, with the form.
有什么方法可以让提交按钮也重定向到包含表单的 div 吗?
Is there any way I can make the submit button also redirect to the div containing the form ?
这里是基本标记,使用了 php 脚本:
Here is the basic markup, with the php script used :
<div id="container">
<div id="div1">
<div class="content">...</div>
</div>
<div id="div2">
<div class="content">...</div>
</div>
<div id="div3">
<div class="content">...</div>
</div>
<div id="div4">
<div class="content">
<form method="post" action="index.php">
<label>Nom</label>
<input class="small" name="name" placeholder="Name">
<label>Email</label>
<input class="small" name="email" type="email" placeholder="Email">
<label>Message</label>
<textarea class="small" name="message" placeholder="Message"></textarea>
<label>What's the best burger ever (antispam)</label>
<input class="small" name="human" placeholder="type the right answer">
<input id="submit" name="submit" type="submit" value="send">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ...';
$to = 'johndoe@gmail.com';
$subject = 'New message';
$human = $_POST['human'];
$body = "From: $name
E-Mail: $email
Message:
$message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == 'bacon burger') {
if (mail ($to, $subject, $body, $from)) {
echo '<div class="alert green"><p>Message sent!</p></div>';
} else {
echo '<div class="alert red"><p>An error occured. Go back and try again.</p></div>';
}
} else if ($_POST['submit'] && $human != 'bacon burger') {
echo '<div class="alert red"><p>You answered wrong to the antispam question.</p></div>';
}
} else {
echo '<div class="alert red"><p>You have to fill in all fields !</p></div>';
}
}
?>
</div>
</div>
</div>
推荐答案
尝试在action
url中添加anchor id:
Try adding anchor id to action
url:
<form method="post" action="index.php#div4">
这篇关于如何使表单提交按钮也重定向到页面中的特定 div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使表单提交按钮也重定向到页面中的特定 div?


基础教程推荐
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01