Input not being checked via php

1

I have the following input :

<input type="radio" id="isgift0" name="isgift" value="0" class="arredondado" <?php if(Mage::getSingleton('core/session')->getInputMensagem() == 1){echo "checked='checked'"} ?> /> 

Trying to check it via php is not working because it is not being checked. By doing this via jQuery , it is checked correctly, however it takes a lot of time to execute this and it affects the functionality of the page.

Check via jQuery :

$j(document).ready(function () {
     <?php 
        if(Mage::getSingleton('core/session')->getInputMensagem() == 1) {
     ?>
          $j('#isgift0').prop('checked','checked');
          $j('#isgift0').click();
     <?php
        }
     ?>
});

I wanted to know if there would be any way to do this same check, but in a faster way.

    
asked by anonymous 29.12.2017 / 20:48

2 answers

1

The way you did using PHP is correct, just forgot to close the code line with ; , which would look like this (as quoted in the other answer, you can only use checked ):

<input type="radio" id="isgift0" name="isgift" value="0" class="arredondado" <?php if(Mage::getSingleton('core/session')->getInputMensagem() == 1){echo 'checked';} ?> />

Alternatively, you can check radio in instant using pure JavaScript instead of jQuery with document ready (which takes longer to load). Just enter a script right after the element:

<input type="radio" id="isgift0" name="isgift" value="0" class="arredondado" />
<?php
if(Mage::getSingleton('core/session')->getInputMensagem() == 1){
   echo '<script>document.getElementById("isgift0").checked = true;</script>';
}
?>

Why do I say "instantly"?

As the page loads from down , the browser's JavaScript interpreter will be ready and will run script soon after loading the radio element in question, checking it immediately before the rest of the page is loaded.

But the first option using only PHP is still the best, as it is straightforward and you will not need to add more code to the page.

    
30.12.2017 / 00:44
1

Matheus wants to do it via PHP like this:

<?php
$checkedisgift0 = Mage::getSingleton('core/session')->getInputMensagem() == 1 ? 'checked' : '';
?>

<input type="radio" id="isgift0" name="isgift" value="0" class="arredondado" <?php echo $checkedisgift0; ?> />

And remove the jQuery part you created just for this, so when you have the session ==1 it will display something like:

<input type="radio" id="isgift0" name="isgift" value="0" class="arredondado" checked />
  

In html5 it is not necessary checked=checked basta checked .

It's still good to note that elements can not repeat IDs, and radio type is generally used for multiple choice, if there is only one radio with the same name (name="isgift") then it is preferable to use the type="checkbox"

    
29.12.2017 / 21:06