通过PHP通过id预填充表单输入,但不使用表单HTML内的echo

问题描述

我需要在页面上预填表单输入,但不能在表单HTML内使用echo。所有输入都有唯一的ID,到目前为止,我已经有了此代码。顺便说一下,这是Joomla CMS内部的页面

$user_id = $_GET['findId'];
$user = \JFactory::getUser($user_id);
echo $user->email;

上面的回显只是为了查看代码是否确实有效。

我一直在搜索SO和Google以获得答案,但我一直在寻找以下答案:

<input type="text" id="hello" name="hello" value="<?PHP echo $hello; ?>">

如何在不使用HTML内部回显语句的情况下实现相同的目标?

我应该在以下代码中进行哪些更改以实现此目的:

$user_id = $_GET['findId'];
$user = \JFactory::getUser($user_id);
$hello.value = $user->email;

解决方法

这可以解决吗?

HTML

<input type="text" id="hello" name="hello">

JS

var inputfield = document.getElementById("hello");
inputfield.setAttribute('value','<?=$hello?>');