How to display multilevel dynamic form field in PHP? - php

My HTML Form, generated by PHP from MySQL database:
`
<!-- 1st product -->
<input name="product[]" value="Speaker" />
<!-- 3 components & its values of 1st product -->
<input name="component_name[]" value="Color" />: <input name="value_name[]" value="Black" />
<input name="component_name[]" value="Diameters" />: <input name="value_name[]" value="10" />
<input name="component_name[]" value="Brand" />: <input name="value_name[]" value="Pokijan" />
<!-- 2nd product -->
<input name="product[]" value="Desktop PC" />
<!-- 6 components & its values of 2nd product -->
<input name="component_name[]" value="Brand" />: <input name="value_name[]" value="Tulkiyem" />
<input name="component_name[]" value="OS" />: <input name="value_name[]" value="Linux" />
<input name="component_name[]" value="Processor" />: <input name="value_name[]" value="Intel " />
<input name="component_name[]" value="RAM" />: <input name="value_name[]" value="16 GB" />
<input name="component_name[]" value="HDD" />: <input name="value_name[]" value="-" />
<input name="component_name[]" value="SSD" />: <input name="value_name[]" value="1600 GB" />
<input type="submit" value="Send" />
</form>`
CONDITIONS:
The words "Speaker", "Desktop PC" are take from mysql product category table
The words "Colors", "Diameters", "Brand", "RAM", etc are taken from product component table
I want to create unlimited components and its value under each product/category
I want to post the form-datas into 2 different MySQL tables: transaction & transaction_components.
THE QUESTION ARE, (USING PHP):
How to post it all into correct array like this (think I'm right):
$product=array("Speaker"=> ["color"=>"Black", "Diameters"=>"10", "Brand"=>"Pokijan"], "Desktop PC"=>["Brand"=>"Tulkiyem", "OS"=>"Linux", "Processor"="Intel", "RAM"=>"16 GB", "HDD"=> "-", "SSD"=>"1600 GB"]);
OR
Display the data like this:
` Speaker:
color: Black
Diameters: 10
Brand: Pokijan
Desktop PC
Brand: Tulkiyem
OS: Windows
Processor: Intel
RAM: 16 GB
HDD: -
SSD: 1600 GB
`
This doesn't work for me:
`<?php
$cid=$_POST['cid'];
$komponen=$_POST['komponen'];
$opsi=$_POST['opsi'];
echo "<ul>";
$jb=count($cid);
for($i=0; $i<$jb;$i++)
{
if($cid[$i]<>"")
{
$jk=count($komponen);
echo "<li>Product-$i: $cid[$i]
<ol>";
for($x=0;$x<$jk; $x++)
{
echo "<li>$komponen[$x]: $opsi[$i][$x]</li>";
}
echo "</ol></li>";
}
}
echo "</ul>";
?>`

Instead of trying to heavily reformat upon submission, you should make sure you get as close as possible to your desired array structure when generating the form. You can't get exactly what you're looking for, but explicitly generating the keys in the names like this:
<input name="products[0][name]" value="Speaker" />
<!-- 3 components & its values of 1st product -->
<input name="products[0][component_names][0]" value="Color" />: <input name="products[0][component_values][0]" value="Black" />
<input name="products[0][component_names][1]" value="Diameters" />: <input name="products[0][component_values][1]" value="10" />
<input name="products[0][component_names][2]" value="Brand" />: <input name="products[0][component_values][2]" value="Pokijan" />
<!-- 2nd product -->
<input name="products[1][name]" value="Desktop PC" />
<!-- 6 components & its values of 2nd product -->
<input name="products[1][component_names][0]" value="Brand" />: <input name="products[1][component_values][0]" value="Tulkiyem" />
<input name="products[1][component_names][1]" value="OS" />: <input name="products[1][component_values][1]" value="Linux" />
<input name="products[1][component_names][2]" value="Processor" />: <input name="products[1][component_values][2]" value="Intel" />
<input name="products[1][component_names][3]" value="RAM" />: <input name="products[1][component_values][3]" value="16 GB" />
<input name="products[1][component_names][4]" value="HDD" />: <input name="products[1][component_values][4]" value="-" />
<input name="products[1][component_names][5]" value="SSD" />: <input name="products[1][component_values][5]" value="1600 GB" />
will produce an array like this:
array(
'products' => array(
0 => array(
'name' => 'Speaker',
'component_names' => array(
0 => 'Color',
1 => 'Diameters',
2 => 'Brand',
),
'component_values' => array(
0 => 'Black',
1 => '10',
2 => 'Pokijan',
),
),
1 => array(
'name' => 'Desktop PC',
'component_names' => array(
0 => 'Brand',
1 => 'OS',
2 => 'Processor',
3 => 'RAM',
4 => 'HDD',
5 => 'SSD',
),
'component_values' => array(
0 => 'Tulkiyem',
1 => 'Linux',
2 => 'Intel',
3 => '16 GB',
4 => '-',
5 => '1600 GB',
),
),
),
)
Performing the output now becomes much easier:
<ol>
<?php foreach ($_POST['products'] as $product): ?>
<li><?= $product['name'] ?></li>
<ul>
<?php foreach ($product['component_names'] as $key => $componentName): ?>
<li><?= $componentName.': '.$product['component_values'][$key] ?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
</ol>
Since we've explicitly defined the keys, we know the value corresponding to the component name will be at the same key under 'component_values'.
Also note that I have used the alternative syntax to output HTML, as this allows IDEs and advanced text editors to properly highlight your HTML elements and offer autocomplete values for them, unlike printing the HTML as a string.

Related

How to fill out a form using PHP and XPatch

I have this HTML code and I would like it to be automatically filled with parameters taken from the database/table:
Array
(
[0] => Array
(
[id] => 35
[nr_wyceny] => W/8/01041122/2021
[nazwa_firmy] => Test
[adres_firmy] => testowy
[kraj_firmy] => polska
[telefon_firmy] => 79280xxxx
[osoba_firmy] => ja
[email_firmy] => pow#d.pl
[data_inwestycji] => 12/12/2020
[wycena_wstepna] =>
[wycena_zrzutem] => on
[jakosc_niska] => on
[jakosc_srednia] =>
[jakosc_wysoka] =>
[wygoda_mala] => on
[wygoda_srednia] =>
[wygoda_maksymalna] =>
[cena_niska] => on
[cena_srednia] =>
[cena_wysoka] =>
[wydajnosc_niska] =>
[wydajnosc_srednia] =>
[wydajnosc_wysoka] => on
[srodki_wlasne] => on
[finansowanie_zewnetrzne] =>
[finansowanie_niepodane] =>
[dzialalnosc_uslugowa] => on
... //more data
The point is that I download an array with the names and values of input fields from the database, if there is a checkbox then the value is on or null if there is input type text then it should have a value, but I don't know how to fill in the form using XPatch - possibly some other method to fill it this form, without rewriting the code. I have a lot of values to fill in and would like to do it automatically. i know how to do it the other way, but i would like to use xpatch and a DOM parser
<form action="/xxxxxx.php" method="post" id="xForm" >
<div class="tabela">
<div id="sekcja1" class="sekcja">
<div class="headerx"><p><b>Dane klienta</b></p></div>
<p>Nazwa firmy: <input class="xForm" type="text" name="nazwa_firmy" value=""/>
<p>Adres: <input class="xForm" type="text" name="adres_firmy" value=""/>
<p>Kraj: <input class="xForm" type="text" name="kraj_firmy" value=""/>
<p>Telefon kontaktowy: <input class="xForm" type="text" name="telefon_firmy" value=""/>
<p>Osoba do kontaktu: <input class="xForm" type="text" name="osoba_firmy" value="" />
<p>Email: <input class="xForm" type="text" name="email_firmy" value=""/>
<p><b>Planowany termin realizacji inwestycji: <input class="xForm" type="text" name="data_inwestycji" id="datepicker" value=""/></b>
</div>
<div id="sekcja2" class="sekcja">
<div class="headerx"><p><b>Dane na temat formy preferowanej wyceny:</b></p></div>
<p>Wstępna wycena <input class="xForm" type="checkbox" name="wycena_wstepna" checked="" />
<p>Wycena docelowa z rzutem urządzeń <input class="xForm" type="checkbox" name="wycena_zrzutem" checked="" />
<br />
<!---// More form data more input fields //--->
</form>
My PHP code is:
<?php
$url = file_get_contents('/wycena.html');
$url = mb_convert_encoding($url, 'utf-8', mb_detect_encoding($url));
$url = mb_convert_encoding($url, 'html-entities', 'utf-8');
$dom = new DOMDocument();
$dom->loadHTML($url);
$xpath = new DOMXpath($dom);
$html = $dom->saveHTML();
$dom->loadHTML($html);
// in this case i have fill only one field of input by name "nazwa_firmy" //and value "testtest" only for testing
//=======================================================
$key="nazwa_firmy"; //name of input element - test data
$val="testest"; //value of input element - test data
//=======================================================
$item = $xpath->query('//input[#name = "'.$key.'"]/#type');
if($item[0]->nodeValue=="text"){
$itemx = $xpath->query('//input[#name = "'.$key.'"]/#value');
}
if($item[0]->nodeValue=="checkbox"){
$itemx = $xpath->query('//input[#name = "'.$key.'"]/#checked');
}
$itemx[0]->nodeValue = $val;
echo $html = $dom->saveHTML();
?>
Why is it not possible to fill in the form in this way, is there any method to fill it in?
If you aleady have $array containing the values, just echo them inside the value attrubute of the form: -
<p>Nazwa firmy: <input class="xForm" type="text" name="nazwa_firmy" value="<?= $array [nazwa_firmy] ?>"/>
<p>Adres: <input class="xForm" type="text" name="adres_firmy" value="<?= $array [adres_firmy] ?>"/>
<p>Kraj: <input class="xForm" type="text" name="kraj_firmy" value="<?= $array [kraj_firmy] ?>"/>
<p>Telefon kontaktowy: <input class="xForm" type="text" name="telefon_firmy" value="<?= $array [telefon_firmy] ?>"/>
<p>Osoba do kontaktu: <input class="xForm" type="text" name="osoba_firmy" value="<?= $array [osoba_firmy] ?>" />
<p>Email: <input class="xForm" type="text" name="email_firmy" value="<?= $array [email_firmy] ?>"/>
<p><b>Planowany termin realizacji inwestycji: <input class="xForm" type="text" name="data_inwestycji" id="datepicker" value="<?= $array [data_inwestycji] ?>"/></b>
</div>

php inserting multiple radio values with same name

I'm trying to insert multiple values using radio, here is my example:
<input type="radio" name="toppingPrice[]" value="<?= $topping['name'];?>-<?= $topping['price'];?>">
this one work if I insert single input, but if I want to insert more than one for example:
Size: small, medium, large <- name="toppingPrice[]" for all input values
Cheese: yes, no <- name="toppingPrice[]" for all input values
Level: spicy, normal <- name="toppingPrice[]" for all input values
this will not work because it will merge into 1 group so if I have to choose only one of all toppings.
my original code looks like:
foreach ($toppingPrice as $key) {
list($toppingName,$toppingNameEn, $toppingPrice) = explode("-",$key,3);
$tName[] = $toppingName;
$tNameEn[] = $toppingNameEn;
$tPrice += $toppingPrice;
}
$tn = implode(",", $tName);
$tn_en = implode(",", $tNameEn);
$price = $price + $tPrice;
Html:
<input type="checkbox" name="toppingPrice[]" id="<?= $topping[0];?>" value="<?= $topping['name'];?>-<?= $topping['name_en'];?>-<?= $topping['price'];?>" <? if($topping['price'] < 1){echo "checked";}?>>
I hope I delivered the question in the right way
please give me any idea or solution for fix this issue
You should use name attribute as shown in below code.
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
<form name="test" method="post">
<input type="radio" name="toppingPrice.size[]" value="1"> 1
<input type="radio" name="toppingPrice.size[]" value="2"> 2
<input type="radio" name="toppingPrice.size[]" value="3"> 3
<br>
<input type="radio" name="toppingPrice.toping[]" value="4"> 4
<input type="radio" name="toppingPrice.toping[]" value="5"> 5
<input type="radio" name="toppingPrice.toping[]" value="6"> 6
<br>
<input type="radio" name="toppingPrice.level[]" value="7"> 7
<input type="radio" name="toppingPrice.level[]" value="8"> 8
<input type="radio" name="toppingPrice.level[]" value="9"> 9
<button type="submit" value="save" /> Save
</form>
This will be your $_POST after form submit
Array
(
[toppingPrice_size] => Array
(
[0] => 1
)
[toppingPrice_toping] => Array
(
[0] => 5
)
[toppingPrice_level] => Array
(
[0] => 9
)
)

Array displaying only one record

Friends,
I apologize in advance for posting this question again.
I got great help from Barmar in resolving the problem I was having with displaying array results to users.
The error issue was resolved and result was being displayed.
Problem is the array displays one record.
When we insert multiple rows of records, only one record is displayed.
Does anyone know what could be wrong?
Basically, by default, a user is presented with one row of textboxes.
If user needs to add additional records, the user clicks the Add button to dynamically add an additional row of textboxes,
The code below is from a page called preview.php. It is supposed to capture all records entered in markup page by the user and displays them so user will have the option to review the records and go back to make changes if needed or submit records if everything is ok.
So far as indicated above, it displays only one record regardless of whether the user created one row or multiple rows.
We would like to capture all rows.
Any ideas what I might be missing with code below?
I apologize in advance. I have to show this tomorrow at work and it has occupied my entire weekend.
<?php
//echo "DEBUG POST DATA: <pre>".print_r($_POST, 1)."</pre>";
if(isset($_POST['employeename']))
$employeename = $_POST['employeename'];
if(isset($_POST['ttitle']))
$ttitle = $_POST['ttitle'];
echo $employeename .'<br>';
echo $ttitle .'<br> <hr width=400 align=left>';
$rowIDs = $_POST['rowIDs'];
for ($id = 0; $id < $rowIDs; $id++){
$sourcename1 = $_POST['sourcename1'][$id];
$sourceaddress1 = $_POST['sourceaddress1'][$id];
$income1 = $_POST['income1'][$id];
echo $sourcename1 .'<br>';
echo $sourceaddress1.'<br>';
echo $income1.'<br>';
}
?>
DEBUG POST DATA:
Array
(
[employeename] => Catherine Duffy
[ttitle] => Sr. Systems Analyst
[rowIDs] => 1
[sourcename1] => Array
(
[0] => Mark
Zverkov
)
[sourceaddress1] => Array
(
[0] => Address1
)
[income1] => Array
(
[0] => $79,000.00
)
[sourcename13] => Jim Brown
[sourceaddress13] => 32 Xooker Rd
[income13] => $99,000.00
[spousename] =>
[spouseAddress] =>
[spouseIncome] =>
[dividentname] =>
[dividentaddress] =>
[dividentAmt] =>
[reimbursmentName] =>
[reimburseAddr] =>
[remursementAmt] =>
[inputHonoraria] =>
[giftname] =>
[giftaddress] =>
[giftamount] =>
[orgname] =>
[orgaddresss] =>
[donationamt] =>
[creditorname] =>
[creditoraddress] =>
[creditAmt] =>
[email] =>
[submitted] => true
)
Catherine Duffy
Sr. Systems Analyst
Mark Zverkov
Address1
$79,000.00
//Markup
<script id="row-template" type="text/x-handlebars-template">
<div>
<!--reseed attribute IDs in case of gap resulting from deletions -->
<input type="hidden" name="rowIDs[]" value="{{rowNumber}}" />
<div class="form-group">
<label for="sourcename{{rowNumber}}">Name</label><br>
<input type="text" name="sourcename1{{rowNumber}}" id="sourcename1{{rowNumber}}" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<div class="form-group">
<label for="sourceaddress1{{rowNumber}}">Address</label><br>
<input type="text" name="sourceaddress1{{rowNumber}}" id="sourceaddress1{{rowNumber}}" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<div class="form-group">
<label for="income1{{rowNumber}}">Income</label><br>
<input type="text" style="width:250px;" class="form-control" name="income1{{rowNumber}}" id="income1{{rowNumber}}" value="<?php if(isset($_POST['spouseIncome{{rowNumber}}'])) echo $_POST['spouseIncome{{rowNumber}}'];?>" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<input id="Button{{rowNumber}}" type="button" rel="remove-row" value="Remove" />
</div>
</script>
<div id="addrow">
<div>
<!--reseed attribute IDs in case of gap resulting from deletions -->
<input type="hidden" name="rowIDs[]" value="{{rowNumber}}" />
<div class="form-group">
<label for="sourcename">Name</label><br>
<input type="text" name="sourcename1[]" id="sourcename1" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<div class="form-group">
<label for="sourceaddress1">Address</label><br>
<input type="text" name="sourceaddress1[]" id="sourceaddress1" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</div>
<div class="form-group">
<label for="income1">Income</label><br>
<input type="text" name="income1[]" id="income1" style="width:250px;" class="form-control" value="" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
<input type="button" value="Add More" rel="add-row" />
</div>
</div>
</div><br><br>
From the code snippets and debug you posted, your PHP code appears to be expecting the data in a different format than your HTML form is generating. It's clear that you have a lot going on with the form, I might suggest starting a new, blank page and re-creating the form piece by piece to make sure each piece submits correctly before you add the next block of fields.
Specific to the question of receiving multiple records from the form, if you format your HTML for the part of the form that gets repeated (assuming {{ rowNumber }} gets replaced by 1, 2, 3,... each time a new one is added) like this:
<div class="this-div-gets-repeated">
<label>
Source Name:
<input type="text" name="source[{{ rowNumber }}][name]"/>
</label>
<label>
Source Address:
<input type="text" name="source[{{ rowNumber }}][address]"/>
</label>
<label>
Income:
<input type="text" name="source[{{ rowNumber }}][income]"/>
</label>
</div>
They you can expect to parse it in PHP like this:
<?php
$sources = isset($_POST['source']) ? $_POST['source'] : []; // just in case no sources are posted
foreach ($_POST['source'] as $id => $source) {
$sourcename = $source['name'];
$sourceaddress = $source['address'];
$income = $source['income'];
echo "$sourcename<br>$sourceaddress<br>$income<br>";
}
Edit: further comments on your HTML
Your HTML form has two blocks: a set of fields for the first "record", and then a repeating template for any additional records.
The first issue with your HTML is that your "first record" form fields have different names than your "more records" form fields:
Your "first record" form fields have fields like <input type="text" name="sourcename1[]"/>.
Your "more records" form fields have fields like <input type="text" name="sourcename1{{rowNumber}}"/>.
If you were to look at the HTML that gets generated once you've clicked "Add More" a couple times, your HTML would look like this:
<!-- the default "first record" -->
<input type="text" name="sourcename1[]"/>
<!-- from the first click on "Add More" -->
<input type="text" name="sourcename11"/>
<!-- from the second click on "Add More" -->
<input type="text" name="sourcename12"/>
From that you can see how the values entered into the "first record" fields will appear differently than the values from the dynamically-added "add more" fields?
It also appears that there's some weirdness happening with your Angular templates: your PHP $_POST contains the values from your "first record" fields (in [sourcename1] => Array), and there are values from your "add more" fields (in [sourcename13] => Jim Brown where {{rowNumber}} was 3), but are you intentionally starting {{rowNumber}} at 3 in Angular?

Get value of input where checkbox[] is checked

I've a problem, i need to get a value of an input (text) when his checkbox is selected.
<form method=GET>
<input type =checkbox name = checkbox[]>
<input type = 'text' name=? >
<input type =checkbox name = checkbox[]>
<input type ='text' name=? >
<input type =checkbox name = checkbox[]>
<input type = 'text' name=? >
<input type = 'submit' name='Submit' >
</form>
This is what I want excactly
If(checkbox == checked)
{
Echo the entered input text value
}
Thanks inadvance.
Checkbox and radio type field does not get submit when they are not checked or selected.
So you need to check the get/request object whether checkbox name is there or not in the object.
Like
if(isset($_GET['checkbox'])){
}
When you check checkbox you can get value of next input box.
$("input:checkbox").change(function(){
if(this.checked){
alert($(this).next("input").val());
}
});
Demo
I am assuming you meant after submission to a PHP script.
I just threw this together and have not tested it yet, but looks fairly straight forward.
It will also work with <input type=checkbox name="c[]">, but just a little apprehensive about the text and checkbox synchronization with the array.
HTML
<form action="./chkbx.php" method=GET>
<input type=checkbox name="c1" value="1"/>
<input type="text" name="t1" />
<input type=checkbox name="c2" value="2"/>
<input type="text" name="t2" />
<input type=checkbox name="c3" value="3"/>
<input type="text" name="t3" >
<input type="submit" name='Submit' />
</form>
PHP
foreach ($_GET as $key => $val){
$chk[substr($key,0,1)] = intval(substr($key,1,1));
$txt[substr($key,0,1)][intval(substr($key,1,1))] = $val;
}
echo '<h2>Text=' . $txt['t'][$chk['c']] . '<h2>';
Test Code
<?php
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Menu Test</title><meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style type="text/css">
</style></head><body>
<form action="./chkbx.php" method=GET>
<input type=checkbox name="c1" value="1"/>
<input type="text" name="t1" />
<input type=checkbox name="c2" value="2"/>
<input type="text" name="t2" />
<input type=checkbox name="c3" value="3"/>
<input type="text" name="t3" >
<input type="submit" name='Submit' />
</form>
EOT;
foreach ($_GET as $key => $val){
$chk[substr($key,0,1)] = intval(substr($key,1,1));
$txt[substr($key,0,1)][intval(substr($key,1,1))] = $val;
}
echo '<h2>' . $txt['t'][$chk['c']] . '<h2>';
echo '<pre>';
var_export($chk);
echo"-------------------\n";
var_export($txt);
echo '</pre></body></html>';
Results:
With checkbox c2 checked and text boxes containing "Text One" "Text Two" "Text Three"
Below output from is the echo '<h2>Text=' . $txt['t'][$chk['c']] . '<h2>';
Text Two
$chk (
't' => 3,
'c' => 2,
'S' => 0,
)-------------------
$txt (
't' =>
array (
1 => 'Text One',
2 => 'Text Two',
3 => 'Text Three',
),
'c' =>
array (
2 => '2',
),
'S' =>
array (
0 => 'Submit',
),
)
Summary:
The submitted check box value is stored in $chk['c']
The submitted Text is in $txt['t'][0], $txt['t'][2], $txt['t'][3] respectfully.
So the text can be retrieved by $txt['t'][$chk['c']
Loop and echo executed in 0.000054 seconds. 54 micro seconds, not too bad.
The value of checkbox only returns boolean (i.e. true of false).
You should use
if(isset($_GET["checkbox"]))
instead of
if(checkbox == checked)

How to handle $post_[] on all inputs

What's the ideal way to handle an input form with multiple values, without going through each $POST_['inputname'] individually?
For example, let's say I have an invite form like this:
<form id="input-form" action="" method="post" >
<input id="email_1" name="email_1" />
<input id="role_1" name="role_1" />
<input id="email_2" name="email_2" />
<input id="role_2" name="role_2" />
<input id="email_3" name="email_3" />
<input id="role_3" name="role_3" />
</form>
Ideally, I'd like to create two arrays with the data, one for the email addresses and another for the roles
The resulting arrays would look like this:
$emailArray = array(
'email1' => "emailOneData",
'email2' => "emaiTwoData",
'email3' => "emaiThreeData",
);
$RoleArray = array(
'role1' => "roleOneData",
'role2' => "roleTwoData",
'role3' => "roleThreeData",
);
Not sure what the best way of splitting the data is though. Also I don't really want to do something like this:
$_POST['email_1']
$_POST['email_2']
$_POST['email_3']
.....
Use instead an $_POST Array.
<form id="input-form" action="" method="post" >
<input id="email_1" name="email[]" />
<input id="role_1" name="role[]" />
<input id="email_2" name="email[]" />
<input id="role_2" name="role[]" />
<input id="email_3" name="email[]" />
<input id="role_3" name="role[]" />
</form>
After this you got an array like this:
echo'<pre>';
print_r($_POST['email']);
print_r($_POST['role']);
echo'</pre>';
Of course you still have to escape your input in dependency of your use.
After this you can use a normal foreach on the array.
$mails=array();
$count=count($_POST['role']);
for($i=0;$i<$count;$i++){
$mails["email_".$i]=$_POST['role'][$i];
}

Categories