简体   繁体   English

通过 php 提交表单重定向到另一个页面

[英]On submitting form redirects to another page through php

I have coded a form in which I am using if statement so when someone click on send button this if statement will redirects to "thanks.php" page from "career-a.php" page.我编写了一个使用 if 语句的表单,因此当有人单击发送按钮时,此 if 语句将从“career-a.php”页面重定向到“thanks.php”页面。 In thanks.php page I have simply added bootstrap column with thanks message.在Thanks.php 页面中,我只是添加了带有感谢消息的引导列。 But this code is not directing to other page.但是这段代码并不指向其他页面。 Can you tell me how should I solve this issue?你能告诉我应该如何解决这个问题吗? Code on top of thanks.php:感谢上面的代码。php:

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

Code for career-a.php (I have added php and html on same page so SUBMIT button is on this same page). 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{
   
}
}
   ?>

You have bunch of syntax errors in your code and that is why it does not work.您的代码中有一堆语法错误,这就是它不起作用的原因。 It seems like you used different sources of information on how you send mails via PHP.您似乎使用了不同的信息来源来了解如何通过 PHP 发送邮件。

Personally I recommend to use library like PHPMailer , but if you want to keep your dependencies at minimum you can adapt your code based on this answer我个人建议使用PHPMailer 之类的库,但是如果您想将依赖项保持在最低限度,您可以根据此答案调整您的代码

So it will look something like this:所以它看起来像这样:

<?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}");

Please note that this code is not tested!请注意,此代码未经测试! Its purpose is to give you directions on how to continue其目的是为您指明如何继续

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM