0
我有一個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 = '[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>
電子郵件和數據庫插入正在進行,只有頁面沒有重定向。
您正在吐出一大堆標記___在調用header()函數之前......它在這種情況下不起作用。如果你使用header(),它必須在任何其他輸出被髮送到瀏覽器之前,否則默認頭文件已經被髮送。 – 2014-10-20 07:14:36
必須在發送任何實際輸出之前調用header(),或者通過普通的HTML標籤,空白行在一個文件中,或從PHP。這是一個非常常見的錯誤讀碼與包括或要求,功能,或另一種文件訪問功能,並具有是頭之前輸出的空間或空行()被調用。使用單個PHP/HTML文件時存在同樣的問題。 – 2014-10-20 07:17:00
@Touregsys無法準確得到你...因爲我是一個新手......在上述情況下,我應該把頭() – user3305327 2014-10-20 07:18:53