www.webdeveloper.com
Results 1 to 6 of 6

Thread: Problem with CHECKED Radio Button?

  1. #1
    Join Date
    May 2010
    Posts
    6

    Problem with CHECKED Radio Button?

    Hi,
    I am tring to use radio buttons in a form. The thing I am trying to do is this, I have a variable that gets its data from a database. The variable's name is $layout. I am trying to have the value that is stored in the database be used to determine which radio button is checked, but it is not working.

    I have checked and made sure there is a value being transmitted to the $layout variable, and there is. Everything else is working but what I want, how can I fix it?

    I need to keep the code in my echo statement.

    Here is the code I am trying to use.....

    PHP Code:
    echo'
    ...More html form code up here...

    <input name="layout" type="radio" '
    .$layout=='1' ? echo 'CHECKED' .'value="1" />
    <input name="layout" type="radio" '
    .$layout=='2' ? echo 'CHECKED' .'value="2" />
    <input name="layout" type="radio" '
    .$layout=='3' ? echo 'CHECKED' .'value="3" />
    <input name="layout" type="radio" '
    .$layout=='4' ? echo 'CHECKED' .'value="4" />

    ...More html form code down here...'

    Please help?

    Thanks.

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    To help debug your code,

    1) open the php file containing the form in your browser

    2) right click the page and select 'view source'

    3) this will be the code generated by your echo statement.

    4) check if the php generated html code is correct for the radio buttons.

    5) make sure the value of $layout is a valid value and within the correct range of values.

  3. #3
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by BigDC View Post
    Hi,
    I am tring to use radio buttons in a form. The thing I am trying to do is this, I have a variable that gets its data from a database. The variable's name is $layout. I am trying to have the value that is stored in the database be used to determine which radio button is checked, but it is not working.

    I have checked and made sure there is a value being transmitted to the $layout variable, and there is. Everything else is working but what I want, how can I fix it?

    I need to keep the code in my echo statement.

    Here is the code I am trying to use.....

    PHP Code:
    echo'
    ...More html form code up here...

    <input name="layout" type="radio" '
    .$layout=='1' ? echo 'CHECKED' .'value="1" />
    <input name="layout" type="radio" '
    .$layout=='2' ? echo 'CHECKED' .'value="2" />
    <input name="layout" type="radio" '
    .$layout=='3' ? echo 'CHECKED' .'value="3" />
    <input name="layout" type="radio" '
    .$layout=='4' ? echo 'CHECKED' .'value="4" />

    ...More html form code down here...'

    Please help?

    Thanks.
    It looks like you might be using an xHTML doctype based on your closing of the input tags. If that is the case you should be using the checked attribute as in:
    HTML Code:
    <input type="radio" checked="checked" name="layout" value="yourValue" />

  4. #4
    Join Date
    May 2010
    Posts
    6
    What I am trying to get at is this....


    If the value of what is in the variable of $layout is 1, then the first radio button is the one that should be showed as being checked.

    If the value of what is in the variable of $layout is 2, then the second radio button is the one that should be showed as being checked.

    If the value of what is in the variable of $layout is 3, then the third radio button is the one that should be showed as being checked.

    If the value of what is in the variable of $layout is 4, then the fourth radio button is the one that should be showed as being checked.

    That is what I am trying to do with this code below, but it is not working. I get syntax error, unexpected T_ECHO.

    Obviously my php code BELOW is incorrect somehow and I do not know what I need to change to fix it?

    PHP Code:
    echo
    ...More html form code up here... 

    <input name="layout" type="radio" '
    .$layout=='1' ? echo 'CHECKED' .'value="1" /> 
    <input name="layout" type="radio" '
    .$layout=='2' ? echo 'CHECKED' .'value="2" /> 
    <input name="layout" type="radio" '
    .$layout=='3' ? echo 'CHECKED' .'value="3" /> 
    <input name="layout" type="radio" '
    .$layout=='4' ? echo 'CHECKED' .'value="4" /> 

    ...More html form code down here...'

    Please help?
    Last edited by BigDC; 05-26-2010 at 12:07 AM.

  5. #5
    Join Date
    Mar 2010
    Posts
    2,803
    I get syntax error, unexpected T_ECHO.
    Why didn't you post your error message in your first post.

    If you don't post the error messages, let alone all your code then I can't help anymore.

    I skipped school the day they taught mind reading

    Just use some basic Debugging 101 to find the source of the t_echo error.
    Last edited by tirna; 05-26-2010 at 12:14 AM.

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by BigDC View Post
    What I am trying to get at is this....


    If the value of what is in the variable of $layout is 1, then the first radio button is the one that should be showed as being checked.

    If the value of what is in the variable of $layout is 2, then the second radio button is the one that should be showed as being checked.

    If the value of what is in the variable of $layout is 3, then the third radio button is the one that should be showed as being checked.

    If the value of what is in the variable of $layout is 4, then the fourth radio button is the one that should be showed as being checked.

    That is what I am trying to do with this code below, but it is not working. I get syntax error, unexpected T_ECHO.

    Obviously my php code BELOW is incorrect somehow and I do not know what I need to change to fix it?

    PHP Code:
    echo
    ...More html form code up here... 

    <input name="layout" type="radio" '
    .$layout=='1' ? echo 'CHECKED' .'value="1" /> 
    <input name="layout" type="radio" '
    .$layout=='2' ? echo 'CHECKED' .'value="2" /> 
    <input name="layout" type="radio" '
    .$layout=='3' ? echo 'CHECKED' .'value="3" /> 
    <input name="layout" type="radio" '
    .$layout=='4' ? echo 'CHECKED' .'value="4" /> 

    ...More html form code down here...'

    Please help?
    So maybe you need to use a full if statement rather than a ternary operator?

    Code:
    if(statement evaluates to true){ do this }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles