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.

Community
  • 1
  • 1
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156

3 Answers3

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.

Nicholas Byfleet
  • 581
  • 3
  • 15
  • There wasn't a bracket missing at the end of the `$_SESSION['date']`, but there was a bracket missing on the if statement and the semicolons. Thanks for your help! – BlackHatSamurai Nov 06 '13 at 02:05
  • I think there was a bracket missing though. You closed your isset() but not your if(). Maybe I'm wrong, but I don't think so. Anyway, I hope your situation is resolved. Good luck :) – Nicholas Byfleet Nov 06 '13 at 02:15
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;?>
Wagner Leonardi
  • 4,226
  • 2
  • 35
  • 41
  • Thanks, that worked like a charm! I'll accept once I can! I figured it would be something stupid like that. Thank you again :) – BlackHatSamurai Nov 06 '13 at 02:06
  • Don't worry, also, now you can see PHP errors instead none (blank page) it'll be useful for you further. Search for turn-on/off errors via php.ini (php configuration) because you can do it once to the whole PHP application instead changing it manually through the code. It's very useful when you want to hide for users, and show for debugging. – Wagner Leonardi Nov 06 '13 at 02:18
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?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103