PHP,Javascript和url变量问题

I have two files......

a.php

<html>
<head>
<script>
    var randomnumber=new Array(); 

for(var i=1; i<=10; i++)
{
   randomnumber[i]=Math.random();
}

document.getElementById("h_field").value = randomnumber[1];

</script>
</head>
<body>

    <form id="myform" action="process.php?var1=h_field"  method="GET" >
    <input type="hidden" id ="h_field">
    <input type="Submit" value="my button">
</form>

</body>
</html>

b.php

<?
echo $_GET['var1'];
?>

What i plan to do is to use the JavaScript to create an array of random numbers. Then place the first element of this array in the hidden field whose id is h_field. Then create a url variable and assign the value of the hidden field to that variable. And Finally $_GET it, and print it to the screen with the page that will process the form.

Problem

  1. The outcome is just a white page.
  2. I cannot see the url variable in the address bar of the processing page either.

While your script located in <head></head> section, it will be executed immediately, before <body></body> section loaded: <input type="hidden" id ="h_field"> might not even exist in this moment.

  1. Move <script></script> section to the <body></body> section after <form></form> declaration.
  2. Other way is convert script to a function and assign it as an event handler for window.onload.
  3. If you want to submit your value to the server, you should supply name attribute to the input field, as @DevZer0 mentioned.

Fixed script:

<html>

<head>
<script>
function init(){
    var randomnumber = []; 

    for(var i=1; i<=10; i++)
    {
        randomnumber[i] = Math.random();
    }

    document.getElementById("h_field").value = randomnumber[1];
}

window.onload = init;
</script>
</head>

<body>

<form id="myform" action="process.php?var1=h_field"  method="GET" >
    <input type="hidden" name="h_field" id="h_field">
    <input type="Submit" value="my button">
</form>

</body>
</html>

your hidden input needs a name attribute

<input type="hidden" id ="h_field" name="h_field" />

Why create a new variable. GET will automatically add all the input inside the form as GET variables. so you can use it like this

<form id="myform" action="b.php"  method="GET" >
    <input type="hidden" id ="h_field" name="h_field">
    <input type="Submit" value="my button">
</form>

You can get the values from h_field like so: $_GET['h_field'];

also your javascript code run before the html was loaded so you will get an error Cannot set property 'value' of null on you javascript code.

Here is a complete code

a.php

<html>
<head>
<script> 

function LoadArray(){
    var randomnumber=new Array(); 

    for(var i=0; i<=10; i++)
    {
       randomnumber[i]=Math.random(); 
    } 
    document.getElementById("h_field").value = randomnumber[0];
}
 window.onload = LoadArray;
</script>
</head>
<body> 
    <form id="myform" action="b.php"  method="GET" >
        <input type="hidden" id ="h_field" name="h_field">
        <input type="Submit" value="my button">
    </form>

</body>
</html>

and on your b.php always put php when creating a php tags<?php ?>

b.php

<?php
echo $_GET['h_field'];
?>