Search code examples
phpformsjoomlaecho

Prefill form inputs by id via PHP but without using echo inside the HTML of the form


I need to prefill form inputs on a page but I cannot use echo inside the HTML of the form. All inputs have unique ID's and so far I have this code. By the way this is a page inside the Joomla CMS.

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

The above echo is just to see if the code was actually working and it is.

I have been searching SO and Google to get an answer but I keep finding these answers:

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

How can I achieve the same but without using the echo statement inside the HTML?

What should I change in the below code to achieve this:

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

Solution

  • Could this be a solution?

    HTML

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

    JS

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