dongyong8491 2016-06-28 14:16
浏览 40
已采纳

使用PHP会话显示状态消息

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>
  • 写回答

1条回答 默认 最新

  • dongxian3852 2016-06-28 15:56
    关注

    HTML pages will not parse PHP data/syntax unless specifically told to via .htaccess, httpd.conf or some similar server level association methods.

    You are trying to display the PHP SESSION data within a HTML page, which is not going to happen until you tell the html page to interpret PHP code.

    This StackOverflow question gives you clear guides on how to achieve this.

    If your demo.htm is just a HTML page with no ability to handle PHP then you will see output (including all the PHP code) as if it was only HTML.

    Example:

    demo.html (from your question):

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

    This is being treated as HTML and so will produce a mess of output due to the < and > being HTML parser opening and closing tags.

    I have also removed the $ from the echo statement as echo is a function not a variable.

    Solution:

    While the link I give above is useful, it is quickest and easiest to simply rename your demo.htm to demo.php to indicate to the server to parse the page as a PHP page. You will need to update links to the page (such as the form action tag) but it will mean the page will be correctly processed by the server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题