-1

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:

  1. Javascript: I create array blocks and apply order_blocks.
  2. 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.

4
  • I can't see any PHP code (at least trying to) "iterating over the array." Commented Sep 26, 2013 at 8:32
  • Correct djot. I don´t know how to iterate over the array in the php block. Sorry if I have not expressed well. Commented Sep 26, 2013 at 8:36
  • So may I help you with this? php.net/manual/en/control-structures.foreach.php Commented Sep 26, 2013 at 8:37
  • Yes it can help me, but my problem is that in the first pass I don´t know how many iteration I must to do because I don´t know the lenght of the array. Commented Sep 26, 2013 at 8:39

1 Answer 1

0

Replace this :

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>';   

with this (should work) :

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 language="text/javascript">
            for(var i = 0; i < blocks.length; i++) {
                document.pass.numblocks.value=blocks.length;
                document.pass.block1_w[i].value=blocks[i].w;
                document.pass.block1_h[i].value=blocks[i].h;

            }
        </script>';   
1
  • Thanks mansoulx but it don't let me make the assigment document.pass.block1_w[i].value=blocks[i].w; Commented Sep 26, 2013 at 13:05

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.