0

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.

2
  • thats really bizzare what you trying to do here is .... data[] is a simple string in input location case and data[] is array in shipval case ... show how can one element be array and string at the same time.. change your input name method Commented Oct 2, 2012 at 15:03
  • Do you have idea i could done this ? i have no idea to solve this.. Commented Oct 2, 2012 at 15:14

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I've try your code. And change the <inputs> for the form to input. but i get nothing for the output. Already try echo the location and the shipval. Any idea?
Corrected the syntax in the $_POST array. @muhamad-azam if you are going to be using multiple inputs like this you should not use id="location" or id="shipval" as those must be unique so you might want to use class attributes instead.
Ok thanks! I got this working now. Thanks you very much for those who helping me on this .
0

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=""/>

2 Comments

if this is copy/pasted code: good catch, this is undoubtedly the issue. +1
Might be worth noting it actually throws a warning - which may be why it wasn't noticed, if warnings are not being shown.
0

For multiple select, the name has to end in square brackets, so you'll need to change the name of your shipval inputs

Comments

0

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.

1 Comment

I didn't get it..what do you mean by "will treat each variable as an array, but the location isn't an array". any solution for this?
0

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>

3 Comments

first of all main problem is in input field names he wont get any id reason::: assume scenario his location is like data[0] = 'london' from locations then lets say id should be 5 then data[0][id] = 5... look at this how can data[0] can be string value and assoc array at same time
@Surace that was just the basic issue but have updated my answer with the form i used
Hi,Basically i have multiple input for location and shipval. And it will be pair of it. Means one location for one shipval. how if i have 4 input location for location and shipval? What will the code will be?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.