首页 > 解决方案 > 在提交表单时通过 php 重定向到另一个页面

问题描述

我编写了一个使用 if 语句的表单,因此当有人单击发送按钮时,此 if 语句将从“career-a.php”页面重定向到“thanks.php”页面。在thanks.php 页面中,我简单地添加了带有感谢信息的引导列。但是这段代码并不指向其他页面。你能告诉我应该如何解决这个问题吗?感谢.php之上的代码:

<?php
require "header.php";
session_start();
$message=$_SESSION['msg'];
echo $message;
?>

career-a.php 的代码(我在同一页面上添加了 php 和 html,因此提交按钮位于同一页面上)。

<?php
$page = 'careers';
require "header.php";
?>

<?php
$statusMsg='';
if(isset($_FILES["file"]["name"])){
   $email = $_POST['email'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $phone = $_POST['phone'];
    $option = $_POST['option'];
    $message = $_POST['message'];
    
    if(!empty($_POST['check_list'])) {
    $checks = array();
    foreach($_POST['check_list'] as $check) {
        $checks[] = $check;
    }
    $check = implode('</br>&bull;', $checks);
}
$fromemail =  $email;
$subject="Tell Us About Yourself to MyGeo";
$email_message = '<h2>User Information</h2>
                    <p><b>First Name:</b> '.$fname.'</p>
                    <p><b>Last Name:</b> '.$lname.'</p>
                    <p><b>Email:</b> '.$email.'</p>
                    <p><b>Phone:</b> '.$phone.'</p>
                    <p><b>Interest :</br></b> &bull;'.$check .'</p>
                    <p><b>Field :</b> '.$option.'</p>
                    <p><b>Introduce Yourself :</b> '.$message.'</p>';
$email_message.="Please find the attachment";
$semi_rand = md5(uniqid(time()));
$headers = "From: ".$fromemail;
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

if($_FILES["file"]["name"]!= ""){  
    $strFilesName = $_FILES["file"]["name"];  
    $strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));  
    
    
    $email_message .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $email_message .= "\n\n";


    $email_message .= "--{$mime_boundary}\n" .
    "Content-Type: application/octet-stream;\n" .
    " name=\"{$strFilesName}\"\n" .
    //"Content-Disposition: attachment;\n" .
    //" filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $strContent  .= "\n\n" .
    "--{$mime_boundary}--\n";
}
$toemail="mail@mail.com"; 

if(mail($toemail, $subject, $email_message, $headers)){
    session_start();
    $_SESSION['msg'] = $statusMsg;
   header("Location: thanks.php?mailsend");
}else{
   
}
}
   ?>

标签: phphtmlforms

解决方案


您的代码中有一堆语法错误,这就是它不起作用的原因。您似乎使用了不同的信息来源来了解如何通过 PHP 发送邮件。

我个人建议使用PHPMailer 之类的库,但如果您想将依赖项保持在最低限度,您可以根据此答案调整您的代码

所以它看起来像这样:

<?php

session_start();

// Best not to use $_POST directly
$input = filter_input_array(\INPUT_POST, [
    'email' => FILTER_VALIDATE_EMAIL,
    'fname' => FILTER_DEFAULT,
    'lname' => FILTER_DEFAULT,
    'phone' => FILTER_DEFAULT,
    'option' => FILTER_DEFAULT,
    'message' => FILTER_DEFAULT,
    'check_list' => [
        'filter' => FILTER_DEFAULT,
        'flags'  => FILTER_REQUIRE_ARRAY,
    ],
]);

$to="mail@mail.com";
$subject="Tell Us About Yourself to MyGeo";

// a random hash will be necessary to send mixed content
$separator = md5(time());

// carriage return type (RFC)
$eol = "\r\n";

$attachment = null;
$attachmentName = null;
if (isset($_FILES["file"]["tmp_name"])) {
    $attachmentName = $_FILES["file"]["name"];  
    $attachment = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
}

// main header (multipart mandatory)
$headers = "From: {$input['fname']} <{$input['email']}>{$eol}";
$headers .= "MIME-Version: 1.0{$eol}";
$headers .= "Content-Type: multipart/mixed; boundary=\"mixed-{$separator}\"";

ob_start();
?>
<h2>User Information</h2>
<p><b>First Name:</b> <?php echo $input['fname']; ?></p>
<p><b>Last Name:</b> <?php echo $input['lname']; ?></p>
<p><b>Email:</b> <?php echo $input['email']; ?></p>
<p><b>Phone:</b> <?php echo $input['phone']; ?></p>
<p><b>Interest:</br></b> &bull; <?php echo implode('</br>&bull;', $input['check_list']); ?></p>
<p><b>Field:</b> <?php echo $input['option']; ?></p>
<p><b>Introduce Yourself:</b> <?php echo $input['message']; ?></p>
<?php
$html = ob_get_clean();

ob_start();
?>
--mixed-<?php echo $separator; ?>  
Content-Type: multipart/alternative; boundary="alternative-<?php echo $separator; ?>" 

--alternative-<?php echo $separator; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<?php echo strip_tags($html); ?>

--alternative-<?php echo $separator; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<?php echo $html; ?>

--alternative-<?php echo $separator; ?>-- 
<?php if ($attachment) : ?>
--mixed-<?php echo $separator; ?>  
Content-Type: application/octet-stream; name="<?php echo $attachmentName; ?>"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
<?php endif; ?>
--mixed-<?php echo $separator; ?>-- 
<?php
$message = ob_get_clean();


if (mail($to, $subject, $message, $headers)) {
    $_SESSION['msg'] = 'Thank you for showing your interest';
    $location = 'thanks.php?mailsend';
} else {
    $_SESSION['msg'] = 'Mail function failed!';
    $location = 'thanks.php?mailfailed';
}
header("Location: {$location}");

请注意,此代码未经测试!其目的是为您指明如何继续


推荐阅读