doudi5892 2012-05-29 13:32
浏览 71
已采纳

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

The website i'm doing is made of a fullscreen slider, with 4 different panels.

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.

Is there any way I can make the submit button also redirect to the div containing the form ?

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>
  • 写回答

1条回答 默认 最新

  • dory4404 2012-05-29 13:35
    关注

    Try adding anchor id to action url:

    <form method="post" action="index.php#div4">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题