Help me debug this radio button code

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Idiot88
New php-forum User
New php-forum User
Posts: 17
Joined: Thu Jan 25, 2018 2:56 pm

Tue Feb 13, 2018 12:27 pm

Hi

I’m trying to pre-select a radio button using the following code:

$g2 = ($pc->get_gender() == 'male') ? '\'checked\'=>\'checked\'' : '';
$frmStr = $frmStr . $frm->addInput('radio', 'gender', /*'male',*/ array($g2, 'required'=>true) ) . ' male ' . PHP_EOL;
echo($frmStr);

$pc->get_gender() returns “male”.

add_input() calls add_attributes() and they are defined as:

private function addAttributes( $attr_ar ) {
$str = '';
// check minimized (boolean) attributes
$min_atts = array('checked', 'disabled', 'readonly', 'multiple',
'required', 'autofocus', 'novalidate', 'formnovalidate'); // html5
foreach( $attr_ar as $key=>$val ) {
if ( in_array($key, $min_atts) ) {
if ( !empty($val) ) {
$str .= $this->xhtml? " $key=\"$key\"": " $key";
}
} else {
$str .= " $key=\"$val\"";
}
}
return $str;
}

function addInput($type, $name, $value, $attr_ar = array() ) {
$str = "<input type=\"$type\" name=\"$name\" value=\"$value\"";
if ($attr_ar) {
$str .= $this->addAttributes( $attr_ar );
}
$str .= $this->xhtml? ' />': '>';
return $str;
}

I know that addInput() and add_attributes() work because they are in a web tutorial on dyn_web. The problem is the parameters that I pass to addInput(). Please help me with this.

Thanks.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 588
Joined: Mon Feb 22, 2016 5:52 pm

Tue Feb 13, 2018 1:48 pm

You've commented out one of your parameters.

Don't assume that just because something is in a web tutorial, that it is correct; do your own tests to make sure that it's what you want and does what you want. I'm not saying it's wrong, just beware that it might not be what you think it is.

Use [ code] tags when posting code, it makes helpers life easier and makes it more likely that you will get an answer and more quickly.

Idiot88
New php-forum User
New php-forum User
Posts: 17
Joined: Thu Jan 25, 2018 2:56 pm

Tue Feb 13, 2018 2:14 pm

I uncommented the ‘male’ parameter but it doesn’t fix it.

Code: Select all

  $g2 = ($pc->get_gender() == 'male') ? '\'checked\'=>checked' : '';
  $frmStr = $frmStr . $frm->addInput('radio', 'gender', 'male', array($g2, 'required'=>true)  ) . ' male ' . PHP_EOL
Maybe addInput() or addAttr() is not working but I need understanding. Here are those functions again in code tags:

Code: Select all

    private function addAttributes( $attr_ar ) {
        $str = '';
        // check minimized (boolean) attributes
        $min_atts = array('checked', 'disabled', 'readonly', 'multiple',
                'required', 'autofocus', 'novalidate', 'formnovalidate'); // html5
        foreach( $attr_ar as $key=>$val ) {
            if ( in_array($key, $min_atts) ) {
                if ( !empty($val) ) { 
                    $str .= $this->xhtml? " $key=\"$key\"": " $key";
                }
            } else {
                $str .= " $key=\"$val\"";
            }
        }
        return $str;
    }
    
    function addInput($type, $name, $value, $attr_ar = array() ) {
        $str = "<input type=\"$type\" name=\"$name\" value=\"$value\"";
        if ($attr_ar) {
            $str .= $this->addAttributes( $attr_ar );
        }
        $str .= $this->xhtml? ' />': '>';
        return $str;
    }

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 588
Joined: Mon Feb 22, 2016 5:52 pm

Tue Feb 13, 2018 2:23 pm

Have you looked at / checked the html that is output?

Idiot88
New php-forum User
New php-forum User
Posts: 17
Joined: Thu Jan 25, 2018 2:56 pm

Tue Feb 13, 2018 2:29 pm

How do I look at the html?

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 588
Joined: Mon Feb 22, 2016 5:52 pm

Tue Feb 13, 2018 3:12 pm

View source in your browser :o

Idiot88
New php-forum User
New php-forum User
Posts: 17
Joined: Thu Jan 25, 2018 2:56 pm

Tue Feb 13, 2018 3:57 pm

Thanks for your help. The html source for the relevant line is:

Code: Select all

<p><label for="gender">Gender: </label><input type="radio" name="gender" value="male" 0="0" required="required"> male 
It looks like the problem is the 0=‘0’
What should it be?

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 588
Joined: Mon Feb 22, 2016 5:52 pm

Tue Feb 13, 2018 8:59 pm

To be blunt, the problem is that you don't know enough about HTML, PHP or the code that you have copied and pasted.

However, the

Code: Select all

0="0"
is and indication of what you have done wrong, browsers will ignore what they don't understand

compare these two bits from the HTML that you posted

Code: Select all

'\'checked\'=>checked'

'required'=>true
you can see (from the output) that one worked and the other one didn't.

Idiot88
New php-forum User
New php-forum User
Posts: 17
Joined: Thu Jan 25, 2018 2:56 pm

Wed Feb 14, 2018 2:19 pm

Thank you Hyper. My code is working now.

You are right, I don’t know enough about PHP, HTML and the example code I have. But I am learning :)

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 588
Joined: Mon Feb 22, 2016 5:52 pm

Wed Feb 14, 2018 2:36 pm

But I am learning :)
That's the important thing :)

Post Reply