0

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

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

有什么办法可以让提交按钮也重定向到包含表单的 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>
4

1 回答 1

5

尝试将锚 id 添加到actionurl:

<form method="post" action="index.php#div4">
于 2012-05-29T13:35:29.953 回答