I have an array in Javascript with a list of blocks. This array may change his length. I must to access it from PHP because I must to do some operations in my database. The problem is lenght of this array may change. This is the code:
<html>
<head>
</head>
<body>
<script type='text/javascript'>
var blocks = [
{ w: 500, h: 600 },
{ w: 300, h: 200 },
{ w: 150, h: 150 },
{ w: 150, h: 150 },
{ w: 200, h: 250 },
{ w: 100, h: 250 }
];
order_blocks(blocks); //blocks are ordered and can be added new blocks
//Next code is only to test
for(var n = 0 ; n < blocks.length ; n++) {
var block = blocks[n];
if (block.fit) {
var str1 = "Block " + (n+1) + ": (" + block.w + "," + block.h + ")";
document.write(str1);
}
}
</script>
<?php
if (!isset($_POST[numblocks]))
{
echo '<form action="'.$_SERVER[PHP_SELF].'" method=post name=pass>
<input type=hidden name=numblocks>
<input type=hidden name=block1_w>
<input type=hidden name=block1_h>
</form>';
echo '<script languaje="JavaScript">
document.pass.numblocks.value=blocks.length;
document.pass.block1_w.value=blocks[0].w;
document.pass.block1_h.value=blocks[0].h;
document.pass.submit();
</script>';
}
echo '<br><br>There are '.$_POST["numblocks"].' Blocks<br>';
echo 'Block 1 ('.$_POST["block1_w"].','.$_POST["block1_h"].')<br>';
?>
</body>
</html>
The process is:
- Javascript: I create array blocks and apply order_blocks.
- PHP: I pass Javascript variables with POST method.
In the code you can see I pass correctly the first element of the array, but I should do it iterating over the array.