Jump to content

submit multiple radio boxes


Chris Val Kef

Recommended Posts

got something like this:

<input type="radio" name="1" value="0">
<input type="radio" name="1" value="1">
...
<input type="radio" name="2" value="0">
<input type="radio" name="2" value="1">
...
<input type="radio" name="3" value="0">
<input type="radio" name="3" value="1">
...
and so on... I got one submit button.

how should i manage them with the $_POST? Now i got an increasing pointer and do something like this

if (isset ( $_POST["$cnt"]))
{
      if ($_POST["$cnt"] == 0)
      {
      }
      elseif ($_POST["$cnt"] == 1)
      {
      }
}
....

what am i doing wrong? i'm getting a blank page when i submit...

thanx in advance!
Link to comment
Share on other sites

First, don't use numbers for names. Try something like this:
(Form)
[code]
<input type="radio" name="rb[1]" value="0">
<input type="radio" name="rb[1]" value="1">
<input type="radio" name="rb[2]" value="0">
<input type="radio" name="rb[2]" value="1">
<input type="radio" name="rb[3]" value="0">
<input type="radio" name="rb[3]" value="1">
[/code]
(script)
[code]<?php
for ($cnt=1;$cnt<4;$cnt++)
  if (isset($_POST['rb'][$cnt]))
    if ($_POST['rb'][$cnt] == 0)
          echo "Radio button $cnt is 0<br>";
    else
          echo "Radio button $cnt is 1<br>";
?>[/code]

Ken
Link to comment
Share on other sites

If you only have two options for each somethign allogn the lines of the bellow will work

[code]<input type="radio" name="box1" value="0">
<input type="radio" name="box1" value="1">
<input type="radio" name="box2" value="0">
<input type="radio" name="box2" value="1">
<input type="radio" name="box3" value="0">
<input type="radio" name="box3" value="1">[/code]

[code]
$box1=$_POST['box1'];
$box2=$_POST['box2'] ;
$box3=$_POST['box3'];

echo $box1 $box2 $box3
[/code]

basicly what your left with here are $box1 $box2 $box3 which hold the value's for each box you can then  place those anywhere you want , whether thats an echo or putting into a database

KIS ( keep it simple )
Link to comment
Share on other sites

[quote author=Fallen_angel link=topic=116888.msg476686#msg476686 date=1164941217]
[code]
$box1=$_POST['box1'];
$box2=$_POST['box2'] ;
$box3=$_POST['box3'];

echo $box1 $box2 $box3
[/code]

KIS ( keep it simple )

[/quote]
Do NOT redeclare variables like that, there is absolutely no reason to do $box1 = $_POST['box1'];, just use $_POST['box1'].  You're creating more code and more variables that don't need to be there and over complicate the code.  Other than the basic fact of the processing of more variables, if someone comes in to read $box2, they don't know where it came from, but if they see (or you see later on after you haven't worked on it for quite some time) $_POST['box2'], you know where the variable is coming from.
Link to comment
Share on other sites

would you mind explaing why I should never do that besided my memory mabey forgettign the variable ? because it's not very difficult to search for $box1 and find the place it is defined at the top of the page 

Ofcourse there is no reason you HAVE to do it , however I can't see where the problem with doign it is  I got into the habbit because it makes things neater for me when inserting items into my database and have never had any issues however IF there is something actually wrong with doign it this way and not just ease of following the script ( as I find my way easier when coding ) could you please explain it to me  so that I can fix the error in my habits :)

to the poster of the thread  :  If you skip that first step ( which is needless it's jsut habbit and tidyness on my part ) then the value's of your form can be found in 

$_POST['box3']  $_POST['box1']  $_POST['box2'] 

and same principle applies you can put em anywhere


Link to comment
Share on other sites

[quote author=Fallen_angel link=topic=116888.msg476761#msg476761 date=1164949411]
would you mind explaing why I should never do that besided my memory mabey forgettign the variable ? because it's not very difficult to search for $box1 and find the place it is defined at the top of the page 

Ofcourse there is no reason you HAVE to do it , however I can't see where the problem with doign it is  I got into the habbit because it makes things neater for me when inserting items into my database and have never had any issues however IF there is something actually wrong with doign it this way and not just ease of following the script ( as I find my way easier when coding ) could you please explain it to me  so that I can fix the error in my habits :)

to the poster of the thread  :  If you skip that first step ( which is needless it's jsut habbit and tidyness on my part ) then the value's of your form can be found in 

$_POST['box3']  $_POST['box1']  $_POST['box2'] 

and same principle applies you can put em anywhere
[/quote]
It depends on the type of data, but it's a matter of efficiency really.  You're creating extra work and defeating the purpose really having register_global Off (for the most part).  I do a lot of work with high-traffic sites, so getting very optimized and efficient is a must.  It's generally just a bad practice to get into, and you open yourself up to problems where you've got $box1 = $_POST['bxo1']; and little things like that.  More lines of code = more debugging time when you have a problem.  PHP isn't making a reference to $_POST['box1'] when you do that, it's making a copy of the data as well.  It's not a HORRIBLE practice, and you could get away with it, especially if you're just doing simple little things for personal or small time use, but you should try to stay away from doing it that way.  Not everyone is always going to follow what is considered the standard, but if everyone tries to follow a few similar things then it makes it better as a community.  When you want code debugged or put it up for someone else to take a look at, it makes it a lot easier for the rest of us as well.
Link to comment
Share on other sites

still can't fix it! think i got problem with the name of the radio buttons and the way i handle them.

<input type=radio name=rb1 value=0>
<input type=radio name=rb1 value=1>
.
.
.
<input type=radio name=rbN value=0>
<input type=radio name=rbN value=1>

i handle them like this

for ( $cnt=1; $cnt<N; $cnt++ )
{
    if ( isset ( $_POST['rb$cnt'] ) )
    {...}
}

should i do something like $_POST['rb$cnt']['$cnt'] like when having an array of checkboxes?

p.s. Fallen Angel i got this habbit too. easy when coding but projectshifter is right...
Link to comment
Share on other sites

SOLVED! the problem was somewhere else in the code... I'm not telling you where because you will laugh with me till xmas...

ok ok after the isset($_POST... bla bla bla) i supposed to use explode() somewhere but i forgot to write --explode-- so i got something like this

$var = (".",$var);...

OOPS! sorry guys!  :-[ :-[ :-[
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.