How to include php variable in html input tag value element?

I am trying to include a value from a database table in an input field value element.
This is what I have, but it does not work:

?><input type="text" size="10" value="<?= date("Y-m-d", 
strtotime($rowupd['upcoming_event_featured_date'])) ?>" name="upcoming_event_featured_date" 
id="keys"/><?php

I have done this before, but usually print it like this:

print '<input type="text" size="10" value="'.date("Y-m-d", 
strtotime($rowupd['upcoming_event_featured_date'])).'" name="upcoming_event_featured_date" 
id="keys"/>';

What is a suitable way to do this without using print ''?

+3
source share
4 answers

It is a good idea to always use full PHP tags, because it will cause your application to break if you switch to another server, or your config is changed to prevent short tags.

?>
<input type="text" size="10" value="<?php
echo(date("Y-m-d", strtotime($rowupd['upcoming_event_featured_date'])));
?>"name="upcoming_event_featured_date" id="keys"/><?php

Also note that you are missing ;the end of your PHP code.

PHP , echo() HTML, PHP HTML .

+3

, , short_open_tag php.ini. <?=. , , , , , .

, . . , :

$featured_date = date("Y-m-d",strtotime($rowupd['featured_date']));

?><input type="text" value="<?=$featured_date?>" name="featured_date" /><?php

, , HTML. , script, HTML-, . , .

+2

You can use the short_open_tag directive to include <? = shortcut for printing. A.

If this is not available, you must use either print or echo to accomplish this.

You can try:

ini_set('short_open_tag', true);
+1
source

You can do

[...] ?><input 
    type="text" 
    size="10" 
    value="<?php echo date("Y-m-d", strtotime($rowupd['upcoming_event_featured_date'])) ?>" 
    name="upcoming_event_featured_date" 
    id="keys"/>
<?php [...]

if there is no PHP in your configuration short_open_tag.

0
source

Source: https://habr.com/ru/post/1698654/


All Articles