The list of all PHP and Mysql posts

how to make a drop off menu

Drop-off menu is one of the most effective form elements. User can select his choice easilly , and this kind of element takes very small room .But the html codes to generate a drop-off menu are a little boring to add. So here , we show a function that you can summon anywhere you want to add a drop-off menu.
Add the following codes to "funcs.php" (the file to hold the functions of your website , you will include it to the page that you want to summon the functions in it).

code:

function generate_menu($name,$options,$default =""){

// the function has three variables that should be sent to it. variable "$name" is the name of the drop-off menu that you want to creat , variable "$options" is an array that contains the options that are shown in the Drop-off menu , Variable "$default" is an optional variable (it can be remained unvalued) , it's the default value of the drop-off menu.

// the following codes produce the drop-off menu html and return it ($html) as the output.

$html = "<SELECT NAME=\"$name\">";
foreach($options as $value => $label){
 $html .="<OPTION ";
 if($value == $default)
 $html .="SELECTED ";
 $html .="VALUE=\"$value\">$label</OPTION>";
 }
$html .= "</SELECT>";
return($html);
}

Now , we have the function to generate a dropp-off menu , to summon the function use the following codes in the page that you want to have a drop-off menu.
Assume , you want to have a drop-off menu with the name of "field" and with these five options , "satire,drama,tragic,joke,others"

code:

<?php
$options = array("satire" => "SATIRE","drama" => "DRAMA","tragic" => "TRAGIC"
,"joke" => "JOKE","others" => "OTHERS");
// Options variable is an array of options


$default= "satire";
$html = generate_menu("field",$options,$default); // summon the function
echo $html;
?>


the result of these codes is look like the image.

No comments:

Post a Comment