0

I have got an issue where the PHP header is not working. Here is the full code. Everything is working fine only the page is not redirected at the end.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
    <script src="js/jquery.tabSlideOut.v1.3.js"></script>
    <script>
         $(function(){
             $('.slide-out-div').tabSlideOut({
                 tabHandle: '.handle',                              //class of the element that will be your tab
                 pathToTabImage: '',          //path to the image for the tab (optionaly can be set using css)
                 imageHeight: '273px',                               //height of tab image
                 imageWidth: '63px',                               //width of tab image    
                 tabLocation: 'right',                               //side of screen where tab lives, top, right, bottom, or left
                 speed: 300,                                        //speed of animation
                 action: 'click',                                   //options: 'click' or 'hover', action to trigger animation
                 topPos: '200px',                                   //position from the top
                 fixedPosition: false                               //options: true makes it stick(fixed position) on scroll
             });
         });

         </script>
             <script src="js/form_value.js"></script>
             <script type="text/javascript">
<!--
function validateEmail()
{

   var emailID = document.myForm.email.value;
   atpos = emailID.indexOf("@");
   dotpos = emailID.lastIndexOf(".");
   if (atpos < 1 || ( dotpos - atpos < 2 )) 
   {
       alert("Please enter correct email ID")
       document.myForm.email.focus() ;
       return false;
   }
   return( true );
}

function validate()
{
   if( document.myForm.fname.value == "" )
   {
     alert( "Please provide your First name!" );
     document.myForm.fname.focus() ;
     return false;
   }

    if( document.myForm.lname.value == "" )
   {
     alert( "Please provide your Last name!" );
     document.myForm.lname.focus() ;
     return false;
   }
   if( document.myForm.phone.value == "" ||
           isNaN( document.myForm.phone.value ) ||
           document.myForm.phone.value.length < 8 )
   {
     alert( "Please provide a valid phone number" );
     document.myForm.phone.focus() ;
     return false;
   }
   if( document.myForm.email.value == "" )
   {
     alert( "Please provide a valid Email address" );
     document.myForm.email.focus() ;
     return false;
   }else{
     // Put extra check for data format
     var ret = validateEmail();
     if( ret == false )
     {
          return false;
     }
   }

   if( document.myForm.city.value == "" )
   {
     alert( "Please provide your City" );
     document.myForm.city.focus() ;
     return false;
   }
   return( true );
}
//-->
</script>


<body>

<div id="wrapper">
    <div id="footer">
        <div id="footer_inside">
        <span class="number">******</span>
        <div id="follow">
        <span>Follow us on :</span>

        </div>
        </div>
        </div><!-- #footer -->

    </div><!-- #wrapper -->

<div class="slide-out-div">
        <a class="handle" href="http://link-for-non-js-users">Content</a>
        <h1>Your Contact Information</h1>
    <form method="post" action="thankyou.php" name="myForm"  onsubmit="return(validate());"> 
       <label>First Name </label>
       <input name="fname" class="text" type="text" />
       <label>Last Name </label>
        <input name="lname" class="text" type="text" />
        <label>Phone </label>
        <input name="phone" class="text" type="text" />
        <label>Email </label>
        <input name="email" class="text" type="text"/>
        <label>City </label>
        <input name="city" class="text" type="text"  />

        <input name="submit" class="submit" type="submit" value="" />
       </form>
    </div>
<?php 
error_reporting(E_ALL);
ob_start();
if(isset($_POST['submit'])) {

//include validation class

//assign post data to variables

$title= ""; // storing the phone number    

$fname = trim($_POST['fname']); // Storing username

$lname = trim($_POST['lname']); // Storing username

$phone= trim($_POST['phone']); // storing the phone number

$email = trim($_POST['email']); // Storing email address

$city= trim($_POST['city']); // storing the phone number





if(empty($fname) && empty($phone) && empty($email))

{

echo "All fields are compulsory";

}

$subject = "Contacted by ". $fname;
$subject1 = "Reply From Test2014! ";
$emailTo = '[email protected]'; //Put your own email address here

        $body = "First Name: $fname \n\nLast Name: $lname \n\nPhone:\n $phone \n\nEmail: $email \n\nCity: $city" ;
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
                $headers1 = 'From: Test 2014 <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;

                $body1 ="Dear Sir/Madam, \n\n\n;



        mail($emailTo, $subject, $body, $headers);
                mail($email, $subject1, $body1, $headers1);
        $emailSent = true;
                $conn = mysql_connect("localhost", "root", "****");
                if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("aurevoir_db")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}
mysql_select_db("aurevoir_db", $conn);


$sql = "INSERT INTO `aurevoir_db`.`contact` (`title`, `fname`, `lname`, `phone`, `email`, `city`, `timestamp`) VALUES ('$title', '$fname', '$lname', '$phone', '$email', '$city', NOW())";

mysql_query($sql) or die('Error, insert query failed' . mysql_error());

$url = "thankyou.html";
header('Location: '.$url);
exit();
}

?>
        <?php 

        ?>
</body>
</html>

The emails and DB insertion is going perfect only the page is not redirected.

3
  • 1
    You're pawning out a ton of markup before calling the header() function..... it doesn't work in this circumstance. If you use header() it must be before any other output is sent to the browser, otherwise the default headers will have been sent already Commented Oct 20, 2014 at 7:14
  • 1
    header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. Commented Oct 20, 2014 at 7:17
  • @Touregsys could not exactly got you...since am a newbie...in the above case where should i put the header() Commented Oct 20, 2014 at 7:18

2 Answers 2

1

If you move your PHP code to top of page, it will start working.

<?php 
error_reporting(E_ALL);
ob_start();
if(isset($_POST['submit'])) {

//include validation class

//assign post data to variables

$title= ""; // storing the phone number    

$fname = trim($_POST['fname']); // Storing username

$lname = trim($_POST['lname']); // Storing username

$phone= trim($_POST['phone']); // storing the phone number

$email = trim($_POST['email']); // Storing email address

$city= trim($_POST['city']); // storing the phone number

if(empty($fname) && empty($phone) && empty($email))

{

echo "All fields are compulsory";

}

$subject = "Contacted by ". $fname;
$subject1 = "Reply From Test2014! ";
$emailTo = '[email protected]'; //Put your own email address here

        $body = "First Name: $fname \n\nLast Name: $lname \n\nPhone:\n $phone \n\nEmail: $email \n\nCity: $city" ;
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
                $headers1 = 'From: Test 2014 <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;

                $body1 ="Dear Sir/Madam, \n\n\n;



        mail($emailTo, $subject, $body, $headers);
                mail($email, $subject1, $body1, $headers1);
        $emailSent = true;
                $conn = mysql_connect("localhost", "root", "****");
                if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("aurevoir_db")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}
mysql_select_db("aurevoir_db", $conn);


$sql = "INSERT INTO `aurevoir_db`.`contact` (`title`, `fname`, `lname`, `phone`, `email`, `city`, `timestamp`) VALUES ('$title', '$fname', '$lname', '$phone', '$email', '$city', NOW())";

mysql_query($sql) or die('Error, insert query failed' . mysql_error());

$url = "thankyou.html";
header('Location: '.$url);
exit();
}

?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
    <script src="js/jquery.tabSlideOut.v1.3.js"></script>
    <script>
         $(function(){
             $('.slide-out-div').tabSlideOut({
                 tabHandle: '.handle',                              //class of the element that will be your tab
                 pathToTabImage: '',          //path to the image for the tab (optionaly can be set using css)
                 imageHeight: '273px',                               //height of tab image
                 imageWidth: '63px',                               //width of tab image    
                 tabLocation: 'right',                               //side of screen where tab lives, top, right, bottom, or left
                 speed: 300,                                        //speed of animation
                 action: 'click',                                   //options: 'click' or 'hover', action to trigger animation
                 topPos: '200px',                                   //position from the top
                 fixedPosition: false                               //options: true makes it stick(fixed position) on scroll
             });
         });

         </script>
             <script src="js/form_value.js"></script>
             <script type="text/javascript">
<!--
function validateEmail()
{

   var emailID = document.myForm.email.value;
   atpos = emailID.indexOf("@");
   dotpos = emailID.lastIndexOf(".");
   if (atpos < 1 || ( dotpos - atpos < 2 )) 
   {
       alert("Please enter correct email ID")
       document.myForm.email.focus() ;
       return false;
   }
   return( true );
}

function validate()
{
   if( document.myForm.fname.value == "" )
   {
     alert( "Please provide your First name!" );
     document.myForm.fname.focus() ;
     return false;
   }

    if( document.myForm.lname.value == "" )
   {
     alert( "Please provide your Last name!" );
     document.myForm.lname.focus() ;
     return false;
   }
   if( document.myForm.phone.value == "" ||
           isNaN( document.myForm.phone.value ) ||
           document.myForm.phone.value.length < 8 )
   {
     alert( "Please provide a valid phone number" );
     document.myForm.phone.focus() ;
     return false;
   }
   if( document.myForm.email.value == "" )
   {
     alert( "Please provide a valid Email address" );
     document.myForm.email.focus() ;
     return false;
   }else{
     // Put extra check for data format
     var ret = validateEmail();
     if( ret == false )
     {
          return false;
     }
   }

   if( document.myForm.city.value == "" )
   {
     alert( "Please provide your City" );
     document.myForm.city.focus() ;
     return false;
   }
   return( true );
}
//-->
</script>


<body>

<div id="wrapper">
    <div id="footer">
        <div id="footer_inside">
        <span class="number">******</span>
        <div id="follow">
        <span>Follow us on :</span>

        </div>
        </div>
        </div><!-- #footer -->

    </div><!-- #wrapper -->

<div class="slide-out-div">
        <a class="handle" href="http://link-for-non-js-users">Content</a>
        <h1>Your Contact Information</h1>
    <form method="post" action="thankyou.php" name="myForm"  onsubmit="return(validate());"> 
       <label>First Name </label>
       <input name="fname" class="text" type="text" />
       <label>Last Name </label>
        <input name="lname" class="text" type="text" />
        <label>Phone </label>
        <input name="phone" class="text" type="text" />
        <label>Email </label>
        <input name="email" class="text" type="text"/>
        <label>City </label>
        <input name="city" class="text" type="text"  />

        <input name="submit" class="submit" type="submit" value="" />
       </form>
    </div>        
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

header() function must be called before any output. But you can trick with ob_ functions. Please change a bit your script:

<?php
ob_start(); // start buffering, nothing will be output
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
    <script src="js/jquery.tabSlideOut.v1.3.js"></script>
    <script>
...
<?php 
error_reporting(E_ALL);
// ob_start(); // comment out or delete this line
...
$url = "thankyou.html";
header('Location: '.$url);
// exit(); // comment out or delete this line
}

?>
</body>
</html>
<?php 
ob_end_flush(); // start to output, headers must be placed correctly now
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.