Solved

PHP Radio Array

Posted on 2016-09-26
4
49 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 54

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

773 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