我正在尝试在联系表单上方显示提交状态,因此我的计划是使用会话,但它无法正常工作。表单成功提交,页面成功重定向回demo.htm页面(在重定向到index2.php之后),但是没有显示任何状态消息。
我还打算使用会话,以便在空白字段上显示所需的标签,因为iOS不支持“必需的”输入标记(巨型注释掉的html表单块将替换当前的表单字段来处理iOS上的必填字段。
更新:echo有一些奇怪的事情......可能是php.ini文件的内容(如果有的话,是什么?)?
输入#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>
输出#1:
Text Here
Temp
"$_SESSION['status']."
"); unset($_SESSION['status']); } ?>
More text here
输入#2:
<?php echo strcmp("Hello world!","Hello world!")."<br/>"; ?>
输出#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: \r\n
Name: $firstName $lastName\r\n
E-mail: $email\r\n
Company Name: $companyName\r\n
Job Title: $jobTitle\r\n
Phone Number: $phoneNumber\r\n
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>
答案 0 :(得分:0)
HTML页面不会解析PHP数据/语法,除非通过.htaccess
,httpd.conf
或某些类似的服务器级关联方法明确说明。
您正试图在HTML页面中显示PHP SESSION
数据,直到您告诉html页面解释PHP代码才会发生这种情况。
This StackOverflow question gives you clear guides on how to achieve this
如果您的demo.htm
只是一个无法处理PHP的HTML页面,那么您将看到输出(包括所有PHP代码),就像它只是HTML 一样。
示例:
demo.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>
由于<
和>
是HTML解析器的开始和结束标记,因此将其视为HTML,因此会产生大量输出。
我还从$
语句中删除了echo
,因为echo是一个函数而不是变量。
虽然我上面给出的链接很有用,但最简单,最简单的方法是将demo.htm
重命名为demo.php
,以指示服务器将页面解析为PHP页面。您需要更新页面的链接(例如表单操作标记),但这意味着服务器将正确处理页面。