I'm trying to display a submission status above my contact form, so my plan is to use sessions, but it's not working properly. The form successfully submits, and the page gets successfully redirected back to the demo.htm page (after it was redirected to index2.php), but no status message is ever diplayed.
I was also going to use the session in order to display required tags on blank fields since iOS doesn't support the 'required' input tag (The giant commented out chunk of the html form will replace the current form fields to handle the required fields on iOS).
UPDATE: There's something strange going on with echo.... Could it be something with the php.ini file (if so, what?)?
Input#1:
<p>Text Here</p>
<p><?php echo($_SESSION['status']); ?></p>
<p>Temp</p>
<p id="status">
<?php
if(isset($_SESSION['status'])){
echo("<br/>"$_SESSION['status']."<br/>");
unset($_SESSION['status']);
}
?>
</p>
<p>More text here</p>
Output #1:
Text Here
Temp
"$_SESSION['status']."
"); unset($_SESSION['status']); } ?>
More text here
Input #2:
<?php echo strcmp("Hello world!","Hello world!")."<br/>"; ?>
Output #2:
"; ?>
PHP:
<?php
session_start();
$firstName = $lastName = $email = $companyName = $jobTitle = $phoneNumber = $comments = "";
$noErrors = true;
if($_POST['submit']) {
if(empty($_POST["firstname"])) { $_SESSION["nameErr"] = "First name is required"; $noErrors=false;}
else { $firstName = $_POST['firstname']; }
if(empty($_POST["lastname"])) { $_SESSION["lastErr"] = "Last name is required"; $noErrors=false;}
else { $lastName = $_POST['lastname']; }
if(empty($_POST["email"])) { $_SESSION["emailErr"] = "Email is required"; $noErrors=false;}
else { $email = $_POST['email']; }
if(empty($_POST["companyname"])) { $_SESSION["companyErr"] = "Company name is required"; $noErrors=false;}
else { $companyName= $_POST['companyname']; }
if(empty($_POST["position"])) { $_SESSION["jobErr"] = "Job title is required"; $noErrors=false;}
else { $jobTitle = $_POST['position']; }
if(empty($_POST["number"])) { $_SESSION["phoneErr"] = "Phone number is required"; $noErrors=false;}
else { $phoneNumber = $_POST['number']; }
$comments = $_POST['comments'];
$header = "From: xxxxx.com";
$to = 'xxxxx.com';
$subject = 'Demo request';
$message = "From:
Name: $firstName $lastName
E-mail: $email
Company Name: $companyName
Job Title: $jobTitle
Phone Number: $phoneNumber
Comments: $comments";
if(($noErrors == true) && mail ($to, $subject, $message, $header)) {
$_SESSION['status'] = "Your message has been sent!";
header('Location: demo.htm');
} else {
$_SESSION['status'] = "Something went wrong, please try again";
header('Location: demo.htm');
}
exit();
}
?>
HTML:
<?php
session_start();
?>
<!DOCTYPE html><html>
<head>*stuff*</head><body>
<p id="status">
<?php
if(isset($_SESSION['status'])){
$echo $_SESSION['status'];
unset($_SESSION['status'];
}
?>
</p>
<div id="requestADemo">
<p style="line-height: 2%; font-size: 24px; font-weight: 200;">Request A Demo</p>
<p style="line-height: 2%; font-style:italic; font-size: 13px;">*indicates required field</p>
<form method="post" action="index2.php">
<!--p class="demo" style="margin:0;">First Name:*</p>
<input type ="name" name="firstname" required>
<span><?php
if(isset($_SESSION['nameErr'])){
$echo $_SESSION['nameErr'];
unset($_SESSION['nameErr'];
}
?></span>
<p class="demo">Last Name:*</p>
<input type ="name" name="lastname" required>
<span><?php
if(isset($_SESSION['lastErr'])){
$echo $_SESSION['lastErr'];
unset($_SESSION['lastErr'];
}
?></span>
<p class="demo">Email:*</p>
<input type ="email" name="email" required>
<span><?php
if(isset($_SESSION['emailErr'])){
$echo $_SESSION['emailErr'];
unset($_SESSION['emailErr'];
}
?></span>
<p class="demo">Company Name:*</p>
<input type ="name" name="companyname" required>
<span><?php
if(isset($_SESSION['companyErr'])){
$echo $_SESSION['companyErr'];
unset($_SESSION['companyErr'];
}
?></span>
<p class="demo">Job Title:*</p>
<input type ="name" name="position" required>
<span><?php
if(isset($_SESSION['jobErr'])){
$echo $_SESSION['jobErr'];
unset($_SESSION['jobErr'];
}
?></span>
<p class="demo">Phone Number:*</p>
<input type ="number" name="number" required>
<span><?php
if(isset($_SESSION['phoneErr'])){
$echo $_SESSION['phoneErr'];
unset($_SESSION['phoneErr'];
}
?></span>
<p class="demo">Comments:</p>
<textarea name="comments" placeholder="Type Here" rows ="10" columns="50"></textarea>
<p class="demo"><input type="submit" value="Submit" name="submit"></p-->
<p class="demo">First Name:*<br/><input type ="name" name="firstname" required></p>
<p class="demo">Last Name:*<br/><input type ="name" name="lastname" required></p>
<p class="demo">Email:*<br/><input type ="email" name="email" required></p>
<p class="demo">Company Name:*<br/><input type ="name" name="companyname" required></p>
<p class="demo">Job Title:*<br/><input type ="name" name="position" required></p>
<p class="demo">Phone Number:*<br/><input type ="number" name="number" required></p>
<p class="demo">Comments:<br/><textarea name="comments" placeholder="Type Here" rows ="10" columns="50"></textarea></p>
<p class="demo"><input type="submit" value="Submit" name="submit"></p>
</form>
</body>
</html>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…