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">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备