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

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.

Related

How do i get values of multiple inputs in php

I am writing an sql embedded in php database.
I am facing a problem when dealing with user inputs. I have a php file where the user can select the type of search. For example, select all data entries or limit the search to a given id num, name, last name...
This the code where the user selects type of search
<select name="type" >
<option value="fm"> Faculty Member Search </option>
<tr><td>List all Faculty Members<input type="checkbox" name="list[]" value="listAll" >
<div>
<label class="textClick"> List By Id<input type="checkbox" name="list[]" value="listId" > </label>
<input class="text" type="text" size = "8" placeholder="Insert Id" name="listById" > </div>
<div>
<label class="textClick">List By First Name <input type="checkbox" name="list[]" value="listName" ></label>
<input class="text" type="text" placeholder="Insert Name" name="list" >
</div>
<div>
<label class="textClick">List By Last Name<input type="checkbox" name="list[]" value="listFname" ></label>
<input class="text" type="text" placeholder="Insert Last Name" name="listLast" >
</div>
</select>
<input type="submit" value="Get Info">
Now in the other php file (where search is actually done) i can't seem to get the values of the user's inputs.. This the part where my code goes wrong
if($_POST["type"]=="fm"){
Print "LIST IS" . $_POST["list"];
if($_POST['list']=="listAll")
{
?>
<h1> Listing ALL Faculty Members </h1>";
<?php
this code gave me the following output:
LIST IS
I tried both $_POST['list'] and $_POST["list"] but my code just doesn't seem to pass the if condition
$_POST['list'] is going to be an array of the submitted values. You will need to loop through the array to get the values or access them like any other array value (i.e. $_POST['list'][0]) to get the first element.
Do var_dump($_POST['list']) and you'll see the array and its values.
What you probably want is only one value and should change the checkboxes to radio buttons and remove the array syntax from the input field names:
<label class="textClick">List By First Name <input type="rado" name="list" value="listName" ></label>

Count the number of times a specific input is present in an form

Is there a way to count how many times an <input type="text" name="a"> appears in an form?
I have tried
$count = count($_POST['a']) ;
but this counts all inputs regardless the name of the input...
HTML forms shouldn't contain more than one <input> element with the same name attribute.
If you use the following markup:
<input type="text" name="a">
<input type="text" name="a">
then the value will get overridden and you'll just receive the value of the last <input> element in your $_POST array.
If you want to grab the inputs from multiple <input> elements, use the following markup instead:
<input type="text" name="a[]">
<input type="text" name="a[]">
<input type="text" name="a[]">
In this case, each of the <input> elements will be considered separately and you will be able to retrieve the values in the $_POST array.
To count the number of occurences of the <input> element, you can use the following:
echo count($_POST['a']);
Consider the following code:
<?php
if (isset($_POST['submit'])) {
print_r($_POST);
echo count($_POST['a']);
}
?>
<form action="" method="post">
<input type="text" name="a[]">
<input type="text" name="a[]">
<input type="text" name="a[]">
<input type="submit" name="submit">
</form>
This will correctly display the number of <input> elements with the name attribute equal to a.
The answer is simple, it will just appear once. The name attribute MUST be unique for all text input fields unless you append square brackets [] to the name. Otherwise the last one in the form will overwrite the value from the others.
if you named input name as array a
<input type="text" name="a[]">
<input type="text" name="a[]">
<input type="text" name="a[]">
then you will get counter as
echo count ($_POST['a']);
Output: 3
otherwise counter 1

How to capture form text fields as an array like a radio group

I hope I ask this question correctly, and if not please direct me how to repair it. I have had it deleted as a post once already...
My goal is to submit a form with one drop down, with numbers like 100, 200, 300 (for how many T-shirts you want to order)... Then depending on what is selected from the drop down have a series of text boxes (for number placement) that must add up to the selected number of shirts you want to order from the dropdown.
My idea is to capture all these text fields in an array, and send them off to a function to be added...
Can someone assist me please?
Here is the form code I know does not work, but I want it to work...
<form>
<label>
<input type="checkbox" name="PoloDesign" value="100" id="PoloDesign_0" />
100</label>
<br />
<label>
<input type="checkbox" name="PoloDesign" value="200" id="PoloDesign_1" />
200</label>
<br />
<label>
<input type="checkbox" name="PoloDesign" value="300" id="PoloDesign_2" />
300</label>
<br />
<input type="text" name="name[1]" id="name1" value="{$name1}"/>
<input type="text" name="name[1]" id="name2" value="{$name2}"/>
<input type="text" name="name[1]" id="name3" value="{$name3}"/>
<input type="text" name="name[1]" id="name4" value="{$name4}"/>
<input type="text" name="name[1]" id="name5" value="{$name5}"/>
<input type="text" name="name[1]" id="name6" value="{$name6}"/>
<input type="text" name="name[1]" id="name7" value="{$name7}"/>
<input type="submit" value="submit"/>
</form>
Just change each
name="name[1]"
To
name="name[]"
Then the fields are posted as an array you can iterate through in PHP
if (is_array($_POST['name']):
foreach ($_POST['name'] as $key=>$field):
// do something here
$yourKey = $key +1;
$yourValue = $field;
I have changed your code a little and tried to make it work using regular expression:
<?php
$name_array = preg_grep('/name[1-9]*/', $_GET);
?>
So, basically it checks all submitted variables and creates array from all variables that have name at start and a number at end. So, the form part should change to look like this:
<input type="text" name="name1" id="name1" value="{$name1}"/>
<input type="text" name="name2" id="name2" value="{$name2}"/>
<input type="text" name="name3" id="name3" value="{$name3}"/>
<input type="text" name="name4" id="name4" value="{$name4}"/>
<input type="text" name="name5" id="name5" value="{$name5}"/>
<input type="text" name="name6" id="name6" value="{$name6}"/>
<input type="text" name="name7" id="name7" value="{$name7}"/>
I tested on Apache2 and PHP 5.3

Many fields in HTML form

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 ..

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']);
?>

Categories