开发者

Radio Button Array - Need Help

I have a radio button array that I need help with. Here is the code:

            <input type="radio" name="radio" id="academic" value="1"<?php
                    if ($row_EventInfo['academic'] == '1') {
                    echo ' checked="checked"';
                    }
                    开发者_如何学编程else {$row_EventInfo['academic'] = '';}
                    ?>>
        <label for="academic">Academic</label><br />

        <input type="radio" name="radio" id="personal" value="1"<?php
                    if ($row_EventInfo['personal'] == '1') {
                    echo ' checked="checked"';
                    }
                    else {$row_EventInfo['personal'] = '';}
                    ?>>
        <label for="personal">Personal</label><br />

        <input type="radio" name="radio" id="diversity" value="1"<?php
                    if ($row_EventInfo['diversity'] == '1') {
                    echo ' checked="checked"';
                    }
                    else {$row_EventInfo['diversity'] = '';}
                    ?>>
        <label for="diversity">Diversity</label><br />

What I'm trying to do is this. I have a column in my database table for each radio button because we have to have their inputs separately. However, I want them to only be able to select one of the buttons at a time. I changed all the names to be the same ("radio"), but since PHP MYSQL uses the names to know where to post the information in the table it doesn't know where to post.

Is there any way to create an if statement to tell it to only allow one button at a time to be selected and keep the inputs separate for the database table?

Please let me know if you need clarification. Thanks!


set the value to the column name, eg

<input type="radio" name="radio" id="diversity" value="diversity"

on the php end, simply do something like

$sql = "UPDATE table SET {$_POST['radio']} = 1";


this is in a form, right? Then you get the data in the POST. I wouldn't set the value to 1, set the value to the value you want to select, e.g. Then in the post, get the value and use this to set the data in the DB.

Hope this helps!

BR,

TJ


If you use:

<input type="radio" name="radio" id="diversity" value="value1">
<input type="radio" name="radio" id="diversity" value="value2">
<input type="radio" name="radio" id="diversity" value="value3">
<input type="radio" name="radio" id="diversity" value="value4">

it will be available in $_POST['radio']; with what ever radio button you selected. eg.value3 dont confuse yourself by naming input types with the same name

If you use:

<input type="radio" name="myradio" id="diversity" value="value1">

It will be available in $_POST['myradio'];

and so on

plus you may want to look into using the ternary operator

if ($row_EventInfo['diversity'] == '1') {
    echo ' checked="checked"';
}
else {$row_EventInfo['diversity'] = '';}

into

echo ($row_EventInfo['diversity'] == '1') ? ' checked="checked"':'';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜