Mat5hias Mat5hias - 3 months ago 11
PHP Question

Execute jQuery script if value is 1 inside a php echo in Joomla

I like to execute a jQuery Script if "Yes" is selected in the radio button. I am getting confused of how to implement the addScriptDeclaration from Joomla inside an echo, also I'm not sure if the call

if (count($displayonload) == '1'){
is the right way to get the value from the checkbox.

Explaining the xml code: The radio button in the backend of Joomla with value 0 if no and value 1 if yes is selected.

<field name="displayonload"
type="radio" default="0"
label="Display Modal on load"
description="">
class="btn-group btn-group-yesno">
<option value="1">JYES</option>
</field>


The value 1 or 2 is stored like this:

$displayonload = $params->get('$displayonload');


The php code to echo the js script (jQuery):

if (count($displayonload) == '1'){
echo '
$document = JFactory::getDocument();
$document->addScriptDeclaration('

jQuery(window).load(function(){
jQuery("#myModal").modal("show");
});
');';
}


Sorry if my code is a mess but I'm just starting with Joomla and PHP development.

Answer

I found the solution, may this will help someone.

if ($params->get('displayonload', '0') == '1') {
      $document = JFactory::getDocument();
    $js = 'jQuery(window).load(function(){

      jQuery("#myModal").modal("show");
    });';
      $document->addScriptDeclaration($js); 
}