Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. /**
  2.     * function to create a html select box and set the selected value if the item is found in post
  3.     *
  4.     * @param string $elementId and id of html element
  5.     * @param array $options of items to add to select box value => display text
  6.     * @param string $attributes of attributes to add e.g class=''
  7.     * @return string
  8.     */
  9.     function createSelectHtml($elementId, $options, $attributes=''){
  10.         $returnHtml="";
  11.         //start building return html
  12.         $returnHtml.="\n<select name='{$elementId}' id='{$elementId}'>\n";
  13.         //default selected item to blank (if not found defaults to 1st item)
  14.         $selectedValue='';
  15.         //check if a post has been sent containing this item
  16.         if ($_POST && isset($_POST[$elementId])){
  17.             //a post has been done set selected to post value
  18.             $selectedValue=$_POST[$elementId];
  19.         }
  20.         //loop through options adding to html
  21.         foreach ($options as $id => $value){
  22.             //check if the item we are adding is the selected item
  23.             if ($id==$selectedValue){
  24.                 $isSelected=" selected='selected'";
  25.             }else{
  26.                 $isSelected="";
  27.             }
  28.             //add this option to the return Html
  29.             $returnHtml.= " <option value='{$id}'{$isSelected}>{$value}</option>\n";
  30.         }
  31.         //end select box
  32.         $returnHtml.= "</select>\n";
  33.         //if the item has been submitted check if it is valid
  34.         if ($_POST){
  35.             //display error message
  36.             if (!selectIsValid($elementId, $options)){
  37.                 $returnHtml.= "<span class='error'>Please select a valid option from the list provided.</span>\n";
  38.             }
  39.         }
  40.         //return html
  41.         return $returnHtml;
  42.     }
  43.     /**
  44.     * function to check if a valid option has been posted to a form
  45.     *
  46.     * @param string $elementId and id of html element  
  47.     * @param array $options of items to add to select box value => display text
  48.     * @return boolean
  49.     */
  50.     function selectIsValid($elementId, $options){
  51.         $isValid=false;
  52.         //loop through options adding to html
  53.         foreach ($options as $id => $value){
  54.             if ($id!='' && $id==$_POST[$elementId]){
  55.                 $isValid=true;
  56.             }
  57.         }
  58.         return $isValid;
  59.     }
  60.  
  61. <form method="post">
  62. <?php
  63.     $validOptions=array('' => 'Please select', 1 => 'Matt', 2 => 'John', 3 => 'Smith');
  64.     echo "<label>Please select:</label>" . createSelectHtml('aSelectBox', $validOptions) . "";
  65.     echo "<p>&nbsp;</p>";
  66.     echo "<label>Please select:</label>" . createSelectHtml('aSelectBox1', $validOptions) . "";
  67.     echo "<p>&nbsp;</p>";
  68.     if ($_POST){
  69.         if(selectIsValid('aSelectBox', $validOptions)){
  70.             echo "<p>The form is valid</p>";
  71.         }
  72.     }
  73. ?>
  74. <input type="submit">
  75. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement