1
votes

I customize joomla registration module by adding some extra fields. If registration succeeds, I need to call some javascript code. So I changed the registration controller file (components/com_users/controllers/registration.php) like:

public function register()
{                   

    // Check for request forgeries.
    JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        ...
    // Attempt to save the data.
    $return = $model->register($data);
    if ($return === true) {
        $script = "<Here comes javascript code>";
        echo $script; // this doesn't work

        // I don't know how to call above script
        ...
    }
    ...
}

The echo doesn't work here and I found that joomla needs to use its api like "$document->addScriptDeclaration($script);" But this code needs to be running on view, not on controller.

Have any thoughts? Thanks in Advance.

1
No, no, no. Please don't edit any core files. Instead do your changes via a plugin which can then manipulate the register functionLodder
I know it's bad. Only I want to know how to call javascript code in such situation.Alex

1 Answers

1
votes

You should use exit construct to execute the script. as script runs perfectly after window load. so when you use exit after javascript it terminate the execution of php script and then your javascript will run.

try below code.

public function register()
{                   

// Check for request forgeries.
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

    ...
// Attempt to save the data.
$return = $model->register($data);
if ($return === true) {
    $script = "<Here comes javascript code>";
    echo $script; // this doesn't work
    exit;
    // I don't know how to call above script
    ...
}
...
}

hope it will help.