1

I have a form which has same products but with different attributes. The form looks something similar to this:

<form>
<table>
    <tr>
        <td> Line-1 </td>
        <td> <input type='text' name='ítem[1][Size]'</td>
        <td> <input type='text' name='ítem[1][Color]'</td>
        <td> <input type='text' name='ítem[1][Price]'</td>
    </tr>
    <tr>
        <td> Line-2 </td>
        <td> <input type='text' name='ítem[2][Size]'</td>
        <td> <input type='text' name='ítem[2][Color]'</td>
        <td> <input type='text' name='ítem[2][Price]'</td>
    </tr>
    <tr>
        <td> Line-3 </td>
        <td> <input type='text' name='ítem[3][Size]'</td>
        <td> <input type='text' name='ítem[3][Color]'</td>
        <td> <input type='text' name='ítem[3][Price]'</td>
    </tr>
    <tr>
        <td> Line-4 </td>
        <td> <input type='text' name='ítem[4][Size]'</td>
        <td> <input type='text' name='ítem[4][Color]'</td>
        <td> <input type='text' name='ítem[4][Price]'</td>
    </tr>
    </table>
</form>

PHP Sample Code:

for($i = 0;$i < count($_POST);$i++){
    var_dump($_POST[$i]);
}

Error:

Notice: Undefined offset: 0 in C:\xampp\htdocs\test\testing.php on line 5

If I was to remove item[2] dynamically using js, on submit php would throw an error because it cant find number 2. How could go about this?

Perhaps I'm not doing it right?

12
  • Can you show us What are you doing here? It's hard to tell anything without code Commented May 4, 2015 at 20:38
  • We need to see what your PHP Code is doing. Please edit and post your PHP form handler. What is the error you're getting. Commented May 4, 2015 at 20:40
  • Because you are violating array indexes. And please put your error here? Commented May 4, 2015 at 20:40
  • I'm not sure what other code i could write in. I have a form, which gets processed in php using post, In php, if I would write foreach(), it would throw an error because it cant find number 2 so it cant carry on. I will try adding snippet Commented May 4, 2015 at 20:42
  • 3
    @Vilius I'm assuming you're using for not foreach. foreach will never produce an undefined index error. Commented May 4, 2015 at 20:45

1 Answer 1

5

In your PHP Code, use foreach as has been suggested:

foreach($_POST['item'] as $item){
    echo $item['Size'] . "<br />";
    echo $item['Color'] . "<br />";
    echo $item['Price'] . "<br />";
    echo "<hr />"
}

EDIT:

Another question how would i go about re-changing row numbers once I deleted row number 2?

This is done in JS or JQuery. Something like:

var i = 1;
$.each($("tr"), function(){
  $(this).next("input[name*='Size']").attr("name", "item[" + i + "][Size]");
  $(this).next("input[name*='Color']").attr("name", "item[" + i + "][Color]");
  $(this).next("input[name*='Price']").attr("name", "item[" + i + "][Price]");
  i++;
});
Sign up to request clarification or add additional context in comments.

Comments

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.