0

jsfiddle

I'm trying to print the input text field values on nextpage (postdata.php). But it always print the first row result only. I didn't get the remaining row values on next page. I've posted my full codes on jsfiddle.. How do i get the remaining js dynamic row values in php (postdata.php page)?

JS

$(document).ready(function(){
    $("span").click(function(){
        $("#container").append('<tr><td>Email : </td><td><input type="text" name="email[]" placeholder="Email id" /></td> &nbsp; <td>Name : </td><td><input type="text" name="name[]"  placeholder="Your Name "/></td>&nbsp; <td><a title="Delete this row" href="javascript:void(0);" class="remove">Del</a></td></tr>');
    });
    $("#container").on('click','.remove',function(){
        $(this).parent().parent().remove();
    });
});

Php

<?php
    echo "
    <table>
    <tr>   
        <td>
            Email :
        </td>
        <td>
             $_POST[email]
        </td>
        <td>
            Name :
        </td>
        <td>
             $_POST[name]
        </td>
    </tr>
    </table>";
?>
4
  • The initial names in HTML part are incorrect should be email[] & name[]. Commented Feb 25, 2014 at 9:17
  • @Rikesh : i tried that. But it just display Array in postdata.php page Commented Feb 25, 2014 at 9:18
  • 1
    Obviously it will be an array, var_dump($_POST); on postdata.php file, you will get the better idea. Commented Feb 25, 2014 at 9:21
  • 1
    in your html you have misplaced the form tag. place it before table tag. It will resolve your issue Commented Feb 25, 2014 at 9:40

2 Answers 2

1

Since name of fields you declared is array the $_POST becomes multidimensional array.So try like this

<?php
   $size = sizeof($_POST['email']);
   echo "<table>" ;
   for($i=0; $i<$size;$i++)
   {
    echo "
      <tr>   
        <td>
            Email :
        </td>
        <td>
             ".$_POST['email'][$i]."
        </td>
        <td>
            Name :
        </td>
        <td>
             ".$_POST['name'][$i]."
        </td>
    </tr>
    ";
    }
     echo "</table>";
?>

also in your html change names of Name and Email field to name[] and email[] respectively.Also you have misplaced the form tag. It starts after <table> and ends after <table>. which was not correct. so place form tag before table tag

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

3 Comments

This is not the most efficient code, try replacing $i<sizeof($_POST['email']) with $i<$count while initializing $count = sizeof($_POST['email']);!
BTW you shouldn't be looping the <table> tag :)
We both find it at same time.hahaha.. When copying didnt notice it.
1

When you add square brackets to the name of an input field, PHP will receive it's value as an array. Your JS code is fine, but the PHP code doesn't handle arrays at all. Look at the following:

echo "
<table>";
if(!is_array($_POST['email'])) {
    $_POST['email'] = array($_POST['email']);
    $_POST['name'] = array($_POST['name']);
}

foreach($_POST['email'] as $key => $email) {
    // get the corresponding name to the email
    $name = $_POST['name'][$key];
    echo "<tr>   
        <td>
            Email :
        </td>
        <td>
             $email
        </td>
        <td>
            Name :
        </td>
        <td>
             $name
        </td>
    </tr>";
}

echo "</table>";

Note: This code will check whether multiple values were submitted or not and will work in both scenarios.

5 Comments

But it doesn't display anything on postdata.php page. I'm getting this warning message Warning: Invalid argument supplied for foreach()
in your html also change names of Name and Email field to name[] and email[] respectively
See updated code too handle single scenario. Like @krishna pointed out, your JS is correct but you base HTML doesn't include the square brackets. Please add them to fix the issue.
@Beginner in your html you have misplaced the form tag. place it before table tag. It will resolve your issue.
@Birla even if you submit one value it will be an array, because the name of fields were array.so there is not point in checking it when you give names as array.This works fine when you mix names with array and strings which is not advised.

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.