Accessing Form Input with User-Defined Arrays or Working with PHP and arrays of data

Accessing Form Input with User-Defined Arrays

or

Working with PHP and arrays of data

  • Supergloabal variable is a pre-defined associative array created by PHP that holds all input data submitted to a PHP script via the GET  or POST method.
  • One can have arrays created in $_GET / $_POST by using a square-bracket syntax when specifying the name attribute for form input controls. For example:
    • <input type="text" name="address[city]" />
  • Variable interpolation in double-quoted strings can be used to output the values of both indexed and associative arrays. There is both a 'simple' and 'complex' syntax available for this.
  • Multi-dimensional array values in double-quoted strings should be output using the complex, or 'curly brace', syntax.

<fieldset>

<input type="text" name="item[1]" />

<input type="text" name="item[2]" />

<input type="hidden" name="fset[]"/>

</fieldset>

example:- 

<?php

$vowels  = array("A","E","I","O", “U”);

foreach($vowels  as $ vowel){

    echo $ vowel."<br>\n";

}

$tools = array("hammer","tongs","screwdriver");

foreach($tools as $tool){

    echo $tool."<br>\n";

}

$stunames = array("RAM","MOHAN","SITA","MIRA");


foreach($stunames as $sname){

    echo $sname."<br>\n";

}

?>

 example:- 

function AddToArray ($post_information) {

    //Create the return array

    $return = array();

    //Iterate through the array passed

    foreach ($post_information as $key => $value) {

        //Append the key and value to the array, e.g.

            //$_POST['keys'] = "values" would be in the array as "keys"=>"values"

        $return[$key] = $value;

   }

    //Return the created array

    return $return;

}

The test with:

if (isset($_POST['submit'])) {

    var_dump(AddToArray($_POST));

}

Example:-

<?php
if ($_POST) {
    echo '<pre>';
    echo htmlspecialchars(print_r($_POST, true));
    echo '</pre>';
}
?>
<form action="" method="post">
    Name:  <input type="text" name="personal[name]" /><br />
    Email: <input type="text" name="personal[email]" /><br />
    Beer: <br />
    <select multiple name="beer[]">
        <option value="warthog">Warthog</option>
        <option value="guinness">Guinness</option>
        <option value="stuttgarter">Stuttgarter Schwabenbräu</option>
    </select><br />
    <input type="submit" value="submit me!" />
</form>


Save this file abcd.php and run

Output: -

When working with elements such as checkboxes because the user chooses one or more items.

If you name the checkbox INPUT element with a plain name, like so

<input type=”checkbox” id=”products” name=”products”>

The script that receives this data has access to only a single value corresponding to this name ($_POST[‘products’]) (and therefore only the first checkbox in the list that the user selected). You can change this behavior by renaming an element of this kind so that its name ends with an empty set of square brackets.

Example:- An HTML Form Including Multiple Check Boxes

 <!DOCTYPE html>

 <html>

 <head>

 <title>An HTML form with checkboxes</title>

 </head>

 <body>

 <form action=”send_formwithcb.php” method=”POST”>

 <p><label>Name:</label><br/>

 <input type=”text” name=”user” /></p>

 <fieldset>

 <legend>Select Some Products:</legend><br/>

 <input type=”checkbox” id=”tricorder”

 name=”products[]” value=”Tricorder”>

 <label for=”tricorder”>Tricorder</label><br/>

 <input type=”checkbox” id=”ORAC_AI”

 name=”products[]” value=”ORAC AI”>

 <label for=”ORAC_AI”>ORAC AI</label><br/>

 <input type=”checkbox” id=”HAL_2000”

 name=”products[]” value=”HAL 2000”>

 <label for=”HAL_2000”>HAL 2000</label>

 </fieldset>

 <button type=”submit” name=”submit” value=”submit”>Submit Form</button>

 </form>

 </body>

 </html>

Put these lines into a text file called formwithcb.html and place that file in your web server document root. Next, in the script that processes the form input, you find that the value of all checked checkboxes with the “products[]” name is available in an array called $_POST[‘products’]. That the user’s choices are made available is demonstrated in an array.

Reading Input from the Form in Listing

<!DOCTYPE html>

 <html>

 <head>

 <title>Reading checkboxes</title>

 </head>

 <body>

 <p>Welcome, <strong><?php echo $_POST[‘user’]; ?></strong>!</p>

 <p>Your product choices are:

 <?php

 if (!empty($_POST[‘products’])) {

 echo “<ul>”;

 foreach ($_POST[‘products’] as $value) {

 echo “<li>$value</li>”;

 }

 echo “</ul>”;

 } else {

 echo “None”;

 }

 ?>

 </body>

 </html>

Put these lines into a text file called send_formwithcb.php and place that file in your web server document root. Now access the form (formwithcb.html) with your web browser and check some checkboxes.



The looping technique is particularly useful with checkboxes, it can also work with other types of form elements.

For example, if you use a SELECT element that allows for multiple selections, you are also enabling a user to choose many values within a single field name.

As long as the name you choose ends with empty square brackets, PHP compiles the user input for this field into an array.

=================================================


Post a Comment

0 Comments