I've used Respect/Validation successfully for my general concern.
But now I'm validating some form Input where the user can check multiple checkboxes and the data is send with an array.
The form looks something like this:
<form method="post" action="">
<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="checkbox" name="options[]" value="1">
<input type="checkbox" name="options[]" value="2">
<input type="checkbox" name="options[]" value="3">
<button type="submit">Send</button>
</form>
So, my post-data will look like this:
Array
(
[firstname] => Peter
[lastname] => Parker
[options] => Array
(
[0] => 1
[1] => 3
)
)
I've build a validation rule which works:
<?php
//used in class, so "use Respect\Validation\Validator AS v;"
$validReq = v::create()
->key('firstname', v::stringType()->length(1, 32))
->key('lastname', v::stringType()->length(1, 32))
->key('options', v::optional(v::arrayType()))
->setName('valid request');
My question now is, how do I validate the array options with (e.g.) v::intVal()?
Maybe I've just oversaw how to accomplish this. Thank you for your time.
Cheers,
Patrik
Solved with the help of alganet over at github.
This could be accomplished using each():
<?php
$validReq = v::create()
->key('firstname', v::stringType()->length(1, 32))
->key('lastname', v::stringType()->length(1, 32))
->key('options', v::optional(v::arrayType()->each(v::intVal())))
->setName('valid request');
Cheers,
Patrik
Related
I have 1000 checkboxes of object fields and I want to create an Array from all the fields that I check after submitting using PHP can you tell me how to do it?
well if you name your checkbox like this, you already have an array:
<input name="mycheckbox[]" value="1"> hello1
<input name="mycheckbox[]" value="2"> hello2
<input name="mycheckbox[]" value="3"> hello3
in PHP you will get:
print_t($_REQUEST['mycheckbox']);
/*
[
0 => '1',
0 => '2',
0 => '3'
]
*/
<input type="checkbox" name="somename[]" value="somevalue"> if you need an array with numerical indices
or
<input type="checkbox" name="someobject[property]" value="somevalue"> if you are looking for an associative array
I'm trying to insert multiple values using radio, here is my example:
<input type="radio" name="toppingPrice[]" value="<?= $topping['name'];?>-<?= $topping['price'];?>">
this one work if I insert single input, but if I want to insert more than one for example:
Size: small, medium, large <- name="toppingPrice[]" for all input values
Cheese: yes, no <- name="toppingPrice[]" for all input values
Level: spicy, normal <- name="toppingPrice[]" for all input values
this will not work because it will merge into 1 group so if I have to choose only one of all toppings.
my original code looks like:
foreach ($toppingPrice as $key) {
list($toppingName,$toppingNameEn, $toppingPrice) = explode("-",$key,3);
$tName[] = $toppingName;
$tNameEn[] = $toppingNameEn;
$tPrice += $toppingPrice;
}
$tn = implode(",", $tName);
$tn_en = implode(",", $tNameEn);
$price = $price + $tPrice;
Html:
<input type="checkbox" name="toppingPrice[]" id="<?= $topping[0];?>" value="<?= $topping['name'];?>-<?= $topping['name_en'];?>-<?= $topping['price'];?>" <? if($topping['price'] < 1){echo "checked";}?>>
I hope I delivered the question in the right way
please give me any idea or solution for fix this issue
You should use name attribute as shown in below code.
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
<form name="test" method="post">
<input type="radio" name="toppingPrice.size[]" value="1"> 1
<input type="radio" name="toppingPrice.size[]" value="2"> 2
<input type="radio" name="toppingPrice.size[]" value="3"> 3
<br>
<input type="radio" name="toppingPrice.toping[]" value="4"> 4
<input type="radio" name="toppingPrice.toping[]" value="5"> 5
<input type="radio" name="toppingPrice.toping[]" value="6"> 6
<br>
<input type="radio" name="toppingPrice.level[]" value="7"> 7
<input type="radio" name="toppingPrice.level[]" value="8"> 8
<input type="radio" name="toppingPrice.level[]" value="9"> 9
<button type="submit" value="save" /> Save
</form>
This will be your $_POST after form submit
Array
(
[toppingPrice_size] => Array
(
[0] => 1
)
[toppingPrice_toping] => Array
(
[0] => 5
)
[toppingPrice_level] => Array
(
[0] => 9
)
)
I am new to php programming language.So basically I am creating a simple form.
<?php
print_r($_POST);
?>
<form name="form1" method="post" action="">
Name: <input type="text" name="mail"><br>
Phone No: <input type="text" name="phon" /><br/>
Course:<input type="text" name="course" /> <br />
Experience: <select name="exp"> <option value="beginner">Beginner</option> <option value="intermediate">Intermediate</option> <option value="advanced">Advanced</option> </select><br>
<input type="submit" name="Submit" value="Sign Up">
</form>
So the output will be something like this:
Array ( [mail] => john [phon] => 123455666 [course] => bsc [exp] => beginner [Submit] => Sign Up )
I want to modify or change the output something like this,
Name=john
Phone No=123455666
Course=bsc
Experience=beginner
And I want to store that in the arrays, i.e. right hand side parameters in one array and left hand side parameters in another array. So that it will be easy to access or search the data. In the next level of this i want to save these values in a file.
Please help me out.
Any help or advice is appreciated. Thanks in advance.
$array1= array();
$array2= array();
foreach($_POST as $key => $value){
echo $key ."=". $value ;
echo "<br>";
$array1[]=$key; //first array for left hand side
$array2[]=$value; //second array for right hand side
}
print_r($array1); print_r($array2);
Output:-
phon=123456
course=maths
exp=intermediate
Submit=Sign Up
Array ( [0] => mail [1] => phon [2] => course [3] => exp [4] => Submit ) Array ( [0] => tet [1] => test [2] => stedt [3] => intermediate [4] => Sign Up )
Name of fields are key to $_POST array
$_POST['name_of_input']
Example:
$_POST['mail']
$_POST['phone']
Look at array_keys() and array_values()
http://php.net/manual/en/function.array-keys.php
http://php.net/manual/en/function.array-values.php
Take lessons on MySQL to store data in database
I suggest to set the names you want in the form on the first place.
Changing the $_POST is not a good practice. All it can do is confuse you and anyone else might read your code.
<form name="form1" method="post" action="">
Name: <input type="text" name="Name"><br>
Phone No: <input type="text" name="Phone No" /><br/>
Course:<input type="text" name="Course" /> <br />
Experience: <select name="Experience"> <option value="beginner">Beginner</option> <option value="intermediate">Intermediate</option> <option value="advanced">Advanced</option> </select><br>
<input type="submit" name="Submit" value="Sign Up">
</form>
Right Hand Side parameters:
$rhs_params = $_POST;
Left Hand Side parameters:
$lhs_params = array_keys($_POST);
I am not sure what you are planning to do with this tho!!
foreach($_POST as $key => $value){
#TO DO your operation
}
I am posting checkboxes from an HTML form and am having a weird problem. The first is that I have a default checked and disabled box at the top of the form, but it doesn not get included in the POST data. The second is that If I don't check something, the entire array is left out.
How can I get it to 1) include my default box and 2) POST an empty array if none are selected?
Here's the code:
<form action="file.php" method="POST">
<label><input type="checkbox" name="options[]" value="username" checked disabled> Username</label><br>
<label><input type="checkbox" name="options[]" value="title"> Title</label>
<label><input type="checkbox" name="options[]" value="first"> First Name</label>
<label><input type="checkbox" name="options[]" value="last"> Last Name</label><br>
<label><input type="checkbox" name="options[]" value="address"> Address</label>
<label><input type="checkbox" name="options[]" value="city"> City</label>
<label><input type="checkbox" name="options[]" value="state"> State</label>
<label><input type="checkbox" name="options[]" value="zip"> ZIP</label><br>
<label><input type="checkbox" name="options[]" value="email"> Email</label>
<label><input type="checkbox" name="options[]" value="phone"> Phone</label><br>
<input type="submit" value="submit">
</form>
file.php
<?php var_dump($_POST)
This is a part of standard HTML (i.e. not a browser thing). By definition, unchecked boxes are never successful. Consider a different data structure, or adding something like
if(isset($_POST['options'])) {
//work with options here
}
If that won't work, you can always include a hidden element, that will at least get you the value in $_POST
<input type="hidden" name="options[]" value="NA">
You could also do something like this without changing your HTML at all. Simply, create a list of all possible checkbox values and compare with those posted. As far as username is concerned, since it will always be there, you could just add it manually to the $_POST array.
// Auto insert username to $_POST array (because it's always there by default)
$_POST['options'][] = 'username';
// Create array of all possible checkbox values
$boxes = array('username','title','first','last','address','city','state','zip','email','phone');
// Compare $_POST array to list of possible checkboxes
// and create manual post array
$post_array = array();
foreach ($boxes as $box) {
$post_array[$box] = in_array($box, $_POST['options']) ? 'checked' : 'NOT checked';
}
The output will be an array, $post_array, which will contain something like:
Array
(
[username] => checked
[title] => checked
[first] => NOT checked
[last] => NOT checked
[address] => checked
[city] => checked
[state] => NOT checked
[zip] => NOT checked
[email] => NOT checked
[phone] => checked
)
I have a huge form (for an internal CMS) that is comprised by several sections, some of them are optional some of them are compulsory. All is under an humungous form (it has to be like this, no ajax, no other ways :-( )
Since in a Dilbertesque way everything get changed every second I was wondering if there is any simple way of grouping $_POST data, I mean sending POST like this:
$_POST['form1']['datax']
or to retrieve data from server side easily, and by easily I mean withouth having to expressily declare:
$array1 = array($_POST['datax'],$_POST['datay'],...);
$array2 = array($_POST['dataalpha'],$_POST['dataomega'],...);
since there are around 60 fields.
I hope I was able to explain this well and as always thank you very much..
If you give your input elements array-like names, they arrive in the PHP $_POST (or $_GET) array as an array:
<input type="text" name="foo[]" value="a"/>
<input type="text" name="foo[]" value="b" />
<input type="text" name="foo[]" value="c" />
<input type="text" name="foo[bar]" value="d" />
<input type="text" name="foo[baz][]" value="e" />
<input type="text" name="foo[baz][]" value="f" />
Goes to:
print_r($_POST)
foo => array (
0 => a
1 => b
2 => c
bar => d
baz => array(
0 => e
1 => f
)
)
If you name your inputs properly, you can do that. Example:
<input type="text" name="textInput[]" />
<input type="text" name="textInput[]" />
That will populate an array in $_POST named textInput. That is:
$_POST['textInput'][0] == "whatever the first was set to be"
$_POST['textInput'][1] == "whatever the second was set to be"
Using square brackets after the input name will cause it to be grouped in PHP:
<input name="foo[]" type="text" value="1" />
<input name="foo[]" type="text" value="2" />
You can also make an associative array:
<input name="foo[bar]" type="text" />
I think multi-dimensional arrays would also work, but I'm not sure if I've actually tried it.
Edit: Here's the same thing answered in the PHP FAQ.
you can use your form fields like this:
<input type="text" name="form1['datax']"/>