2016-08-18 74 views
-2

提交表單後得到的郵件,但它去消息發送消息白黑屏。PHP提交表單後提交空白頁

我嘗試在提交表單後重新加載頁面,但出現錯誤。

<?php 
if ($_SERVER['REQUEST_METHOD'] == "POST") { 

    $to = "[email protected]"; 
    $subject = "E-mail with attachment"; 
    $from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">"; 

    // generate a random string to be used as the boundary marker 
    $mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x"; 

    // now we'll build the message headers 
    $headers = "From: $from\r\n" . 
     "MIME-Version: 1.0\r\n" . 
     "Content-Type: multipart/mixed;\r\n" . 
     " boundary=\"{$mime_boundary}\""; 

    $message = "Canditade Resume"; 
    // when we use it 
    $message = "This is a multi-part message in MIME format.\n\n" . 
     "--{$mime_boundary}\n" . 
     "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
     "Content-Transfer-Encoding: 7bit\n\n" . 
     $message . "\n\n"; 

    // iterating each File type 
    print_r($_FILES); 

    foreach ($_FILES as $userfile) { 

    $tmp_name = $userfile['tmp_name']; 
    $type = $userfile['type']; 
    $name = $userfile['name']; 
    $size = $userfile['size']; 


    if (file_exists($tmp_name)) { 
     if (is_uploaded_file($tmp_name)) { 
     $file = fopen($tmp_name, 'rb'); 
     $data = fread($file, filesize($tmp_name)); 
     fclose($file); 
     $data = chunk_split(base64_encode($data)); 
     } 
     $message .= "--{$mime_boundary}\n" . 
      "Content-Type: {$type};\n" . 
      " name=\"{$name}\"\n" . 
      "Content-Disposition: attachment;\n" . 
      " filename=\"{$tmp_name}\"\n" . 
      "Content-Transfer-Encoding: base64\n\n" . 
      $data . "\n\n"; 
    } 
    } 
    // here's our closing mime boundary that indicates the last of the message 
    $message.="--{$mime_boundary}--\n"; 
    // now we just send the message 
    if (@mail($to, $subject, $message, $headers)) 
    echo "Message Sent"; 
    else 
    echo "Failed to send"; 
} else { 
    ?> 
    <form action="index.php" method="post" enctype="multipart/form-data" name="form1"> 
     <label>Name</label> 
     <input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name"> 
     <label>Email Address</label> 
     <input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail"> 
     <label>Designation</label> 
     <input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation"> 
     <label>Upload Your CV</label> 
     <input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1"> 
     </div> 
     <input type="submit" name="Submit" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';"> 
    </form> 
<?php } ?> 

請幫我

+0

你想達到什麼目的?什麼是你沒有預料到的錯誤? – Cagy79

+0

表單工作正常,提交後我想驗證表單,然後想要重新回到其他頁面 –

+0

請用您現在的代碼更新您問題中的代碼。 – Cagy79

回答

0

給頁面名稱header location到要重定向它。

變化

if (@mail($to, $subject, $message, $headers)) 
    echo "Message Sent"; 
    else 
    echo "Failed to send"; 
} else { 

if (@mail($to, $subject, $message, $headers)) 
    header("location:yourpage.php?message=Message Sent"); 
    else 
    header("location:yourpage.php?message=Failed to send"); 
} else { 

yourpage.php

<?php 
if(isset($_GET['message'])){ 
    echo $_GET['message']; 
} 
. 
. 

?> 
0
if (@mail($to, $subject, $message, $headers)) 
    //echo "Message Sent"; 
    header('location:email-success.php'); 
    else 
    //echo "Failed to send"; 
    header('location:email-error.php'); 
} 
+0

仍然出現這個錯誤 –

+0

Array([file1] => Array([name] => sendemail.php [type] => application/octet-stream [tmp_name] =>/tmp/phphNCMKH [error] => 0 [size ] => 801)) –

+0

這不是一個錯誤,那是你的print_r語句'print_r($ _ FILES);'!刪除該行,它將工作。 – Cagy79

0

你的編碼是有點亂。以下是我所做的調整:

我還註釋掉了print_r($ _ FILES),因爲這會顯示當用戶按下提交時不想顯示的內容的數組。

所以我改名爲$ _ POST,並使之成爲isset:

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

    $to = "[email protected]"; 
    $subject = "E-mail with attachment"; 
    $from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">"; 

    // generate a random string to be used as the boundary marker 
    $mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x"; 

    // now we'll build the message headers 
    $headers = "From: $from\r\n" . 
     "MIME-Version: 1.0\r\n" . 
     "Content-Type: multipart/mixed;\r\n" . 
     " boundary=\"{$mime_boundary}\""; 

    $message = "Canditade Resume"; 
    // when we use it 
    $message = "This is a multi-part message in MIME format.\n\n" . 
     "--{$mime_boundary}\n" . 
     "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
     "Content-Transfer-Encoding: 7bit\n\n" . 
     $message . "\n\n"; 

    // iterating each File type 
    //print_r($_FILES); 

    foreach ($_FILES as $userfile) { 

     $tmp_name = $userfile['tmp_name']; 
     $type = $userfile['type']; 
     $name = $userfile['name']; 
     $size = $userfile['size']; 


     if (file_exists($tmp_name)) { 
      if (is_uploaded_file($tmp_name)) { 
       $file = fopen($tmp_name, 'rb'); 
       $data = fread($file, filesize($tmp_name)); 
       fclose($file); 
       $data = chunk_split(base64_encode($data)); 
      } 
      $message .= "--{$mime_boundary}\n" . 
       "Content-Type: {$type};\n" . 
       " name=\"{$name}\"\n" . 
       "Content-Disposition: attachment;\n" . 
       " filename=\"{$tmp_name}\"\n" . 
       "Content-Transfer-Encoding: base64\n\n" . 
       $data . "\n\n"; 
     } 
    } 
    // here's our closing mime boundary that indicates the last of the message 
    $message .= "--{$mime_boundary}--\n"; 
    // now we just send the message 
    if (@mail($to, $subject, $message, $headers)) { 
     echo "Message Sent"; 
    } else { 
     echo "Failed to send"; 
    } 
} 
    ?> 

不知道爲什麼你纏在HTML端的PHP,但是這將是你爲什麼沒有被帶回形式側只是一個空白頁。

<form action="" method="post" enctype="multipart/form-data" name="form1"> 
     <label>Name</label> 
     <input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name"> 
     <label>Email Address</label> 
     <input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail"> 
     <label>Designation</label> 
     <input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation"> 
     <label>Upload Your CV</label> 
     <input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1"> 
     </div> 
     <input type="submit" name="send" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';"> 
    </form> 

我已經測試這個代碼,並將其重定向回形式與形式上面的「發送消息」(如你的任何接觸形式上看到)。

+0

你真的應該添加條件給每個字段if(!empty($ var)){// do something}',因爲表單必填字段是輕鬆繞過。 – Option

+0

感謝您的效果,現在它的重新發送但附件文件沒有收到郵件 –