PHP重定向无法在联系表单上工作(消息正在发送)

时间:2016-12-10 15:21:27

标签: php redirect contact-form

我的联系表单正在运行,但不会将我重定向到我的感谢页面。在我将它转移到新主机之前,它正在工作。

<!-- Contact PHP Stuff -->
<?php

session_start();

require_once 'libs/phpmailer/PHPMailerAutoload.php';

$errors = [];

if(isset($_POST['name'], $_POST['email'], $_POST['subject'], $_POST['message'])) {

    $fields = [
        'name'=> $_POST['name'],
        'email'=> $_POST['email'],
        'subject'=> $_POST['subject'],
        'message'=> $_POST['message']
    ];

    foreach($fields as $field => $data) {
        if(empty($data)) {
            $errors[] = 'The ' . $field . ' field is required'; 
        }
    }
    if(!empty($fields['name'])) {
        if (!preg_match("/^[a-zA-Z ]*$/",$fields['name'])) {
            $errors[] = "Name can only include letters A-Z";
        }
    }
    if(!empty($fields['email'])) {
        if (!filter_var($fields['email'], FILTER_VALIDATE_EMAIL)) {
            $errors[] = "Please enter a valid email address";
        }
    }

    if(empty($errors)) {

        $mail = new PHPMailer(); // create a new object
        $mail->IsHTML(true);
        $mail->Username = "contact@dhmltd.co.uk";
        $mail->Password = "*****";
        $mail->SetFrom($fields['email'], $fields['name']);
        $mail->Subject = $fields['subject'];
        $mail->Body = $fields['message'];
        $mail->AddAddress("admin@dhmltd.co.uk");

        if($mail->Send()) {
            header('Location: thanks');
            die();
        } else {
            $errors[] = "Sorry could not send message. Try again later";
        }

    }

} else {
    $errors[] = 'Something went wrong'; 
}

$_SESSION['errors'] = $errors;
$_SESSION['thanks'] = $thanks;
$_SESSION['fields'] = $fields;

header('Location: contact#contact-form');

?>

header('Location: thanks');正在将我重定向到我以前的主机上的thank.html,但现在即使正在发送和发送消息,也不会发生这种情况。

我在教程中找到了代码,并且记不起它在一年前的位置。

1 个答案:

答案 0 :(得分:0)

在使用标题之前删除所有内容。 在使用header()

之前,您无法获得任何输出

如果你添加

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace maze_3
{//open namespace
    class Room
    {//open class room

        private string RoomName;
        private Room N = null; // so that when i set the links they are automatically null unless i specify in main 
        private Room E = null;
        private Room S = null;
        private Room W = null;

        public Room X { get; private set; } // the software implemented this 

        public void setName(string N) // so that i am able to set the name of the room
        {
            RoomName = N;
        }

        public void setN(Room X) // sets the north direction between the objects of the rooms
        {
            N = X;
        }

        public void setE(Room X)
        {
            E = X;
        }

        public void setW(Room X)
        {
            W = X;
        }

        public void setS(Room x)
        {
            S = X;
        }   

        public Room getN() // want to get a direction from the user, in this case they would input a north direction 
        {
            return N;
        }

        public Room getE()
        {
            return E;
        }

        public Room getS()
        {
            return S;
        }

        public Room getW()
        {
            return W;
        }      

    static void Main(string[] args)// it is asking for a ; here but also says that I should declare it as extern, partial etc

    class chamber// its telling me that a ; is expected ???
    {//open class chamber

        chamber gh = new chamber();// my objects that are the rooms in the haunted house. 
        chamber kit = new chamber();
        chamber Pan = new chamber();
        chamber Dun = new chamber();
        chamber dr = new chamber();
        chamber lib = new chamber();
        chamber din = new chamber();
        chamber sr = new chamber();
        chamber weap = new chamber();
        chamber tow = new chamber();
        chamber gal = new chamber();
        chamber tr = new chamber();

        gh.RoomName("Great Hall"); //to set the object name as the Great Hall, all get and set links were available in class
            gh.setW(dr); ///I want to set a west direction between my object gh to dr
            gh.SetS(kit);// to set a south link between my two objects 

            dr.setName("Drawing Room");//states that all my setlinks are not valid in the current context- this is for all
            dr.setS(lib); //it states the ; is not a valid token in class, struct or interface

            kit.setName("Kitchen");// it states that my objects e.g kit is not valid in this current context-this is for all
            kit.setS(pan);

            pan.setName("Pantry");
            pan.SetE(dun); /// this is a dead end in the game 

            lib.setName("Library ");
            lib.setE(din);

            din.setName("Dining Room");
            din.setN(sr);
            din.setE(gal);
            din.setS(weap); //dead end in the game

            sr.setName("Smoking Room");
            sr.setE(tow);//dead end

            gal.setName("Treasure Room");
            gal.setS(tr)    

            /// </summary> so i wanted to have the actual game play to follow a linked list with a if else structure. 
            int finish = 0;
        string choice;    
        Room current;

        current=gh; 
            while (finish !=1) (finish ==0) //im confused as to which condition is best to use. 
            {Console.WriteLine("You are in room " + current.getRoomname() + "and you can move ");
           if( current.getN() != null)
           Console.WriteLine("North (N) ");
          if( current.getE() != null)
           Console.WriteLine("East (E) ");

         Console.WriteLine("Please enter the direction you wish to go in ");
         string choice = Console.ReadLine();
         if (choice[0] == 'E') // or alternative means of getting in a choice from a string
        current = current.getE();// to be able to move to next 'current'room

            // i was going to do this for all the options. 

        if(current == tr // ie last room
            exit = 1;
        Console.Writeline ("Well done you have found the treasure room");              
        }//close chamber

        }//close class rooom

    } //close namespace

到您可能会看到“标题已发送”错误的页面。

或者你可以使用exit()显然修复了它。