2014-08-30 20 views
-5

當我運行這個PHP:的Javascript在PHP現在工作

<?php 
$field_name = $_POST['cf_name']; 
$field_email = $_POST['cf_email']; 
$field_message = $_POST['cf_message']; 

$mail_to = '[email protected]'; 
$subject = 'Nova SFI poruka '.$field_name; 

$body_message = 'From: '.$field_name."\n"; 
$body_message .= 'E-mail: '.$field_email."\n"; 
$body_message .= 'Message: '.$field_message; 

$headers = 'From: '.$field_email."\r\n"; 
$headers .= 'Reply-To: '.$field_email."\r\n"; 

$mail_status = mail($mail_to, $subject, $body_message, $headers); 

if ($mail_status) { ?> 
    <script language="javascript" type="text/javascript"> 
     alert('Message'); 
     window.location = 'contact.html'; 
    </script> 
<?php 
} 
else { ?> 
    <script language="javascript" type="text/javascript"> 
     alert('Message'); 
     window.location = 'contact.html'; 
    </script> 
<?php 
} 
?> 

的Javascript做工精細,警報彈出,並且它可以傳送我想要的網頁... 但是當我使用這個PHP替代:

<?php 

header('Content-Type: text/plain; charset=utf-8'); 

if(isset($_POST['submit'])){ 
if(isset($_POST['answer']) && $_POST['answer'] == 4){ 


$field_name = $_POST['name']; 
$field_lastname = $_POST['lastname']; 
$field_email = $_POST['email']; 
$field_rank = $_POST['rank']; 

$mail_to = 'email'; 
$subject = 'Rank confirmation '.$field_name; 

$body_message = 'From: '.$field_name."\n".$field_lastname."\n".$field_rank."\n"; 
$body_message .= 'E-mail: '.$field_email."\n"; 

$headers = 'From: '.$field_email."\r\n"; 
$headers .= 'Reply-To: '.$field_email."\r\n"; 

$mail_status = mail($mail_to, $subject, $body_message, $headers); 


try { 

    // Undefined | Multiple Files | $_FILES Corruption Attack 
    // If this request falls under any of them, treat it invalid. 
    if (
     !isset($_FILES['file']['error']) || 
     is_array($_FILES['file']['error']) 
    ) { 
     throw new RuntimeException('Invalid parameters.'); 
    } 

    // Check $_FILES['file']['error'] value. 
    switch ($_FILES['file']['error']) { 
     case UPLOAD_ERR_OK: 
      break; 
     case UPLOAD_ERR_NO_FILE: 
      throw new RuntimeException('No file sent.'); 
     case UPLOAD_ERR_INI_SIZE: 
     case UPLOAD_ERR_FORM_SIZE: 
      throw new RuntimeException('Exceeded filesize limit.'); 
     default: 
      throw new RuntimeException('Unknown errors.'); 
    } 

    // You should also check filesize here. 
    if ($_FILES['file']['size'] > 1000000) { 
     throw new RuntimeException('Exceeded filesize limit.'); 
    } 

    // DO NOT TRUST $_FILES['file']['mime'] VALUE !! 
    // Check MIME Type by yourself. 
    $finfo = new finfo(FILEINFO_MIME_TYPE); 
    if (false === $ext = array_search(
     $finfo->file($_FILES['file']['tmp_name']), 
     array(
      'jpg' => 'image/jpeg', 
      'png' => 'image/png', 
      'gif' => 'image/gif', 
     ), 
     true 
    )) { 
     throw new RuntimeException('Invalid file format.'); 
    } 

    // You should name it uniquely. 
    // DO NOT USE $_FILES['file']['name'] WITHOUT ANY VALIDATION !! 
    // On this example, obtain safe unique name from its binary data. 
    if (!move_uploaded_file(
     $_FILES['file']['tmp_name'], 
     sprintf('./uploads/%s.%s', 
      sha1_file($_FILES['file']['tmp_name']), 
      $ext 
     ) 
    )) { 
     throw new RuntimeException('Failed to move uploaded file.'); 
    } 
?> 
    <script language="javascript" type="text/javascript"> 
     alert('Confirmed! We will check it in maximum 24 hours!\\nPotvrdjeno! Provericemo za maksimum 24 casa!'); 
     window.location = 'index.html'; 
    </script> 
    <?php 

} catch (RuntimeException $e) { 

    echo $e->getMessage(); 

} 

}else{ ?> 
    <script language="javascript" type="text/javascript"> 
     alert('Security answer incorect! Please try again!\\nBezbednosni odgovor netacan! Pokusajte ponovo!'); 
     window.location = 'index.html'; 
    </script> 
    <?php 
} 
} 



?> 

它只是不工作,我不知道我做錯了什麼。 約亂碼

時它的工作原理很抱歉: http://prntscr.com/4hudk9 當它不: http://prntscr.com/4hud98

+3

一般 「不工作」 立即通過我獲得downvotes - 這是最糟糕的錯誤描述。您可能需要先調試您的代碼並嘗試找出哪些代碼無效。 – 2014-08-30 01:30:58

+0

這是btw可怕的代碼和可怕的架構!花時間,使用樣板,框架等,並清楚地分割你的JS,你的PHP和你的前端標記。稍後你會感謝你自己:) – Sliq 2014-08-30 01:34:48

+0

「Javascript in php」的帖子名已被使用。 我從來沒有學過JS或PHP,它只是邏輯,如果它工作,我很自豪,我只知道HTML。 – 2014-08-30 01:45:28

回答

0

header()在代碼塊2呼喚純文本。您應該將其設置爲Javascript或HTML類型的輸出。改用下列header()調用之一。

如果您在輸出JavaScript和HTML: header('Content-Type: text/html');

如果你的輸出僅JavaScript(不包括HTML標記): header('Content-Type: application/javascript');

+0

工程就像一個魅力,謝謝,現在我知道更多,再次感謝。 – 2014-08-30 01:44:45