Jump to content


Photo

Using Radio Buttons to input in database


  • Please log in to reply
3 replies to this topic

#1 chuddyuk

chuddyuk
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationEngland

Posted 14 March 2006 - 01:03 PM

Im making a news system with catergories. my database lists these catergories. and in the record if there is a "1" in for example the business news record then that is the chosen catergory which means the rest will have "0"s.

i want to input the data now through a form not manually into the records.

im just getting stuck with how to make it add the "1" when the radio button is selected and submited any ideas?

the catergories and wat they are called in the database are:::

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Name = database feild name

Business = business
Student Union = su
Employer News = employers
16 - 19 Years = sixteen
Adult News = adults

[/quote]



thanks in advance for any help!!

#2 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 14 March 2006 - 09:03 PM

[!--quoteo(post=354865:date=Mar 14 2006, 08:03 AM:name=chuddyuk)--][div class=\'quotetop\']QUOTE(chuddyuk @ Mar 14 2006, 08:03 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
im just getting stuck with how to make it add the "1" when the radio button is selected and submited any ideas?
[/quote]

I'm not 100% sure of what you want.. Post what code you currently have, as well as the database design, and maybe we can help more.. As for how to detect if a radio button is selected, you need to do something like this :

<input type='radio' name='newstype' value='1'>Business</input>
<input type='radio' name='newstype' value='2'>Student Union</input>
<input type='radio' name='newstype' value='3'>Employer News</input>
<input type='radio' name='newstype' value='4'>16-19 Years</input>
<input type='radio' name='newstype' value='5'>Adult News</input>

And then on the php side :

if ($_REQUEST['newstype'] == 1) {
   // Handle business news here
} else if ($_REQUEST['newstype'] == 2) {
   // Handle student union news here
} else if ($_REQUEST['newstype'] == 3) {
   // Handle employer news here
} else if ($_REQUEST['newstype'] == 4) {
   // Handle 16-19 years news here
} else if ($_REQUEST['newstype'] == 5) {
   // Handle adult news here
} else {
   // Handle exceptions here (nothing was chosen!)
}

--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#3 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 14 March 2006 - 09:26 PM

im pretty sure he wants something like this..

<input type='radio' name='business' value='1'>Business</input>
<input type='radio' name='su' value='1'>Student Union</input>
<input type='radio' name='employer' value='1'>Employer News</input>
<input type='radio' name='sixeen' value='1'>16-19 Years</input>
<input type='radio' name='adultnews' value='1'>Adult News</input>

And then on the php side :

if ($_REQUEST['business'] == 1) {
   update the database saying they want that news
} else { leave it as a zeo }

if ($_REQUEST['su'] == 1) {
   update the database saying they want that news
} else { leave it as a zeo }

if ($_REQUEST['employer'] == 1) {
   update the database saying they want that news
} else { leave it as a zeo }

if ($_REQUEST['sixteen'] == 1) {
   update the database saying they want that news
} else { leave it as a zeo }

if ($_REQUEST['adultnews'] == 1) {
   update the database saying they want that news
} else { leave it as a zeo }


#4 jmag

jmag
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationSweden

Posted 14 March 2006 - 09:58 PM

The post by insrtsnhere13 isn't actually accurate, since if you name the radiobuttons diffrently they may all be selected. Then you may as well use checkboxes.

Radios are supposed to have the same name, and then you can assign them a value like so:
<input type='radio' name='newstype' value='1'>Business</input>
<input type='radio' name='newstype' value='2'>Student Union</input>
<input type='radio' name='newstype' value='3'>Employer News</input>
<input type='radio' name='newstype' value='4'>16-19 Years</input>
<input type='radio' name='newstype' value='5'>Adult News</input>

then I'd suggest you have a column in the database table called "newstype" for example with datatype Enum. Enumerated values are predefined when you create the table by yourself, which means you can say that this column in the database only accepts the values 1, 2, 3, 4 and 5. ([a href=\"http://dev.mysql.com/doc/refman/4.1/en/enum.html\" target=\"_blank\"]read all about enum here[/a])

When pulling data from the table you know for instance that 1 is business etc. This also means you can take the values directly from the radiobutton and insert it into the database, no if-statement or nothing required...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users