0

I'm trying to use PHP to set the value of a text box, and I tried the solution in this question:

Can HTML be embedded inside PHP "if" statement?

Here is what I'm doing:

 <?php if(isset($_SESSION['date']) : ?>
        <div class="label">Service Date:</div> <input type="text" class="service_date" name="service_date" value="<?php echo $_SESSION['date']?>"/>
    <?php else : ?>
        <div class="label">Service Date:</div> <input type="text" class="service_date" name="service_date" value="<?php echo $_SESSION['date']?>"/>
    <?php endif;?>

But this isn't working for me. I know that the <?php echo $_SESSION['date']?> is valid and working, because I inserted this at the beginning of the form, and it displayed the expected data. Can someone please tell me what I'm doing wrong here?

To be clear when I say it isn't working, there is NO data being rendered on the page. You can't see ANY HTML. When I remove the above code, all the HTML fields are displayed as expected.

4

3 回答 3

2

You are missing an end bracket after $_SESSION['date']. You are also missing some semi colons after the echo statements. Your code should read:

<?php if(isset($_SESSION['date'])) : ?>
    <div class="label">Service Date:</div> <input type="text" class="service_date" name="service_date" value="<?php echo $_SESSION['date']; ?>"/>
<?php else : ?>
    <div class="label">Service Date:</div> <input type="text" class="service_date" name="service_date" value="<?php echo $_SESSION['date']; ?>"/>
<?php endif;?>

Hope that helps.

于 2013-11-06T02:02:54.493 回答
1

Probably your PHP configurations (see php.ini) is setted to hide errors, put this code at the beginning of your php code do turn on display errors

<?php

// Report all PHP errors
error_reporting(E_ALL);

?>

The error in your code is a missing ")" of if statment, here's the working code:

<?php if(isset($_SESSION['date'])) : ?>
        <div class="label">Service Date:</div> <input type="text" class="service_date" name="service_date" value="<?php echo $_SESSION['date']?>"/>
    <?php else : ?>
        <div class="label">Service Date:</div> <input type="text" class="service_date" name="service_date" value="<?php echo $_SESSION['date']?>"/>
    <?php endif;?>
于 2013-11-06T02:04:21.663 回答
1

Others have pointed out the missing bracket issue in your if statement, but I find this kind of HTML on one line, PHP on the other formatting to be hard to read & harder to debug. It’s been popularized by template formatting in code used in CMS setups. But since I come from a pure PHP programming background, I would have written that chunk of code as:

<?php
  if (isset($_SESSION['date'])) {
    echo '<div class="label">Service Date:</div> <input type="text" class="service_date" name="service_date" value="' . $_SESSION['date']; . '"/>';
  }
  else {
    echo '<div class="label">Service Date:</div> <input type="text" class="service_date" name="service_date" value="' . $_SESSION['date']; . '"/>';
  }
?>

But that said, looking at your code your if and else are outputting the same value? A typo? Or is this pseudocode for an example rather than production code?

于 2013-11-06T02:14:26.897 回答