2013-07-10 8 views
0

Как перенаправить сообщения об ошибках из моего html_form_send.php обратно на мою страницу email.php. Вот то, что я получил, у меня есть форма email.php:Переадресация сообщений об ошибках

email.php

<form name = 'htmlform' action = '$template/html_form_send.php' 
     method = 'post' class = 'form-horizontal well' > 

Мои email.php ссылки на мой html_form_send.php, который имеет следующий код:

html_form_send.php

<?php 
    if(isset($_POST['email'])) { 

    // CHANGE THE TWO LINES BELOW 
    $email_to = "my email"; 
    $email_subject = "my subject"; 

    function died($error) { 
     // your error code can go here 
     echo "We are very sorry, but there were error(s) found \ 
      with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error . "<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if (!isset($_POST['name']) || !isset($_POST['email']) || 
     !isset($_POST['telephone']) || !isset($_POST['password'])) { 
     died('We are sorry, but there appears to be a problem \ 
      with the form you submitted.');  

     // and instead redirect the user to your error page 
     header("Location: http://hurstblog.co.uk/contact-error"); 
    } 

    $name = $_POST['name']; // required 
    $email_from = $_POST['email']; // required 
    $telephone = $_POST['telephone']; // not required 
    $password = $_POST['password']; // required 

    $error_message = ""; 
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
    if (!preg_match($email_exp,$email_from)) { 
     $error_message .= 'The Email Address you entered does not \ 
      appear to be valid.<br />'; 
    } 
    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if (!preg_match($string_exp,$name)) { 
     $error_message .= 'The First Name you entered does not \ 
      appear to be valid.<br />'; 
    } 
    if (strlen($telephone) < 2) { 
     $error_message .= 'The telephone you entered do not \ 
      appear to be valid.<br />'; 
    } 
    if (strlen($password) < 2) { 
     $error_message .= 'The password you entered do not \ 
      appear to be valid.<br />'; 
    } 
    if (strlen($error_message) > 0) { 
     died($error_message); 
    } 
    $email_message = "Form details below.\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type", "bcc:", "to:", "cc:", "href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "Name: " . clean_string($name) . "\n"; 
    $email_message .= "Email: " . clean_string($email_from) . "\n"; 
    $email_message .= "Telephone: " . clean_string($telephone) . "\n"; 
    $email_message .= "Password: " . clean_string($password) . "\n"; 

    // create email headers 
    $headers = 'From: ' . $email_from . "\r\n". 
    'Reply-To: ' . $email_from . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    @mail($email_to, $email_subject, $email_message, $headers); 

    if (mail($email_to, $email_subject, $email_message, $headers)) { 
     header("Location: http://domain.net"); 
    } 
} 
die(); 

?> 

Мой вопрос, как я могу перенаправить ошибку?

+0

Что вы подразумеваете под перенаправлением ошибки –

+0

Вместо использования функции die() перенаправить страницу на email.php с ошибкой, переданной по переменной GET. –

+0

:) Отправьте его обратно на мою страницу email.php, чтобы он отображался на этой странице не на моем html_form_send.php – user2566655

ответ

0

html_form_send.php

// validation expected data exists 
if(!isset($_POST['name'])){ 
    header("location: email.php?error=name"); 
} 
if(!isset($_POST['email'])){ 
    header("location: email.php?error=email"); 
} 
if(!isset($_POST['telephone'])){ 
    header("location: email.php?error=telephone"); 
} 
if(!isset($_POST['password'])){ 
    header("location: email.php?error=password"); 
} 

$name = $_POST['name']; // required 
$email_from = $_POST['email']; // required 
$telephone = $_POST['telephone']; // not required 
$password = $_POST['password']; // required 

$email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
if(!preg_match($email_exp,$email_from)) { 
    header("location: email.php?error=invalid_email"); 
} 
$string_exp = "/^[A-Za-z .'-]+$/"; 
if(!preg_match($string_exp,$name)) { 
    header("location: email.php?error=invalid_firstname"); 
} 
if(strlen($telephone) < 2) { 
    header("location: email.php?error=invalid_telephone"); 
} 
if(strlen($password) < 2) { 
    header("location: email.php?error=invalid_password"); 
} 

в $ _GET [ 'ошибка'] будет содержать идентификатор ошибки, так что в вашем email.php, вы должны поместить сообщения об ошибках.

$error_message = ""; 
if($_GET['error']=='invalid_email'){ 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
} 
if($_GET['error']=='invalid_firstname'){ 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
} 

// и т.д.

if(strlen($error_message) > 0) { 
    echo($error_message); 
} 
+0

Похоже, что у него проблемы с отправкой назад ошибки – user2566655

+0

Я обновил код, состояние в email.php было неправильным, мое плохое. Это должно быть, если ($ _ GET ['error'] == 'invalid_firstname'), тогда вы должны поместить другие сообщения об ошибках. –

+0

Вы уверены, что этот код действителен? \t if (strlen ($ password) <2) { header ("location: ../../../register.php?error=invalid_password"); \t} – user2566655

0

То, что я всегда делаю это получить код ошибки или присвоить текст ошибки на странице валидации переменного сеанса, а затем просто эхо из этого переменного сеанса на странице, где вы хотите, чтобы пользователь увидел переменную. Не забудьте включить session_start(); в начале каждой страницы!

+0

Вы думаете, что можете опубликовать код? – user2566655

+0

Это вроде поздно, извините, но что вы делаете: ** session_start(); $ _SESSION ['errormessage'] = "любое сообщение об ошибке:". $ Error. "Whatever"; ** <- НА ВАШЕЙ СТРАНИЦЕ ОШИБКИ. Затем на другой странице вы хотите: ** session_start(); echo $ _SESSION ['errormessage']; ** – edwardtyl

Смежные вопросы