Solved

PHP Radio Array

Posted on 2016-09-26
4
62 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
[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
  • 2
4 Comments
 
LVL 56

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 110

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

730 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