I am having a problem with my multidimensional array. I am trying to insert an element via AJAX.
I grab the data from inside a table and send it to a PHP file which is meant to print the results but it prints incorrect data.
My HTML table:
<table id="mytable">
<tr>
<td>first</td>
<td>second</td>
<td>third</td>
</tr>
<tr>
<td>fourth</td>
<td>fifth</td>
<td>sixth</td>
</tr>
</table>
Here is my Javascript:
for(i=0; i<=length; i++){
for(j=0;j<width;j++){
//inside of html table grab cell 1x1
data = document.getElementById("mytable").rows[i].cells[j].innerHTML;
//define the Array
export_table[i]= new Array;
export_table[i][j] = data;
}
}
The PHP in my processdata.php:
print_r($_POST)
The problem I see is PHP prints:
Array
(
[0] => ,,first
[1] => ,,,second
)
But I really need it to print:
Array
(
[0] => Array
(
[0] => "first"
[1] => "second"
[2] => "third"
)
[1] => Array
(
[0] => "fourth"
[1] => "fifth"
[2] => "sixth"
)
)
From what I can see from your Javascript it looks like you are sending your Javascript array variable to the PHP.
If this is true then the array value will not send properly because you can not pass advanced data types like Arrays and Objects into PHP $_POST
and $_GET
queries.
You may need to send it as a serialized string and then explode()
the data inside PHP to move it back into a PHP array variable.
Alternatively you could try this:
for(i=0; i<=length; i++){
//The first MultiDimensional Array
export_table[i]= new Array;
for(j=0;j<width;j++){
//grab tables cell 1x1
data = document.getElementById("mytable").rows[i].cells[j].innerHTML;
//insert 'data' into 2nd dimentional array
export_table[i][j] = data;
}
}
It should return values in format below:
Array
(
[tab] => Array
(
[0] => first,second,third
[1] => fourth,fifth,sixth
)
)
Now PHP can use explode to move each array item into its own array.
This might fix it.
for(i=0; i<=length; i++){
export_table[i]= new Array; // this line was misplaced.
for(j=0;j<width;j++){
data = document.getElementById("mytable").rows[i].cells[j].innerHTML;
export_table[i][j] = data;
}
}
But I'm confused about your workflow. Where is this data coming from that you need to send it back to the server in table format from the client?