Php post array data to server
I have some number which are generated randomly . I need to get these numbers on server side i.e.$POST['number_array'].This "number_array" should include all the randomly generated numbers .Can anybody suggest me so开发者_Go百科me way to do this. I am using PHP , Jquery , Javascript
your GET/POST request should look like this:
number_array[]=1&number_array[]=2&number_array[]=5
Put []
s after the name of the form element.
<input type="text" name="number_array[]" />
<input type="text" name="number_array[]" />
etc.
Then you can access the variables like so:
$number_array = $POST['number_array'];
$number_array[0];
If this is an ajax call, you can do it with jquery:
$.post("test.php", { 'number_array[]': [65, 45] }, function(data) {
alert("Data Loaded: " + data);
}););
EDIT: both 'number_array[]': [65, 45]
and 'number_array': [65, 45]
works fine, and "
around javascript arrays are optional
Let me summary what you need:
Client side with javascript
<script>
//If you have an array with 3 items with random value
var arrInt = [Math.random(),Math.random(),Math.random()];
//If you want to send it to file yourscript.php
//1. Using GET mothod
/*
$.get(
'yourscript.php',
{
number_array: arrInt
}
);
*/
//2. Using POST method:
$.post(
'yourscript.php',
{
number_array: arrInt
}
);
</script>
Server side with PHP:
<?php
//$arrInt = $_GET['number_array'];//If using GET
$arrInt = $_POST['number_array'];//If using POST
print $arrInt[0];
print $arrInt[1];
print $arrInt[2];
?>
精彩评论