Solved

PHP Radio Array

Posted on 2016-09-26
4
37 Views
Last Modified: 2016-10-11
Hi,

I have a DB query thats pulling many rows from a table, each row has an id and an email address, firstname and lastname, I'm trying to print out the query showing each row, then once i have this let the user select 1 of 3 radios buttons,
1. for wipes
2. nappies
3. both

once they have selected for each row then submit the form, if they have choosen radio button 1 i want to run one selection of code, if they have selected radio button 2 for nappies i want to run another selection or code for those rows.

the code below shows what i'm printing out for each row from the db, but how do I do a foreach when submited and break them apart, i'll need the ID or the email address passed through to know who going for what option ?, they can only select one option per row so i'm using radio buttons.

<form name='form1' id='form1' method='post' action=''>
        <?php while($row = mysql_fetch_array($retval, MYSQL_ASSOC)){ ?>
        <div>
          <div style="float:left; width:160px"><?php echo $row['child_firstname'] . " " . $row['child_lastname']; ?></div>
          <div style="float:left; width:100px">
             <input type="radio" name="item[<?php echo $row['id']; ?>]" value="wipes">
          </div>
          <div style="float:left; width:100px">
            <input type="radio" name="item[<?php echo $row['id']; ?>]" value="nappies">
          </div>
          <div style="float:left; width:100px">
            <input type="radio" name="item[<?php echo $row['id']; ?>]" value="both">
          </div>
          <div style="clear:both"></div>
        </div>
        <?php } ?>

Open in new window


Thanks Simon
0
Comment
Question by:pc-buddy
  • 2
4 Comments
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41817270
Not sure I have understand the requirement exactly but you can try this.
$item = isset($_POST['item']) ? $_POST['item'] : false;
if (is_array($item)) {
  foreach($item as $rowselection) {
    switch($rowselection) {
      case 'wipes':
        handleWipes();
      break;
      case 'nappies':
        handleNappies();
      break;
      default:
        handleBoth();  
      break;
    }
  }
}
function handleNappies() 
{
   // Handle Nappie code here
}

function handleWipes() 
{
   // Handle Wipes code here
}

function handleBoth() 
{
   // Handle Both code here
}

Open in new window

0
 

Author Comment

by:pc-buddy
ID: 41818613
Hi Julian,

I'll go have a play, thanks :)
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 41828405
For this demo I stripped out all of the parts that are not relevant to processing an array of radio buttons in a post request.   Feel free to experiment with it on my server.  HTH, ~Ray
https://iconoun.com/demo/temp_pc_buddy.php
<?php // demo/temp_pc_buddy.php
/**
 * https://www.experts-exchange.com/questions/28972537/PHP-Radio-Array.html
 */
error_reporting(E_ALL);

// SHOW THE POST ARRAY
var_dump($_POST);

// USE THE POST ARRAY
if (!empty($_POST))
{
    foreach ($_POST['item'] as $key => $value)
    {
        echo PHP_EOL . "<br>POST ITEM $key POINTS TO '$value' ";
    }
}

// CREATE THE FORM USING HEREDOC NOTATION
$form = <<<EOD
<form method='post'>

<input type="radio" name="item[1]" value="wipes">
<input type="radio" name="item[1]" value="nappies">
<input type="radio" name="item[1]" value="both">
<br>

<input type="radio" name="item[2]" value="wipes">
<input type="radio" name="item[2]" value="nappies">
<input type="radio" name="item[2]" value="both">
<br>

<input type="submit" />
</form>
EOD;

echo $form;

Open in new window

0
 

Author Closing Comment

by:pc-buddy
ID: 41837996
Awesome help, thanks guys
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 …

707 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now