-1

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: \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>
djd97
  • 77
  • 1
  • 1
  • 9

1 Answers1

0

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.

Community
  • 1
  • 1
Martin
  • 19,815
  • 6
  • 53
  • 104
  • Would there be any issues with just making it a .php file instead? That's fine by me if it's not going to break functionality of anything else. (And it does work perfectly when it's .php, thanks). – djd97 Jun 28 '16 at 16:02
  • as long as your anchors on other parts of your site have the correct anchor link destination name (`file.php` rather than `file.htm`) then no, it would not cause any issues. @djd97 – Martin Jun 28 '16 at 16:03
  • Alright, perfect. Thanks. Oh, I did notice one thing the .php file will fail to load if there are any typos anywhere on the page since it's no longer html. Is there any way to prevent that -- i.e. is there anyway to make just the element where the typo exists look broken rather than preventing the whole page from rendering? – djd97 Jun 28 '16 at 16:07
  • Also, I've actually got a somewhat related question. How would I prevent it from clearing the input fields if the input was not correct? The way I have it set up now is if any of the fields are incorrect the else at the very end of my index.php file will execute and then my demo.php file will have all the fields emptied (as well as having notices next to any field that needs correcting). @Martin – djd97 Jun 28 '16 at 16:12
  • 1) please read up on [PHP Error logging](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) .The whole page fails to load because PHP found a fatal error that killed the script (Or possibly your data-processing logic is flawed). – Martin Jun 28 '16 at 16:14
  • 2) You would need to use something like in the form (if HTML5) use autocomplete or when the form submits, the page it submits to loads all the `$_POST` data into a PHP `$_SESSION` array and you can then edit the form page to load the session data. Can be useful if session data is saved using AJAX every few seconds or so. – Martin Jun 28 '16 at 16:16
  • (your comment question 2 is really a question in its own right) - There are answers on StackOverblow on that topic `:-)` – Martin Jun 28 '16 at 16:16
  • Okay, so if one of the fields is blank, I'll save the $_POST values into $_SESSION values. How would I then fill in the input fields with the $_SESSION values (which will either be empty strings or inputs)? Yeah, I know it is it's own question, but I'd rather not get locked out of asking questions for 90 minutes just in case. – djd97 Jun 28 '16 at 16:22
  • why do you get locked out of asking questions? `:-/` – Martin Jun 28 '16 at 16:25
  • search SO for "how to save unsubmitted form data [php] [html]" – Martin Jun 28 '16 at 16:26
  • I'm restricted to one question every 90 minutes (until I have more posts I guess). And thanks :) – djd97 Jun 28 '16 at 16:27
  • I figured everything out. Thanks so much. – djd97 Jun 28 '16 at 16:48
  • I didn't know that about the 90-minute restriction. Glad you got it all sorted though `:-)` – Martin Jun 28 '16 at 17:49