我不想让我的contact.php将我重定向到一个空白站点,我想在不上网的情况下显示一条消息

我不想让我的contact.php将我重定向到一个空白站点,我想在不上网的情况下显示一条消息,php,contact-form,Php,Contact Form,我有一个contact.php表单,它工作得非常好,但是我对这个表单有一个问题,像警报等消息都显示在contact.php文件中,我希望当我点击“发送”按钮时,php会在不离开网站的情况下插入一个eccho来说出消息 这是我的联系人 <?php if(isset($_POST['email'])) { $email_to = "natestale@gmail.com"; $email_subject = "Nuevo contacto de Sitio web";

我有一个contact.php表单,它工作得非常好,但是我对这个表单有一个问题,像警报等消息都显示在contact.php文件中,我希望当我点击“发送”按钮时,php会在不离开网站的情况下插入一个eccho来说出消息

这是我的联系人

    <?php
if(isset($_POST['email'])) {
    $email_to = "natestale@gmail.com";
    $email_subject = "Nuevo contacto de Sitio web";
    function died($error) {
        // Mensaje de error
        echo "Disculpas, pero ha ocurrido un(os) error(es) con lo datos del formulario. ";
        echo "El o los errores son los siguientes.<br /><br />";
        echo $error."<br /><br />";
        echo "Por favor corrija los errores.<br /><br />";
        die();
    }
    // validacion de campos
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['message'])) {
        died('Disculpas, pero ha ocurrido un(os) error(es) al enviar el formulario.');       
    }
    $name = $_POST['name']; // obligatorio
    $email = $_POST['email']; // obligatorio
    $website = $_POST['website']; // no obligatorio
    $prioritycase = $_POST['prioritycase']; // no obligatorio
    $asunto = $_POST['asunto']; // no obligatorio
    $message = $_POST['message']; // obligatorio
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email)) {
    $error_message .= 'La direccion de E-mail es incorrecta.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$name)) {
    $error_message .= 'EL nombre no es un nombre Valido.<br />';
  }
  if(!preg_match($string_exp,$website)) {
    $error_message .= 'La url no es valida.<br />';
  }
  if(strlen($message) < 2) {
    $error_message .= 'El comentario no parece estar completo.<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 .= "Nombre: ".clean_string($name)."\n";
    $email_message .= "Website: ".clean_string($website)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Mensaje: ".clean_string($message)."\n";
// estructura del correo
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
<meta http-equiv="refresh" content="3;URL=http://www.redpandastudio.tk" />
<link rel="stylesheet" type="text/css" href="dist/css/contacto.css" />
<!-- incluimos nuestro mensaje de agradecimiento -->

<?php
}
?>

这是我的网站和联系方式


简单的解决方案是将contact.php文件中的验证放入表单本身所在的文件中。按submit时,将其发布到同一文件。此外,在验证逻辑中添加以下内容:

//查看是否按下了submit btn,这意味着有人点击了按钮

if(isset($_POST["submit"])){
.... 
}

另一个选项是使用$SESSION。如何使用会话进行验证的说明非常好。

如果您不希望表单实际提交,请使用AJAX,否则,您可以提交到同一页面并传递
错误
参数,在页面中,第一件事-检查是否设置了参数错误-显示错误。如何使用AJAX提交?谷歌:“提交表单AJAX”你会发现很多例子