Search code examples
grailscontrollerradio-buttongsp

Radio buttons, true or false


I've just met with a problem on how to use radio buttons. So this is my codes: In GSP

 <g:each in="${Query1}">
 <label for="account_expired">Account Expired:</label><br />
          <input type="radio" name="acc_expired" value="${it.acc_exp}" checked="checked"> True
              <input type="radio" name="acc_expired" value="${it.acc_exp}"> False
<br />
</g:each>

In my controller:

if(params.username != null )
    {
    String updateQuery = "UPDATE sec_user SET account_locked='"+params.account_locked+"'  WHERE Username='"+params.username+"'"


    String queryname = "select * from sec_user where username = '"+params.username+"'"
    def Query1 = sql.rows(queryname)

    def update = sql.executeUpdate(updateQuery)

    [update:update, Query1:Query1]

    }

So, what I'm trying to do is how does the radio button check which values goes to which 'checked' radio button. Because I'm doing an edit page, so the controller will retrieve information from the database and auto select the radio button, whether is it 'True' or 'False'. In this case it's either '1' or '0'. So is there anyone out there can help me with this?

Thank you guys so much.


Solution

  • To have your view correctly check the radio you want, you need to add an expression to your tag. Also I suspect you don't want each button to have the same value. I'm guessing one should be true and the other false.

    <input type="radio" name="acc_expired" value="${true}" ${it.acc_exp == true ? 'checked="checked"' : ''}> True
    <input type="radio" name="acc_expired" value="${false}" ${it.acc_exp == false ? 'checked="checked"' : ''}> False
    

    PS. You may want to look up SQL Injection.