2017-02-23 107 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

空白頁發生,因爲沒有輸出。

希望我幫了忙。