Getting php date() in html input value breaks it

Viewed 55

I'm currently having a little trouble making this piece of html code to work:

(...)

<form action="/get">
  <br>
  <?php date_default_timezone_set("Europe/Budapest"); ?>
  <input class="hidden" type="number" name="h" value="<?php echo date("H"); ?>" >
  <input class="hidden" type="number" name="m" value="<?php echo date("i"); ?>" >
  <input class="hidden" type="number" name="s" value="<?php echo date("s"); ?>" >
  <input class="button" type="submit" value="Send time to server"> 
</form>

(...)

Basically, it should send the client's time to the server. (Like this: .../get?h=19?m=34?s=53 ) My problem is that it does not run the date() function at all, and breaks the input tag like this: hidden and with values shown

I don't have any idea why doesn't it work. Can somebody tell me why?

3 Answers

PHP runs on the server. If you want to send time from the client to the server, you need to use JavaScript instead. The browser can't parse your PHP code and treats it as HTML.

You should probably write a function in a script block to don't mess up your form's readability.

https://tecadmin.net/get-current-date-time-javascript/

I think you have to send time to client browser. So I prefer to use JavaScript instead of PHP.

You can use Date() function to create an date object with the current date. If you want to get date in specific format you might need to refer Date and time in JavaScript article.

here try this:

<input class="hidden" type="number" name="h" value="<?php echo date('H'); ?>" >
<input class="hidden" type="number" name="m" value="<?php echo date('i'); ?>" >
<input class="hidden" type="number" name="s" value="<?php echo date('s'); ?>" >
Related