dqitk20644 2014-05-26 09:48
浏览 60
已采纳

表单不会使用锚点部分进行验证

I have a form on a free standing contact form, however when I attempt to use it at the bottom of a scrolling page or in a section with an anchor it won't visually validate. In other words the error messages won't appear. The form will send an email if all the required fields are filled.But visually nothing happens!? http://patrickmchugh.com/test/

To see how it should work, please see a free standing contact.php patrickmchugh.com/test/contact.php This is how the error messages should work.

<?php   
// check for form submission - if it doesn't exist then send back to contact form  
if (!isset($_POST['save']) || $_POST['save'] != 'contact') { 
    header('Location: index.php#content'); exit; 
} 

// get the posted data 
$name = $_POST['contact_name']; 
$email_address = $_POST['contact_email']; 
$subject = $_POST['contact_subject'];
$message = $_POST['contact_message']; 

// check that a name was entered 
if (empty($name)) 
    $error = 'You must enter your name.'; 
// check that an email address was entered 
elseif (empty($email_address))  
    $error = 'You must enter your email address.'; 
// check for a valid email address 
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address)) 
    $error = 'You must enter a valid email address.'; 
// check that a message was entered 
elseif (empty($message)) 
    $error = 'You must enter a message.'; 

// check if an error was found - if there was, send the user back to the form 
if (isset($error)) { 
    header('Location: index.php?e='.urlencode($error).'#content2'); exit;
} 

// write the email content 
$email_content = "Name: $name
"; 
$email_content .= "Email Address: $email_address
"; 
$email_content .= "Subject: $subject
";
$email_content .= "Message:

$message"; 

// send the email 
mail ("patrick@patrickmchugh.com", "Enquiry from Connolly O'Neill Website", $email_content); 

// send the user back to the form 
header('Location: index.php?s='.urlencode('Thank you for your message.').'#contact2'); exit; 

?>

  • 写回答

1条回答 默认 最新

  • doulou1989 2014-05-26 09:51
    关注

    After submit form your url will be ?e=You+must+enter+your+name.#content2 so you need to get and echo error on your form page like:-

    if(isset($_GET['e'])) {
      echo $_GET['e'];  // or echo urldecode($_GET['e']);
    }
    

    Same for after successfully submit like:-

    if(isset($_GET['s'])) {
      echo $_GET['s'];  // or echo urldecode($_GET['s']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler