www.webdeveloper.com
Results 1 to 3 of 3

Thread: Problem With Array Multidimensional

  1. #1
    Join Date
    Jan 2013
    Posts
    2

    Problem With Array Multidimensional

    Hello,

    I have a big problem with a Insert Element of a multidimensional Array, that i send by AJAX.
    I explain you my problem:

    Here my JS Code

    Code:
    for(i=0; i<=length; i++){
            
            for(j=0;j<width;j++){
                
                    // je récupère un à un (cellule par cellule) les valeurs d'un tableau html
                   // I get value of Cell, one and one , of my html Table
                    result = document.getElementById("table").rows[i].cells[j].innerHTML;
    
                    // Construct my Array 
                    table_export[i]= new Array;
                    table_export[i][j] = result;
            }
        }

    Then I send my table to php file, which return me a result of print_r($_POST)

    My problem is here ! The php Fil return me this Array =>

    Code:
    Array
            (
                [0] => ,,tata
                [1] => ,,,pupu
            )
    But I want =>

    Code:
    Array
            (
                [0] => Array
                                  (
                                     [0] => "toto"
                                     [1] => "titi"
                                     [2] => "tata"
                                  )
                [1] =>  Array
                                  (
                                     [0] => "popo"
                                     [1] => "pipi"
                                     [2] => "pupu"
                                  )
            )
    HELP ME PLEASE !

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    741
    It's hard to tell without certain parts of the code, but it looks like you might be actually sending your javascript array variable to the PHP script. If this is the case, the array variable will not properly send because you can't pass advanced data types like arrays and objects in to PHP $_POST and $_GET queries. You'll need to most likely send it as a string and then use something like explode() in PHP to break it back in to a PHP array variable.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Jan 2013
    Posts
    2

    Thumbs up Resolve !

    I found it !
    Code:
    // table Html Code 
    <table id="mytable">
       <tr>
           <td>papa</td>
           <td>popo</td>
           <td>pupu</td>
       </tr>
       <tr>
           <td>tata</td>
           <td>toto</td>
           <td>tutu</td>
       </tr>
    </table>


    Code:
    // construct array multi dimensional / JS FILE
    for(i=0; i<=length; i++){
            
           // Construct my first dimensional Array 
            table_export[i]= new Array;
            for(j=0;j<width;j++){
                
                   // I get value of Cell, one and one in my html Table
                    result = document.getElementById("table").rows[i].cells[j].innerHTML;
                   // Insert 'result' in second dimensional Array 
                    table_export[i][j] = result;
            }
        }
    
    req = { 'tab[]' : table_export};
            $.ajax({
                       type: "POST",
                       url: "myphpfile.php",
                       data: req ,
                       success: function(x){
                         $('#result').html(x);
                       }
            });
    //PHP file
    Code:
    <?php 
    print_r($_POST);
    ?>
    //RESULT :
    Code:
    Array
    (
        [tab] => Array
            (
                [0] => papa,popo,pupu
                [1] => tata,toto,tutu
            )
    
    )

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles