Reputation: 43
When I output a date in PHP to a text box as the value, if there is a space when I set it the element gets all messed up.
Code is PHP:
$date= date("d-m-Y h:i:s",strtotime($data['Date']));
$type="input";
$idname="test";
$idx="1";
echo "<".$type." id='".$idname."_". $idx. "' value=".$date." ></input>";
the produced HTML looks like
input id="test_1" value="01-01-1970" 01:00:00=""
If I use
$date= date("d-m-Y|h:i:s",strtotime($data['Date']));
Setting the value will work fine. What is going on here? Why is a quote being added when I ouput the $date
causing a new parameter to be added to the element?
Upvotes: 1
Views: 69
Reputation: 318222
You're missing the single quotes around the value, so the space in the date is read as a seperator for a new attribute when the browser tries to fix your mistake.
echo "<".$type." id='".$idname."_". $idx. "' value='".$date."' />";
// ^ ^
Upvotes: 1