将多维数组数据传递给PHP页面,从动态创建的textboex中获取值

I need help regarding how to collect data from text boxes created dynamically. I am giving my html code. my adding and removing code for textboxes is working prefectly. I would like to know where i am doing wrong whether in defining array of textboexs. Whether i am not collecting textbox values properly. Another question is how should i pass this array value to another page after submitting page for inserting into DB.

This code is for taking values for itineraries for a tour for travel agent.

Here is my html form ` Enter Itinerary Details Name of Tour (category) Days City Itinerary Night Stay -->

            <div id="TextBoxesGroup">

              <div id="TextBoxDiv1">

                <label></label>

                <input type='text'  id='dayNo'  name="itinerary[]" >
                <input type="text"  id='cityName'  name="itinerary[]">
                <input type='text'  id='schedule'  name="itinerary[]">
                <input type='text'  id='nightStay'  name="itinerary[]">

              </div>

            </div>

            <input type='button' value='Add Button' id='addButton'>
            <input type='button' value='Remove Button' id='removeButton'>
            <input type='button' value='Get TextBox Value' id='getButtonValue'>     
            <input type="submit" name="submit" id="submit" value="Submit" />

            </td>
        </tr>
      </table>
    </form>`

I would like to know does i have defined correct array name "itinerary[]". If there are 10 days tour then there will be 10 rows containing 4 fields. How should i collect those rows and send to another page for inserting. Please let me how should i write the VALUES () section after collecting into array. Can i send this data of 10 rows using session. Can i send this data as parameter. I am sorry for so many questions in one post. I have tried all these way but no successes or most probably i m wrong somewhere Please help me this is live site. Stuckedup in this part Thanks millions in advance.


Sorry for this delayed reply to your ans. Actually i really getting no idea about how should i declear the arrays. Honestly this is mking me very frustrating. and loosing my time. Please Guide me if u can. I am giving my JS code for adding the text box dynamically and in my previous post i have given the HTML code. Can u check please whether i am doing correct the naming convention for the declearing array. Please make necessary changes into my code please and paste here plz.

Here is my JS code adding textboxes dynamically

$(document).ready(function(){

var counter = 2;

$("#addButton").click(function () {

if(counter>30){
        alert("Only 30 textboxes allow");
        return false;
}   

var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter + ' ' + 'style="width:750px;"');

newTextBoxDiv.html(
      '<input type="text" name="itinerary[dayNo]'+ counter + '" id="dayNo'+ counter + '" value="'+ counter + '" >' +
      '<input type="text" name="itinerary[cityName]'+ counter + '" id="cityName'+ counter + '" value="" >'+
      '<input type="text" name="itinerary[schedule]'+ counter + '" id="schedule'+ counter + '" value="" >'+
      '<input type="text" name="itinerary[nightStay]'+ counter + '" id="nightStay'+ counter + '" value="" >' );

newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
 });

No i am giving again my HTML code.. can you check whether i am giving the names properly or not.. It will be greatest help for me.

Here is my HTML Code

 <div id='TextBoxesGroup'>

              <div id="TextBoxDiv1">

                <label></label>

                <input type='text'  id='dayNo'  name="itinerary[dayNo][]" >
                <input type="text"  id='cityName'  name="itinerary[cityName][]">
                <input type='text'  id='schedule'  name="itinerary[schedule][]">
                <input type='text'  id='nightStay'  name="itinerary[nightStay][]">
              </div>
            </div>

            <input type='button' value='Add Button' id='addButton'>
            <input type='button' value='Remove Button' id='removeButton'>
            <input type='button' value='Get TextBox Value' id='getButtonValue'>     
            <input type="submit" name="submit" id="submit" value="Submit" />              

Now tell me how should i pass the values collected in the array to either session or thru URL like ---action="savedata.php?arr=...."

Please make correction in my code itself.. i m new and feeling very lost since last few days.

I wrote like this about passing the values to array

<?php

   $totalArray = itinerary;

   $_session['tArray'][]=$totalArray;

?>

Is this correct way to send thru session. Thanks millions in advance

I'm not sure to understand your issue very well, i'll try to explain steps.

  • Don't use generic ID for dynamics inputs.
  • PHP doesn't matter about id's
  • If you want to match a "block" of schedule, and match it with jQuery, set a proper data-* to each, or even a class.

Here's an example :

<div class="TextBox" data-formNum="0">
   <input type='text' name="itinerary[dayNo][]">
   <input type='text' name="itinerary[cityName][]">
   <input type='text' name="itinerary[schedule][]">
   <input type='text' name="itinerary[nightStay][]">
</div>

When you want to add a block of form with jQuery, use :

var idToAdd = $(".TextBox:last").attr("data-formNum")+1;

When you send this form to PHP, everything is going to be formated like :

Array
(
    [intinerary] => Array
        (
            [dayNo] => Array
                (
                    [0] => 15
                    [1] => 14
                )

            [cityName] => Array
                (
                    [0] => London
                    [1] => Prague
                )
               ...

Then you can write an INSERT INTO function cycling on this array like:

for ($i=0; $i < count($_POST['itinerary']['dayNo']);$i++)
{
     $day = $_POST['itinerary']['dayNo'][$i];
     $cit = $_POST['itinerary']['cityName'][$i];
}

Hope this helped.
Let me know if I misunderstood.