?
Solved

Getting the value a user selected from a list of radio buttons

Posted on 2008-10-02
15
Medium Priority
?
257 Views
Last Modified: 2011-10-19
Hi everyone i hope you are all well.
Guys i have 2 php files.
1) form.php
2) processing.php

========================================================= form.php

<form>
<form name="input" action="processing.php" method="post">
<input type="radio" name="sex" value="male"> Male
<br>
<input type="radio" name="sex" value="female"> Female
<input type="submit" value="Submit">
</form>

======================================================== processing.php
currently blank.

##########
QUESTION:
##########
This is a really simple example, but i have another scenario im currently working on that is having trouble receiving the correct value a user selected from a list of radio buttons. In that example, it is always getting the value of the LAST sql record returned, so im obviously doing something wrong there.

What id like to know is what is the CORRECT way to get the value of the radio button selected in processing.php?

I know in processing.php, I could do the following:

$getradiobuttonselected = $_POST['name'],
but i have tried this on my other "scenario" and it always gets the wrong value.

Any help greatly appreciated.
0
Comment
Question by:Simon336697
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
15 Comments
 
LVL 17

Expert Comment

by:HuyBD
ID: 22622277
Try this
$getradiobuttonselected = $_POST['sex'];

Open in new window

0
 
LVL 17

Expert Comment

by:HuyBD
ID: 22622292
and remove form tag in first line
<form name="input" action="processing.php" method="post">
<input type="radio" name="sex" value="male"> Male
<br>
<input type="radio" name="sex" value="female"> Female
<input type="submit" value="Submit">
</form>

Open in new window

0
 
LVL 17

Expert Comment

by:HuyBD
ID: 22622304
in processing.php file, check whether sex is select or not
if(isset($_POST['sex']))
     $getradiobuttonselected = $_POST['sex'];

Open in new window

0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 1

Author Comment

by:Simon336697
ID: 22622309
Hi HuyBD,
Thank you SO MUCH for your kind help.
HuyBD, I thought I always needed to use the form tag with an input element, since how can I post to another file without it?
Or am i missing something silly.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22622318
HuyBD,
If i dont check that that is selected, could that explain why in my other "scenario", it always gets the last sql record?

In that case, I am doing an if isset statement also.
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 22622334
If you dont check, the value of sex will be zero, that will take all record of zero may be female
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22622347
HuyBD, this question is just to get clarification on my other scenario, which im doing the following...


================================================================== showtasks.php
<?php while($rows_view_tasks=mysql_fetch_array($result_view_tasks))    {  ?>
          <form name="formedit" method="post" action="edit_task.php">            
                    <input type="radio" name="radioedittask" value="<?php echo $rows_view_tasks['id_ctk']; ?>">
                   <input type="hidden" name="hiddendescription" value="<?php echo htmlspecialchars($rows_view_tasks['description_ctk']); ?>">
<?php       }          ?>      
                   <input id="edittaskbutton" type="submit" name="Submit" value="Edit">
          </form>

================================================================== edit_task.php
if (isset($_POST['radioedittask']) && isset($_POST['hiddendescription'])) {      
                  
      $grab_radioedittask = $_POST['radioedittask'];            
      $grab_hiddendescription = $_POST['hiddendescription'];
      echo "The value of \$grab_hiddendescription is: " . $grab_hiddendescription;

THE PROBLEM ABOVE IS:
The $grab_hiddendescription value.
IT IS ALWAYS THE LAST RECORD RETURNED IN THE MYSQL FETCH OF RECORDS and i dont know why.

0
 
LVL 1

Author Comment

by:Simon336697
ID: 22622355
In the above, Im trying to get the hiddendescription value for the record which the user has selected by clicking on that particular RADIO BUTTON
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22622384
Here is a printscreen dump mate :>)
ee-problem.jpg
0
 
LVL 17

Accepted Solution

by:
HuyBD earned 2000 total points
ID: 22622401
It's not clear, try use javascript to assign value to hidden field because of this is single value
================================================================== showtasks.php
<script language="javascript">
function changethis(val)
{
document.formedit.hiddendescription.value=val;
}
</script>
<form name="formedit" method="post" action="edit_task.php">            
<?php while($rows_view_tasks=mysql_fetch_array($result_view_tasks))    {  ?>
                    <input type="radio" name="radioedittask" value="<?php echo $rows_view_tasks['id_ctk']; ?>" onchange="changethis(<?php echo htmlspecialchars($rows_view_tasks['description_ctk']); ?>)">
                   <input type="hidden" name="hiddendescription" value="">
<?php       }          ?>      
                   <input id="edittaskbutton" type="submit" name="Submit" value="Edit">
          </form>
 
================================================================== edit_task.php
if (isset($_POST['radioedittask']) && isset($_POST['hiddendescription'])) {      
                  
      $grab_radioedittask = $_POST['radioedittask'];            
      $grab_hiddendescription = $_POST['hiddendescription'];
      echo "The value of \$grab_hiddendescription is: " . $grab_hiddendescription;

Open in new window

0
 
LVL 1

Author Comment

by:Simon336697
ID: 22622540
Hi HuyBD,
Mate im working on this now thank you so much..... will report back shortly.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22623063
Hi HuyBD...mate sorry for the delay......i will report back after testing tomorrow...thank you so much for your patience mate.
0
 
LVL 1

Author Comment

by:Simon336697
ID: 22666750
Hi HuyBD,
Firstly a HUGE THANKYOU for your kind answers.
Secondly, a BIG apology to you for the delay......
I have not had time to look at this seriously and will give you the points now and return to it later.
Sorry for the delay I dont usually leave questions unanswered for this long.
Really appreciate your help.
Take care.
Simon
0
 
LVL 1

Author Closing Comment

by:Simon336697
ID: 31502293
Thanks again HuyBD
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 22666760
you're welcome :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question