是/否可变无线电选项 [英] Yes/no variable radio option

查看:105
本文介绍了是/否可变无线电选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个可变的是/否广播类型。
我的脚本是:
$ b $ pre $ while($ row1 = mysql_fetch_assoc($ vragen))
{
$ type = $ row1 ['type'];
if($ type =='kort')
{
echo'
< tr>
< td width =60%>'
。$ row1 ['vraag']。'
< / td>
< td>
< input type =textname =antwoorden'。$ aantalBranches。'[]/>
< / td>
< / tr>';
}
elseif($ type =='lang')
{
echo'
< tr>
< td width =60%>'
。$ row1 ['vraag']。'
< / td>
< td>< textarea name =antwoorden。$ aantalBranches。'[]cols =30rows =5>< / textarea>< td />
< / tr>';


elseif($ type =='ja,nee')
{
echo'
< tr>
< td width =60%>'
。$ row1 ['vraag']。'
< / td>
< td>
< input type =radioname =optie'。$ c。'value =yes>是
< input type =radioname =optie'。$ c。'value =no>否
< / td>';

if('optie'。$ c。''=='yes')
{
echo'
< td>< input type =隐藏name =vragen。$ aantalBranches。'[]value ='。$ row1 ['vraag']。'/>< / td>
< td>< input type =textname =antwoorden'。$ aantalBranches。'[]value =yes/>< / td>
< / tr>';
}
else
{
echo'
< td>< input type =hiddenname =vragen'。$ aantalBranches。'[]值= /> '[vraag ']。' $ ROW1。';< / TD>
< td>< input type =textname =antwoorden。$ aantalBranches。'[]value =no/>< / td>
< / tr>';
}

$ c ++;

}


}

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





$ b

问题是我想将结果存储在1变量$ antwoorden [ ]所以最逻辑的方式是一个如果其他的这个,但由于一个奇怪的原因,它不起作用any1可以帮助我?
现在只显示否,即使我将其更改为是。
我编辑了脚本并添加了其他形式的长字段和短字段(textarea / inputfield)
的选择感谢。

解决方案

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

这部分内容总是评估为 false 您可能不知道这个变量将被命名,因为name取决于变量$ c的值,这就是给你带来困难的原因。



为了克服这个问题,你可以把所有的以'optie'字符串开头的变量,然后在您想要使用它们时循环。你也可以像这样访问这个变量。

  $ optionname ='optie'。$ c; 
$ optionvalue = $$ optionname;
//或
$ optionvalue = $ _POST [$ optionname];

但是一般来说,看着你的代码,它的结构非常糟糕,我会考虑重构那些代码。但没有更多的上下文,我不能给你更多的指示如何做到这一点。



更新:
$ b

只需将提到的条件改为:

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

如果您使用的是'GET',则将$ _POST更改为$ _GET请求传递'optie'变量。


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆