简体   繁体   English

html 的 value 属性中的 php 代码

[英]php code within value attribute for html

I have a html form with a textbox in which i want to pull the name of the person signed in to automatically be filled in the text box when the page loads.我有一个带有文本框的 html 表单,我想在其中拉出登录人员的姓名,以便在页面加载时自动填写在文本框中。 below is the code i am using.下面是我正在使用的代码。

 <input type="text" autocomplete="off" name="createdby" id="createdby" value= <?= $first_name.' '.$last_name ?> readonly>

the issue is with the php code问题出在 php 代码上

 <?= $first_name.' '.$last_name ?>

Because of the space between .'因为 . 之间的空间。 ' . ' 。 the last name does not show.不显示姓氏。 however when i remove the space both the first name and the last name show but without any spaces.但是,当我删除空格时,名字和姓氏都显示但没有任何空格。 How do i pull both names from the database and have them separated我如何从数据库中提取两个名称并将它们分开

您应该在 php 标签周围使用双引号。

我相信它需要如下所示:

<...value="<?= $first_name.' '.$last_name ?>" readonly>

implode()看起来比这样的连接更好:

implode(' ', array($first_name, $last_name));

Convert this:转换这个:

<?= $first_name.' '.$last_name ?>

Into this:进入这个:

<?php echo $first_name.' '.$last_name ?>

Or this if you want to simplify variables concatenation:或者如果你想简化变量连接:

<?php echo “$first_name $last_name” ?>

Use the standard php tags and explicitly echo your values使用标准的 php 标签并明确回应您的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM