0

I using jquery to add / remove inputs

i use append to add multiple Tr for Date / revenue

also i use another append to add multiple td for revenue in same Tr of date

i Add multiple date inputs and into this table i add multiple revenue inputs

I have to use name="date[]" to can use for loop and insert each one in mysql table

but in sametime there is multiple name="revenue[]"

Here is example

    <form method="post" action ="">
    <table>
<tr>
<td>Date : <input type="text" name="date[]" value="25/07/2013"></td>

<td>Revenue : <input type="text" name="revenue[]" value="1"><br>
    Revenue : <input type="text" name="revenue[]" value="2" ><br>
</td>
</tr>

<tr>
    <td>Date : <input type="text" name="date[]" value="26/07/2013"> </td>

    <td>
    Revenue : <input type="text" name="revenue[]" value="12"><br>
    Revenue : <input type="text" name="revenue[]" value="13"><br>
    Revenue : <input type="text" name="revenue[]" value="14"><br>
    </td>
 </tr>   

    <tr>
    <td>Date : <input type="text" name="date[]" value="27/07/2013"></td>
    <td>
    Revenue : <input type="text" name="revenue[]" value="30"><br>
  </td>
 </tr>  
 </table>
    <br><br><input name="submit" value="submit" type="submit">

    </form>

PHP code

<?php
if(isset($_POST['submit'])){
$date = $_POST['date'];
echo "Results<br>";
for($i = 0; $i<count($date); $i++){
echo "Date : $date[$i] <br>";
print_r($_POST['revenue']);
echo "<br><br>";
}
}
?>

Results is :

Date : 25/07/2013
Array ( [0] => 1 [1] => 2 [2] => 12 [3] => 13 [4] => 14 [5] => 30 )

Date : 26/07/2013
Array ( [0] => 1 [1] => 2 [2] => 12 [3] => 13 [4] => 14 [5] => 30 )

Date : 27/07/2013
Array ( [0] => 1 [1] => 2 [2] => 12 [3] => 13 [4] => 14 [5] => 30 ) 

I wanted results to be like that

Date : 25/07/2013
Array ( [0] => 1 [1] => 2)

Date : 26/07/2013
Array ( [0] => 12 [1] => 13 [1] => 14)

Date : 27/07/2013
Array ( [0] => 30 ) 

i want to insert each date into a table with the multiple revenue in same row

Like In Row

Date : 25/07/2013     Revenue : 1-2
Date : 26/07/2013     Revenue : 12-13,14
Date : 27/07/2013     Revenue : 30

Its very important for me, Thank You Very Much

1
  • Your loops are wrong, but the main problem is, that there is no way to tell which "revenues" belong to what "dates". Commented Aug 7, 2013 at 22:52

1 Answer 1

2

When you're creating your date and revenue inputs, name them with the array notation, but include indexes:

<tr>
    <td>Date : <input type="text" name="date[0]" value="25/07/2013"></td>
    <td>
        Revenue : <input type="text" name="revenue[0][]" value="1"><br>
        Revenue : <input type="text" name="revenue[0][]" value="2" ><br>
    </td>
</tr>

<tr>
    <td>Date : <input type="text" name="date[1]" value="26/07/2013"> </td>

    <td>
        Revenue : <input type="text" name="revenue[1][]" value="12"><br>
        Revenue : <input type="text" name="revenue[1][]" value="13"><br>
        Revenue : <input type="text" name="revenue[1][]" value="14"><br>
    </td>
</tr>   

You should then be able to read them from $_POST as $_POST['date'][0] and $_POST['revenue'][0][0], `$_POST['revenue'][0][1], etc.

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

6 Comments

but as i said i using jquery to add / remove multiple inputs for date / revenue , so i think if i used your way, when i coding php , it will cause a problem if $_POST missed
No - you can create these elements in jQuery, with these names. I've just posted the HTML here to show how it works.
this is the way to do it, i do it all the time. although i do name="update[0][date]", name="update[0][revenue][0]", name="update[0][revenue][1]", etc
Yes Mike W, i understood you, but look, i will do the following with jquery i will use append : rename revenue input to : revenue[1][] in first Tr of date, then i added with jquery second TR of date and renamed : revenue input to revenue[2][] , what about if i deleted second TR of data which has revenue[2][] , and i will use for loop on date array, so how i can check the revenues of this date ?
Your PHP script can iterate over the dates in one foreach loop, and iterate over the reveues in a second, inner, foreach loop. Exactly how you do that depends on your application. It's really a different question. Have a go at it, and post a new question if you get stuck.
|

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.