php - 如何使表单提交按钮也重定向到页面中的特定 div?

标签 php javascript slider anchor contact-form

我正在做的网站是由全屏 slider 组成,有 4 个不同的面板。

第四个面板包含一个 PHP 联系表单。当我提交表单时,它会从第一个 slider 重新加载页面,因此用户无法看到第四个面板中表单的结果(发送的消息或任何错误)。

有什么方法可以让提交按钮也重定向到包含表单的 div 吗?

这是基本标记,使用了 php 脚本:

<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\n E-Mail: $email\n Message:\n $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>

最佳答案

尝试将 anchor ID 添加到 action url:

<form method="post" action="index.php#div4">

关于php - 如何使表单提交按钮也重定向到页面中的特定 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10800212/

相关文章:

php - 如果不是数组,为什么 php 不显示未定义索引错误?

javascript - android 上的 html 输入框点击时有绿色突出显示

javascript - 如何在 Bootstrap 网站中添加 Logo /客户端 slider

javascript - 改变 slider 颜色

php - 调用成员函数是否为null?

php - 使用求和函数时,我的查询返回单条记录

php - 从服务器检索数据花费了太多时间

javascript - 我如何在 map 上显示观察位置经纬度

javascript - 取消 Angular mddialog 后传递的值仍在编辑

JavaScript 初始化 + html5 标签