Many fields in HTML form - php

I have a form with about 150 fields, however the user can add more fields, reaching about 300 fields.
I'd like to know if exist a good way to put names and IDs to my fields, or i have to give each one a diferent name and ID. It's a hard job.

html with only one name for input
<form action="checkbox.php">
<input type="checkbox" name="data[]" value="1">1<br>
<input type="checkbox" name="data[]" value="2">2<br>
<input type="checkbox" name="data[]" value="3">3<br>
<input type="checkbox" name="data[]" value="4">4<br>
<input type="checkbox" name="data[]" value="5">5<br>
<input type="submit">
</form>
checkbox.php
$data = $_GET["data"]; //$data is an array with checked option
it works also for type text
<input type="text" name="text_data[1]"><br>
<input type="text" name="text_data[2]"><br>
<input type="text" name="text_data[3]"><br>

if i understand in right, then you can use same name,id for group of same types of control (group of textbox,checkbox,etc...) and then access as array from javascript or code behind ..

Related

How can I use several form inputs with the same name?

I have several checkbox (I don't know number of them) that create from a loop in a form.
<form>
<input type="checkbox" name="id" value="id">
<input type="checkbox" name="id" value="id">
...//create in a loop
<input type="checkbox" name="id" value="id">
</form>
My question is that How can I read them, If I use <?php $_REQUEST['id']; ?>, it only reads the last checkbox.
Use an input array:
<input type="checkbox" name="id[]" value="id_a">
<input type="checkbox" name="id[]" value="id_b">
<input type="checkbox" name="id[]" value="id_c">
<!-- ^^ this makes it an array -->
$_REQUEST['id'] can be accessed:
foreach($_REQUEST['id'] as $id)
{
echo $id;
}
Outputs
id_a
id_b
id_c
Side note: this works with $_POST and $_GET (not just $_REQUEST). Generally speaking though $_REQUEST should be avoided if possible.
Use unique id's for your checkboxes, e.g.,
<form>
<input type="checkbox" name="id1" value="value1">
<input type="checkbox" name="id2" value="value2">
...//create in a loop
<input type="checkbox" name="id3" value="value3">
</form>

php comparison of values in two radio buttons

this is one radio button set
<input type="radio" name="image" id="100" value="a"/>
<input type="radio" name="image" id="100" value="b"/>
<input type="radio" name="image" id="100" value="c"/>
<input type="radio" name="image" id="200" value="d"/>
<input type="radio" name="image" id="200" value="e"/>
<input type="radio" name="image" id="300" value="f"/>
<input type="radio" name="image" id="300" value="g"/>
<input type="radio" name="image" id="400" value="h"/>
and so on...
and this is another radio button set
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>200
<input type="radio" name="number" value="300"/>300
<input type="radio" name="number" value="400"/>400
<input type="radio" name="number" value="500"/>500
<input type="radio" name="number" value="600"/>600
my question is how can i compare value of one with value="100" to other with id="100" in php
there is a comparison i want to make but for the field with id = 100 i have values as abcd.. so i cant assign it value = 100 cuz i m saving it in database
means..
there are other fields with id=100 but their values shall be diffrent otherwise i m not able to identify them after they are send to databse
still i have to do comparison of both.. and that too serverside
so what are my options?
more detailed explanation....
there are two sets of radio buttons..
in one set there are values..
and in other sets there are images related to those values..
when user clicks set of value... and user click on set of images
so php shall match that image to the value.. if user clicked on 100 as value and later he clicks on an image under the id 200
database shall not allow it to enter and gave him some error
Sorry Had to see what your talking about. Help to edit this so to understand your requirement, 'shall match that image to the value'
<input type="radio" name="image" class="100" value="a_100"/>
<input type="radio" name="image" class="100" value="b_100"/>
<input type="radio" name="image" class="100" value="c_100"/>
<br>
<input type="radio" name="number" value="100"/>
<input type="radio" name="number" value="200"/>
<input type="radio" name="number" value="300"/>
<?php
$image = strstr($_POST['image'],'_');
$number = '_'.$_POST['number'];
if($image == $number) {
//Do something
}
The question is looking for to compare the two fields in PHP. Presumably this means that you want to examine them when the form is posted.
This means that you'll only have available to you the parts of the field that are sent via the POST -- ie the field name and the value. PHP will never see the id of any of the fields, nor the class, nor any other attributes in the HTML code other than name and value.
Therefore the discussion in the comments about whether or not to use id is a somewhat moot point -- even if it is a good point for you to know in general for your HTML code, you can't really use either id or class here anyway because PHP will never see them.
You need to take an entirely different approach.
[EDITED after question edit]
Add the 100 or 200, etc to the value of the image radio buttons, like so:
<input type="radio" name="image" value="a_100"/>
<input type="radio" name="image" value="b_100"/>
<input type="radio" name="image" value="c_100"/>
<input type="radio" name="image" value="d_200"/>
<input type="radio" name="image" value="e_200"/>
<input type="radio" name="image" value="f_300"/>
<input type="radio" name="image" value="g_300"/>
<input type="radio" name="image" value="h_400"/>
(The number radio button set remains unchanged from your question)
You can then read it in PHP as follows:
$number = $_POST['number'];
list($imgNumber, $imgValue) = explode('_',$_POST['image']);
You can now compare $number with $imgNumber.
Hope that helps.
It's worth asking a follow-up question though: What are you trying to achieve here? Is this a validation exersise? ie where you're checking that the user is selecting an image option that matches the number option he's picked? If that's what you're doing, you may find it's more user friendly to use Javascript to filter the options available when they select the number option so that the image radio button set only shows options that are valid for the selected number.
This would be an entirely different question, so I won't go into any detail here, but I would suggest that it might be a more user-friendly way of doing things if you did it like that.

PHP post variables titles

I am wondering how I can extract the title variable from my forms posted fields array.
I know $_POST['name'] is the field name but can I do something like $_POST[title] to get the title?
I ask because I have a dynamic form with variable lengths. The dynamics is group_one contains 5 fields, group_two contains 12 fields, group_three contains 2 fields for example.
I am hoping to loop through these groups and post the title of the form field to a column in a DB and its value. Any help appreciated with understanding if I can use the 'title' variable in the form field element.
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesYes" value="Yes" title="Dog at premises" />Yes
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesNo" value="No" title="Dog at premises" />No
<input type="text" name="txtGroupOne[]" id="txtNextOfKinName" title="Next of kin name" />
<input type="text" name="txtGroupOne[]" id="txtNextOfKinContact" title="Next of kin contact" />
No, only the name="x" attribute is sent by default in an HTML Form. title="x" is not sent. You can technically get around it with some crazy Javascript and an Ajax POST, but I would avoid that if I were you.
What do you want the title sent to your server side for? There surely probably a better alternative to achieve your goal.
You can add hidden field to each of fields that need titles to be sent to server besides their values.
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesYes" value="Yes" />
<input type="hidden" name="txtGroupOneTitles[]" value="Dog at premises" >
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesNo" value="No" />
<input type="hidden" name="txtGroupOneTitles[]" value="Dog at premises" >
<input type="text" name="txtGroupOne[]" id="txtNextOfKinName" />
<input type="hidden" name="txtGroupOneTitles[]" value="Next of kin name" >
<input type="text" name="txtGroupOne[]" id="txtNextOfKinContact" />
<input type="hidden" name="txtGroupOneTitles[]" value="Next of kin contact" >
You can do something like this:
foreach($_POST as $form_var){
// Compile your code here, etc.
}
Anything that does not have a "name" attribute in your form will not be part of the $_POST array.
ie,
<input type='text' id='bar'>
<input type='text' name='foo' id='foo'>
Only $_POST['foo'] would exist.
Misread the question slightly, but you could also prepend your input names to include the "group title" upon generation.
<input type='text' name='title1_foo' id='title1_foo'>
Then you can manipulate the information you want from the $_POST loop.

getting multiple checkboxes names/id's with php

How can i get the names or id's of the multiple selected checkboxes on submit, using the PHP? Following is example form. Thanks.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="orange" id="orange">
<input type="checkbox" name="apple" id="apple">
<input type="checkbox" name="sky" id="sky">
<input type="checkbox" name="sea" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
Checkbox values are submitted from a form only if the checkbox is selected. What's more, it's the name attribute that counts, not the ID.
There are several ways of handling checkboxes in PHP:
Give all checkboxes the same name followed by a pair of square brackets, so the entire set is treated as an array. In this case, give each checkbox a value.
Give each checkbox a different name and a value.
Give each checkbox a different name, but no value.
In each case, you need to check for the existence of the checkbox name in the $_POST array.
For example:
<input type="checkbox" name="color[]" id="orange" value="orange">
<input type="checkbox" name="color[]" id="apple" value="apple">
To get the values for these checkboxes:
if (isset($_POST['color'])) {
$colors = $_POST['color'];
// $colors is an array of selected values
}
However, if each checkbox has a different name and an explicit value like this:
<input type="checkbox" name="orange" id="orange" value="orange">
<input type="checkbox" name="apple" id="apple" value="apple">
You still need to use isset():
if (isset($_POST['orange'])) {
// orange has been set and its value is "orange"
}
If you don't set a value, the default value is "on", but it won't be in the $_POST array unless it has been selected, so you still need to use isset().
You need to give the inputs the same name:
<input type="checkbox" name="selection[]" value="orange">
<input type="checkbox" name="selection[]" value="apple">
<input type="checkbox" name="selection[]" value="sky">
<input type="checkbox" name="selection[]" value="sea">
Then iterate over the $_POST['selection'] array in PHP.
You won't get the ids but the names will be associative indexes in the $_POST array (and $_REQUEST). NOTE: They will only be available in the array if they were checked by the client.
if ($_POST['oragne'] == 'on')
You can set them up to post to PHP as arrays, if you build them similar to below:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="checkbox" name="box_group_1[oragne]" id="oragne">
<input type="checkbox" name="box_group_1[apple]" id="apple">
<input type="checkbox" name="box_group_1[sky]" id="sky">
<input type="checkbox" name="box_group_1[sea]" id="sea">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
print_r($_POST['box_group_1']);
?>

HTML Element Array, name="something[]" or name="something"?

I saw something on this site:
Handling array of HTML Form Elements in JavaScript and PHP
http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=343
It said to put the array in the name property and how to get the input collection's value. For example, name="education[]"
But as I know, an HTML input element is array-ready by name.
On the client-side (GetElementsByName) or server-side ($_POST in PHP or Request.Form in ASP.NET).
For example: name="education", so what is the different with or
without the []?
PHP uses the square bracket syntax to convert form inputs into an array, so when you use name="education[]" you will get an array when you do this:
$educationValues = $_POST['education']; // Returns an array
print_r($educationValues); // Shows you all the values in the array
So for example:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]">
</p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]">
</p>
<p><label>Please enter any previous education<br>
<input type="text" name="education[]">
</p>
Will give you all entered values inside of the $_POST['education'] array.
In JavaScript, it is more efficient to get the element by id...
document.getElementById("education1");
The id doesn't have to match the name:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]" id="education1">
</p>
If you have checkboxes, you can pass an array of checked values.
<input type="checkbox" name="fruits[]" value="orange"/>
<input type="checkbox" name="fruits[]" value="apple"/>
<input type="checkbox" name="fruits[]" value="banana"/>
Also multiple select dropdowns
<select name="fruits[]" multiple>
<option>apple</option>
<option>orange</option>
<option>pear</option>
</select>
It's different.
If you post this form:
<input type="text" name="education[]" value="1">
<input type="text" name="education[]" value="2">
<input type="text" name="education[]" value="3">
you will get an array in PHP. In this example you will get $_POST['education'] = [1, 2, 3].
If you post this form without [],
<input type="text" name="education" value="1">
<input type="text" name="education" value="2">
<input type="text" name="education" value="3">
you will get the last value. Here you will get $_POST['education'] = 3.

Categories