Thursday, March 31, 2011

PHP - Defaulting form field value to the current date

I'm trying to default a value in a field to the current date, but my code:

<?php 
echo '<input type="text" name="makeupassignment" size="50" value="'.date("m/d/y").'">';
?>

is only printing this:
'; ?>

From stackoverflow
  • Looks to me that the web page is not running through the PHP interpreter. Check out the source for the web page. Do you see the PHP source code when you click "show source" in your browser?

    I think what happens is that your browser parses this as follows:

    • open tag whose type is ?php
    • two fields named "echo" and "'<input"
    • then fields with values: type="text", name="makeupassignment", size="50", and value="'.date(", and then m/d/y").'"
    • then there is the closing tag >
    • and then the rest is output verbatim
  • I would guess that PHP is not enabled/installed on the webserver you're testing with. View source on that page and you'll see all your code, I bet

  • Basically, PHP is not parsing the page for it's code.

    • Is the script you are running a .html or a .php script? Make sure it's the correct suffix. I find that Firefox won't open a page that is .php (unless i'm accessing it via localhost).
    • Is PHP running? Check by running php -v in a console/terminal window.
    • Is Apache (/IIS/lighttpd etc.) running?

0 comments:

Post a Comment