2017-02-23 112 views
0

我的页面不保留在同一页面我在这里有点新。我试图自己创建我的网站,虽然已经快完成了,但我无法解决以我的联系表单发送信息的问题。当我点击选项提交

页是这样的:ramproducciones.pe(这是在西班牙) 如果他们对他们发现的接触形式走到底,即使他们正确填写数据时,发送电子邮件,但接下来的事情是一个空白给出页面。我想要的是你收到一条消息说,谢谢你!您的消息已发送,但我不知道如何操作。我试图复制许多代码和我目前有此:

<!-- contacto--> 

    <section class="contact" id="contact"> 

     <h1>Contacto</h1> 
     <hr/>  

     <div class="content"> 

      <div class="form"> 

      <form method="post" action="mail.php" name="contact"> 

      <div class="column">NOMBRE<br/><br/> 
      <input name="name" id="name" value="" /> 
      </div> 

      <div class="column-2">E-MAIL<br/><br/> 
      <input name="email" id="email" value="" /> 
      </div> 


      <div class="column-3">MENSAJE<br/><br/>   
      <textarea id="message" name="message" ></textarea> 
      </div> 

       <div class="button"> 
       <span><input class="submit" id="submit" name="submit" type="submit" value="ENVIAR"></span> 
       </div> 

       <div id="dialog-message" title="Thank You"> 
       <p>¡Gracias! Tu mensaje ha sido enviado, te responderemos lo antes posible.</p> 
       </div> 

      </form> 

      </div> 


      <div class="contact-text"> 

      No dudes en solicitarnos información.<br/><br/> 


      <strong>RAM Producciones</strong><br/><br/> 

      e-mail: <strong>[email protected]</strong> 

      <br/><br/> 

      <a href="https://facebook.com/RAMproduccionesSAC" target="_blank"><img src=img/social/Facebook.png /></a> 
      <a href="https://vimeo.com/ramproduccionessac" target="_blank"><img src=img/social/Vimeo.png /></a> 
      <a href="https://www.behance.net/RAMProduccionesSAC" target="_blank"><img src=img/social/Behance.png /></a> 
      <a href="https://www.instagram.com/ram.producciones" target="_blank"><img src=img/social/Instagram.png /></a> 
      <a href="https://www.youtube.com/channel/UCIF9shcqE4D5cF5Xp7LBasg" target="_blank"><img src=img/social/Youtube.png /></a> 

      </div> 

     </div> 

    </section> 

的,而PHP是

<?php 

// define variables and set to empty values 
$name_error = $email_error = ""; 
$name = $email = $message = $success = ""; 

//form is submitted with POST method 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["name"])) { 
    $name_error = "Name is required"; 
    } else { 
    $name = test_input($_POST["name"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) { 
     $name_error = "Solo ingresa letras y espacios."; 
    } 
    } 

    if (empty($_POST["email"])) { 
    $email_error = "Tu E-mail es necesario"; 
    } else { 
    $email = test_input($_POST["email"]); 
    // check if e-mail address is well-formed 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     $email_error = "Formato de E-mail inválido"; 
    } 
    } 

    if (empty($_POST["message"])) { 
    $message = ""; 
    } else { 
    $message = test_input($_POST["message"]); 
    } 

    if ($name_error == '' and $email_error == '' and $phone_error == '' and $url_error == ''){ 
     $message_body = ''; 
     unset($_POST['submit']); 
     foreach ($_POST as $key => $value){ 
      $message_body .= "$key: $value\n"; 
     } 

     $to = '[email protected]'; 
     $subject = 'Mail vía RAMPRODUCCIONES.PE'; 
     if (mail($to, $subject, $message)){ 
      $success = "¡Gracias! Tu mensaje ha sido enviado, te responderemos lo antes posible."; 
      $name = $email = $message = ''; 
     } 
    } 

} 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

不过我还是解决不了问题。

+0

这是你的行为。 – clearshot66

+0

它去mail.php文件。 –

+0

当表单被提交时,您可以将它们发送到另一个页面,在您的表单标签中带有这个'action =“mail.php”'。另外,值得注意的是'test_input()'函数大多是无用的,只是破坏了数据而不是提供“安全性”。 – Qirel

回答

0

这是你的行为。您正在将您的页面发送到另一个名为mail.php的页面。提交表单时,设置操作以重新加载当前页面。

尝试使用

<form id='form' method='post' action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>'>

在您的形式

在网页的顶部的属性,你运行你的成功的表单提交代码,然后隐藏的形式显示您的消息。

echo "<style>#form{display:none}</style>"; 
    echo "<p>Thank you for submitting</p>"; 
+0

'PHP_SELF'非常不安全,您应该首先使用'htmlspecialchars()'删除html。 – Soaku

0

您的情况有很多可能性。 Clearshot66推出了一款。

另一种可能性是保持你的mail.php页面,发送电子邮件后,你可以创建一个逻辑,将用户发送回你的主页面并向他显示一条消息。可以这样做,因为我告诉你如下:

mail.php

header("Location: your_main_page.php?showMessage=1"); 
exit; 

your_main_page.php

<?php 
    if ($_GET["showMessage"] == 1) { 
     echo "Thanks for your contact."; 
    } 
?> 

上面的代码应该把你的main_page.php的beggining

这是另一个IDEA。

最好的做法是使用一些ajax请求发送表单并处理该请求的返回,但这比我们指出的选项稍微复杂一些。

0

正如@ clearshot66所说,你正在重定向到mail.php。我明白,你的意思是你的php页面是这个文件。

什么弗雷迪-ii-锯,你忘了回声$success

空白页发生,因为没有输出。

希望我帮了忙。