How to Prepopulate fields with joomla variables

  • Profile Image
    Asked on February 19, 2016 at 02:58 AM


    I managed to prefill my form for my users with all their data (name, address, etc.), but there is an issue with the "email" field. As you can see on my screenshot, it is only written <span id=, and when I hover the box it says: "Please add '@' in the e-mail address, there is missing a symbol in '<span id='"

    Here is my code, it is pretty straightforward:




    $user = JFactory::getUser();

    $profile = JUserHelper::getProfile($user->id);

    if (!$user->guest) {

       $realname = $user->name;

       $email= $user->email;

       $street = $profile->profile['address1'];

       $city= $profile->profile['city'];

       $postcode= $profile->profile['postal_code'];

       $country= $profile->profile['country'];


       $realname = "guest";


    echo '<script type="text/javascript" src="[city]=' . $city. '&address79[addr_line1]=' . $street. ' &address79[postal]=' . $postcode. '&input87=' . $country . '&yourName86=' . $realname. '&yourEmail=' . $email. '"></script>';




    When I simply echo the $email variable, it works well and appears as a link at the top of my article. Maybe the problem precisely happens because this variable is a link, not a plain text...

    Any idea how to make it work?

  • Profile Image
    Answered on February 19, 2016 at 08:38 AM

    When passing variables directly into any prepopulation embed codes of your form, please try to URL encode them. This will ensure that if a variable contains any HTML character in it, such as a quote, double-quote, or any other special character, these characters will be correctly encoded into an URL and will not break your HTML codes.

    I would recommend that you please try encoding your variables with PHP urlencode, such as:


    Your echo code would be changed into something like this:

    echo '<script type="text/javascript" src="[city]=' . urlencode($city) . '&address79[addr_line1]=' . urlencode($street) . ' &address79[postal]=' . urlencode($postcode) . '&input87=' . urlencode($country) . '&yourName86=' . urlencode($realname) . '&yourEmail=' . urlencode($email) . '"></script>';

    I would also ask you to double-check whether the content of your $email variable actually contains just the email, and not any other HTML codes by accident.

    Please let us know whether URL encoding the variables resolves the issue you were having.

  • Profile Image
    Answered on February 21, 2016 at 11:27 PM

    Hi Boris, 


    It worked, thanks a lot for your help!

    I was expecting the problem to be elsewhere... but that is a relief.


    Thank you again.


  • Profile Image
    Answered on February 22, 2016 at 03:35 AM

    On behalf of my colleague, you are kindly welcome. We are glad to hear that the issue has been resolved for you.

    If you have any question or issues, please do not hesitate contacting us. We will be happy to assist you.