Jump to content

Header not redirecting when a form is submitted


stevengreen22
Go to solution Solved by ginerjm,

Recommended Posts

Hi all,

 

I'm relatively new to PHP and have spent the past 5 hours trying to figure out what the issue is with this code.  What I'm trying to achieve is sending a message to the user once the email has been sent or not as the case may be.

 

I've tried everything I can think of but still can't solve it.  I've checked for whitespace, there are no obvious errors.  I've also tried putting an echo after the mail function but that doesn't get output either yet the mail gets sent and is received.

 

If there's an alternative way of displaying a message on the same page then that'd be great to know too.  This is the only way I know at the moment.  If there is success send the user to the same page but append the url and then use a get to see if the url was appended and if so present a new message.  That works on a few other pages, just not this one.

 

Any help is appreciated.

 

I've a require at the very top that has includes in, there's no whitespace and error_reporting(E_ALL); ini_set('display_errors', 'On'); doesn't display any errors




			
				


	Edited  by stevengreen22
	
	

			
		
Link to comment
Share on other sites

if(verifyContactFormToken('ourContactForm')){

 

    if(isset($_POST['name'])){    //may have to change to see if a field was set instead

 

 

        $myEmail       = 'stevegreen22@me.com'; //Email address where queries get sent.

        //errors already defined in init

        $name   = strip_tags(trim($_POST['name']));

        $email         = strip_tags(trim($_POST['email']));

        $contactNumber = strip_tags(trim($_POST['contactNumber']));

        $companyName   = strip_tags(trim($_POST['companyName']));

        $message       = strip_tags(trim($_POST['message']));

 

        //url check if relevant

 

        //make sure no urls are in any of the fields





            $errors[]= 'Please do not enter url\'s in any of the fields.';

        }

 

        //check email address!

        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){

            $errors[] = 'Please enter a valid email address.';

        }

 

 

 

        //body of message

        $message1 =$subject = $headers =  - to save space

 

        if(empty($errors) === true){

 

            //mail($myEmail, $subject, $message1, $headers);

            header('Location: contact?sent.php');

            exit();

            //echo 'Your message was sent';

        }

 

    }

 

}else{

 

    if(!isset($_SESSION[$ourContactForm.'_token'])){

 

    }else{

        echo "Hack attempt.  Fail.";

    }

 

}

 

?>

<!doctype html>


<head>

</head>

<?php

//create a new token for checking form authenticity

$newToken = createContactFormTokens('ourContactForm');

?>

<body>

 

<div class="container">

 

  <?php include 'include/header.php'; ?>

  <?php include 'include/sidebar.php'; ?>

 

  <div class="content">

 

     <div id="ourContactForm">

 

<h2>Please use the form below to contact us.</h2>

         <?php

         echo 'yasdfasdfsdaf'; //no output

         if(!empty($errors)){

             echo '<div class="errors">';

             echo '<h3>Errors:</h3>';

             echo '<p>' . implode('</p><p>', $errors) . '</p>';

             echo "</div>";

 

         }else if(isset($_GET['sent'])){?>

             <div class='success'>Thank you for contacting us.</div>

         <?php

         }

         ?>

 

<!-- Using juery plugins to validate instead of html5 - 'required' for example.-->

<form action="" method="post" id="ourContactFormID_JS">

 

             <input type="hidden" name="token" value="<?php echo $newToken; ?>">

 

             <div class="ourContactFormElement">

                 <label for="name"><span class="requiredField">*</span>Your Name:</label>

                 <input type="text" id="name" name="name" class="required" minlength="2" <?php if(isset($_POST['name'])){ echo 'value="', htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8'), '"';}  ?> />

             </div>

 

             <div class="ourContactFormElement">

                 <label for="email"><span class="requiredField">*</span>Your Email:</label>

                 <input type="text" name="email" class="required email" <?php if(isset($_POST['email'])){ echo 'value="', htmlspecialchars($_POST['email'], ENT_QUOTES, 'UTF-8'), '"';}  ?> />

             </div>

             <div class="ourContactFormElement" id="message">

                 <label for="message"><span class="requiredField">*</span>Message:</label>

                 <textarea cols="30" rows="8" name="message" class="required" minlength="2"></textarea>

             </div>

 

             <div class="ourContactFormElement">

                 <label>   </label>

                 <input type="submit" value="Send Request!" />

             </div>

 

</form>

 

</div>

 

 

 <div class="clearfloat"></div>

 

    <!-- end .content --></div>

 

  <?php include 'include/footer.php'; ?>

  <!-- end .container --></div>

</body>
Link to comment
Share on other sites

I don't believe that this code actually runs. Turn on error checking and do some cleanup and you might find what's wrong.

 

What is this line: header('Location: contact?sent.php');

Not a valid url here.

 

What is this line: $message1 =$subject = $headers = - to save space

Certainly a php syntax error is generated.

 

As for checking if $errors is empty - you always set it here:

//make sure no urls are in any of the fields

$errors[]= 'Please do not enter url\'s in any of the fields.';

so it will never be empty.

 

Once you clean it up some maybe we can look at code we know is at least trying to work.

 

And where exactly is the email being sent? Couldn't find that.

Link to comment
Share on other sites

I edited the code to cut a lot out to save the space needed for pasting here.  On the first attempt i used comments and made it all nice, the 2nd attempt had less comments and the 3rd attempt is what you see here.

 

The url is wrong, should have the .php before the ?sent of course.

 

$message1 etc is the body and headers of the email with the remainder removed.

 

$errors is an array thats declared in a file that's always included.  As each error occurs it gets added to the array and then output later.  That again works well in another form I have.

 

The email begin sent was commented out in the above, it does work:)

 

I did figure out what the issue was, I made use of a jquery library to tidy and validate the form, the order in which I called php / js etc was all wrong so it was never getting there.

I then found a mistake in the js file where i was targeting an id to append instead of a class.  With that now working I have no need for the redirect so the code has been cleaned a lot! :)  

 

Thank you for taking the time to read and comment.  Don't suppose you know why the code tags weren't working for me? 

 

Thanks again

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.