简体   繁体   中英

Yes/no variable radio option

I am trying to make a variable yes/no radio type. My script is:

                while ($row1 = mysql_fetch_assoc($vragen))
                {
                    $type = $row1['type'];
                    if ($type == 'kort')
                        {
                    echo '
                        <tr>
                            <td width="60%">'
                                .$row1['vraag'].'
                                <input type="hidden" name="vragen'.$aantalBranches.'[]" value="'.$row1['vraag'].'"/>
                            </td>
                            <td>
                            <input type="text" name="antwoorden'.$aantalBranches.'[]"/>
                            </td>
                        </tr>';
                        }
                    elseif ($type == 'lang')
                    {
                    echo '
                        <tr>
                            <td width="60%">'
                                .$row1['vraag'].'
                                <input type="hidden" name="vragen'.$aantalBranches.'[]" value="'.$row1['vraag'].'"/>
                                </td>       
                        <td><textarea name="antwoorden'.$aantalBranches.'[]" cols="30"rows="5"></textarea><td/>
                        </tr>';

                    }
                    elseif ($type == 'ja,nee')
                        {
                        echo'
                        <tr>
                            <td width="60%">'
                                .$row1['vraag'].'
                                </td>
                            <td>
                                <input type="radio" name="optie'.$c.'"  value="yes">Yes
                                <input type="radio" name="optie'.$c.'"  value="no">No  
                            </td>';

                            if ('optie'.$c.'' == 'yes')
                                {
                                echo'
                                <td><input type="hidden" name="vragen'.$aantalBranches.'[]" value="'.$row1['vraag'].'"/></td>
                                <td><input type="text" name="antwoorden'.$aantalBranches.'[]" value="yes"/></td>
                                </tr>';
                                }
                            else
                            {
                            echo'
                            <td><input type="hidden" name="vragen'.$aantalBranches.'[]" value="'.$row1['vraag'].'"/></td>
                            <td><input type="text" name="antwoorden'.$aantalBranches.'[]" value="no"/></td>
                            </tr>';
                            }

                        $c++;

                    }


                }

            echo '</table>';
            $aantalBranches++;
    }

The problem is that i want the result stored in 1 variable the $antwoorden [] so the most logic way was a if else for this but for a strange reason it doesn't work any1 can help me ? It now only shows "no" even if i change it to "yes". I edited the script and added the other choises of the form long and short fields(textarea/inputfield) Thanks in advance.

if ('optie'.$c.'' == 'yes')

This part will always evaluate to false .

You probably don't know how this variable is going to be named because name depends on value from variable $c and this is what is giving you difficulties.

To overcome this you could just pull all of the variables that start with 'optie' string and then cycle through them when you want to use them. You can also access this variable like this.

$optionname = 'optie'.$c;
$optionvalue = $$optionname;
// or
$optionvalue = $_POST[$optionname];

But generally speaking looking at your code it is very badly structured and I would consider restructuring that code. But without more context I can't give you more instructions on how to do this.

Update:

Just change mentioned condition to:

if ($_POST['optie'.$c] === 'yes')

Or change $_POST to $_GET if you are using 'GET' http request to pass 'optie' variables.

It always shows no because this can never evalulate to true:

if ('optie'.$c.'' == 'yes')

A string that contains optie will never match yes . So the else block is always entered that outputs the no value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM