简体   繁体   中英

IE8 is not displaying php echo

I am using the following code to display the current system time in a text field when the start button is hit. This code works fine in FireFox and Chrome, but IE 8 is not displaying the time and date when the start button is hit. It works if I hit the submit button.

Please advise what am I doing wrong here:

    <form name="form1" action='' method='post'>
    <table>
      <tr>
            <th>
            <p>Vendor was contacted at:</p>
            <button name="time" >Start</button><br>
            <?php
            if (isset($_POST['start']))
            {
            $date_start = date('m-d-Y H:i:s');;
            $_SESSION['start'] = $date_start;
            }
            ?>
            <br>
            <input name="start" type="text" class="textfield" value="<?php echo $_SESSION['start']; ?>"
            <br>
            </th>
    </tr>
     <tr>
        <th>
           <br><input type='submit' name='submit' value='Submit'><br>
        </th>
       </tr>
    </table>
    </form>

Your HTML isn't valid. You've forgotten the > on your <input

<input name="start" type="text" class="textfield" value="<?php echo $_SESSION['start']; ?>">
  • Do read my answer in its entirety, including "Footnotes".

Besides the other answer given about the missing closing character for the input; not knowing whether the session has been started or not, am submitting the following answer.

Firstly, you said in comments that it isn't throwing any errors/notices.

"@Fred-ii-, the code you suggested doesn't display any errors. – adminhaq 25 mins ago"

If you didn't start the session, then you don't have error reporting setup properly or is not configured to "display" notices.

session_start(); is required to reside inside all files using sessions. If you are trying to pass the data to another page, then that is also unclear.

The input itself should have contained something similar to:

<b>Notice</b>: Undefined variable: _SESSION in <b>/var/usr/httdocs/folder/file.php</b> on line <b>(line number)</b>

for value="<?php echo $_SESSION['start']; ?>" , so it's best to use a ternary operator for this.

Ie: value="<?php echo !empty($_SESSION['start']) ? $_SESSION['start'] : ""; ?>"

Fully tested code:

<?php 

error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();

?>

<form name="form1" action='' method='post'>
<table>
  <tr>
        <th>
        <p>Vendor was contacted at:</p>
        <button name="time" >Start</button><br>
        <?php
        if (isset($_POST['start']))
        {
        $date_start = date('m-d-Y H:i:s');

        $_SESSION['start'] = $date_start;
        }
        ?>
        <br>

        <input name="start" type="text" class="textfield" value="<?php echo !empty($_SESSION['start']) ? $_SESSION['start'] : ""; ?>">

        <br>
        </th>
</tr>
 <tr>
    <th>
       <br><input type='submit' name='submit' value='Submit'><br>
    </th>
   </tr>
</table>
</form>

Sidenote: Forms do not hold the name attribute, so you can safely delete it from your declaration.

  • Reload your file with my example code above without resubmitting, and you will see that the input will automatically populate itself.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Footnotes:

  • Make sure you don't have a cache problem.
  • Destroy the session and try again. session_destroy() .

You may also have to change your button from:

<button name="time" >Start</button>

to

<button name="time" type="submit">Start</button>

which is the most likely issue, since I just remembered that I too had the same problem once with IE 8.

Sidenote: IE is a very picky browser. It might even not like the space in <button name="time" > yet, adding type="submit" is the most likely solution.

  • Remember to view your HTML source; it's as good a tool as any when it comes to debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM