php标头没有标头功能不起作用

时间:2014-10-20 07:12:51

标签: javascript php

我遇到了PHP标头无效的问题。这是完整的代码。一切正常,只有页面最终没有被重定向。

<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 = 'test@test.com'; //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>

电子邮件和数据库插入非常完美,只有页面不会被重定向。

2 个答案:

答案 0 :(得分:1)

如果您将PHP代码移到页面顶部,它将开始工作。

<?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 = 'test@test.com'; //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>

答案 1 :(得分:0)

必须在任何输出之前调用

header()函数。但你可以使用ob_函数进行欺骗。请改变你的脚本:

<?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
?>