php中的Javascript不起作用


Javascript in php not working?

当我运行这个php:时

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'mail@gmail.com';
$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

代码块2中的header()正在调用纯文本。您应该将其设置为Javascript或HTML类型的输出。请改用以下header()调用之一。

如果输出中有javascript AND html:header('Content-Type: text/html');

如果您的输出是javascript ONLY(没有html标记):header('Content-Type: application/javascript');