Search code examples
phpjavascripthtmlonclickquotes

javascript: quotes problem: <input ... onclick='....value=\'2011-01-07 19:37\';'> doesn't work?


Why does this not work, but is ok for "Y-m-d"?

<?php
echo "<input id=mindate value=$mindate>
<input type=button value=Today 
onclick='javascript:document.getElementById(\'mindate\').value 
=\'".date("Y-m-d H:i:s")."\';'>";
?>

On the page it looks like:

<input type=button value=Today 
onclick='javascript:document.getElementById(\'mindate\').value
=\'2011-01-07 19:37:21\';'>

Must have something to do with all those quotes, but can't see where...


Solution

  • You need to use double quotes for mindate etc.. Try this:

      <?php echo "<input id=mindate value=$mindate> <input type=button value=Today   
          onclick='javascript:document.getElementById(\"mindate\").value  
         =\"".date("Y-m-d H:i:s")."\";'>"; 
      ?>