drqn5418 2015-01-02 11:40
浏览 8
已采纳

PHP转到部分

Okay, so I have a PHP script which is a contact form (as shown below)

$body = "From: $name
 E-Mail: $email
 Message:
 $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
    if ($human == '4') {                 
        if (mail ($to, $subject, $body, $from)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    } 
} else if ($_POST['submit'] && $human != '4') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
    echo '<p>You need to fill in all required fields!</p>';
  }
 }
?>    

And I was wondering (since I'm not very experienced in PHP) if there was a way to bring me back to the div where the contact form is if there was an error? Thanks in advance EDIT: HTML CODE: http://pastebin.com/CwXsDapB

  • 写回答

3条回答 默认 最新

  • doumin4553 2015-01-02 11:43
    关注

    Yes, you can store your errors in session.

    And again redirecting to form.

    e.g.

    $_SESSION['error'] = '<p>Something went wrong, go back and try again!</p>';
    header("Location:YOUR_FORM.PHP?error#form_div");
    

    Add add id form_div to div containing your form.

    <div id="form_div">
    <?php
    if (! empty($_SESSION['error'])) {
      echo $_SESSION['error'];
      $_SESSION['error'] = '';
    }
    ?>
    <form>
    
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏