I got this 2 array in a form to be process. However, i only manage to get the output from only one of the array. Sample as below :
<inputs id="location" type="text" name="data[]" value=""/>
<input id="shipval" type="text" name="data[][id]" value=""/>
And in the PHP part is below :
foreach ($_POST ["data"] as $id => $subs) {
foreach ($subs as $key=>$sub) {
$subcategory = $sub;
if($subs['id']=="$subcategory"){
echo $sql = " insert into x(kodLebuhraya,kodSeksyen) values ('".$subs['id']."','".$sub."')";echo "<br>";
}else{
//echo "hi2";
echo $sql = " insert into x(kodLebuhraya,kodSeksyen) values ('".$subs['id']."','".$sub."')";echo "<br>";
}
}
}
It means one location for one shipval. i have multiple input field for location and shipval. Can you guys enlight me which one is wrong. Thanks in advanced.
So basically you need to pass location and shipval in pairs.
Try this structure in HTML:
<label>Set One</label>
<input class="location" type="text" name="data[location][]" value=""/>
<input class="shipval" type="text" name="data[shipval][]" value=""/>
<label>Set Two</label>
<input class="location" type="text" name="data[location][]" value=""/>
<input class="shipval" type="text" name="data[shipval][]" value=""/>
<label>Set Three</label>
<input class="location" type="text" name="data[location][]" value=""/>
<input class="shipval" type="text" name="data[shipval][]" value=""/>
And this code for PHP:
foreach ($_POST['data']['location'] as $key => $location) {
$shipVal = $_POST['data']['shipval'][$key];
//now you have a pair of $location and $shipVal
echo $location.' : '.$shipVal.'<hr>';
}
Avoid using named indexes after unnamed ones ex. <input name="array[][named]" />
you can lose order of fields if one of pair fields is empty.
You have <inputs id="location"
instead of <input id="location"
Also... foreach ($subs as $key => $sub) {
will throw an error for <inputs id="location" type="text" name="data[]" value=""/>
because it is not multidimensional. So try changing that to <inputs id="location" type="text" name="data[][]" value=""/>
For multiple select, the name has to end in square brackets, so you'll need to change the name of your shipval
inputs
Firstly you have writtten inputs
instead of input
.
Secondly, this line:
foreach ($subs as $key=>$sub) {
will treat each variable as an array, but the location isn't an array.
I did not see the need for a loop since you want to just access $_POST['data'][0]
and $_POST['data'][1]['id']
I also noticed that your SQL is a duplicate so you can try You can try
$sql = "INSERT INTO x(`kodLebuhraya`,`kodSeksyen`) VALUES ('%s','%s')" ;
printf($sql,mysqli_real_escape_string($_POST['data'][0]),mysqli_real_escape_string($_POST['data'][1]['id']));
Output
INSERT INTO x(`kodLebuhraya`,`kodSeksyen`) VALUES ('A','B')
Form Used
<form method="POST">
A : <input id="location" type="text" name="data[]" value="" />
B : <input id="shipval" type="text" name="data[][id]" value="" />
<input id="shipval" type="submit" name="submit" value="Submit" />
</form>