To pass arrays to the server, you would do something like this (note the []
in the name, this signifies an array):
<input type="checkbox" name="myCheckBox[]" value="One" />
<input type="checkbox" name="myCheckBox[]" value="Two" />
<input type="checkbox" name="myCheckBox[]" value="Three" />
<input type="checkbox" name="myCheckBox[]" value="Four" />
Then in php you would do something like this:
foreach($_POST['myCheckBox'] as $value){
echo "<p>$value</p>";
}
You would then get output that looks like this (assuming all but Two
was selected):
One
Three
Four
So assuming you are not using javascript to pass the data, the browser should automatically encode the data. If you are using javascript you can pass the data to the javascript function escape()
and it will escape/encode the data to be passed to the server.
Also for jquery you can use the serialize() operation:
$.ajax(
url: "/to/file.php",
data: $("#my_form").serialize(), // <form id="my_form">
success: function(){
// Do some stuff
}
);
Here is another way to pass data:
<input type="text" name="names[0]['name']['first']" />
<input type="text" name="names[0]['name']['last']" />
<input type="radio" name="names[0]['gender']" value="male" />
<input type="radio" name="names[0]['gender']" value="female" />
<input type="text" name="names[1]['name']['first']" />
<input type="text" name="names[1]['name']['first']" />
<input type="radio" name="names[1]['gender']" value="male" />
<input type="radio" name="names[1]['gender']" value="female" />
<input type="text" name="names[2]['name']['first']" />
<input type="text" name="names[2]['name']['first']" />
<input type="radio" name="names[2]['gender']" value="male" />
<input type="radio" name="names[2]['gender']" value="female" />
<input type="text" name="names[3]['name']['first']" />
<input type="text" name="names[3]['name']['first']" />
<input type="radio" name="names[3]['gender']" value="male" />
<input type="radio" name="names[3]['gender']" value="female" />
The in the php you would do this for example:
foreach($_POST['names'] as $name){
echo "<p>" . $name["name"]["first"] . " is a " . $name["gender"] . "</p>";
}