0
How to push input submitted into an array in php?
POST or GET both are arrays itself but what I want is pushing the submitted data of form into array.....
3 Réponses
+ 9
You can see if the form is submitted and if the data are not empty, you can insert your data inside an array, the code looks like:
$yourarray = array();
if(isset($_POST['submit'])) {
$usr = clean($_POST['username']);
if($usr != "") {
$yourarray[] = $usr;
}
}
The clean() function is used to prevent XSS, it's a simple one, not mandatory but highly recommended... here my solution:
function clean($str) {
$str = htmlentities($str);
$str = htmlspecialchars($str);
$str = stripslashes($str);
return $str
}
... this is just an example, naturally... make your controls, remember that both $_GET and $_POST save the data inside an associative array, you can easily access to your data as i did here... by specifying the name of the input form once your data are submitted. :)
+ 5
Have you tried this:
$arr=array($_POST["data1"], $_POST["data2"]);
Or
$arr=array("data1" => $_POST["data1"], "data2" => $_POST["data2"]);
Where data1 & data2 are the form data.
Hth, cmiiw
+ 4
Depending on what data you want from the form in an array, you can use the array_keys() or array_values() to get data from POST and GET.
Example (PHP):
<?php
$_POST=array("name"=>"Gavin","age"=>26);
$keys=array_keys($_POST);
# Makes an array of the keys
# - will contain 'name' and 'age'
$values=array_values($_POST);
# Makes an array of the values
# - will contain 'Gavin' and 26
?>