gpt4 book ai didi

javascript - php header not header 函数不起作用

转载 作者:行者123 更新时间:2023-12-03 11:36:48 24 4
gpt4 key购买 nike

我遇到了 PHP header 无法正常工作的问题。这是完整的代码。一切工作正常,只是页面最后没有重定向。

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

电子邮件和数据库插入都很完美,只是页面没有重定向。

最佳答案

如果您将 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>

关于javascript - php header not header 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26460211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com