Viewed   81 times
<select id="animal" name="animal">                      
<option value="0">--Select Animal--</option>
<option value="1">Cat</option>
<option value="2">Dog</option>
<option value="3">Cow</option>


I have a dropdown like this. What I want, I want to get selected value and text in button submit using PHP. I mean if it's selected 1st one. I want to get both 1 and Cat

Please help. thank you.


$animals = array('--Select Animal--', 'Cat', 'Dog', 'Cow');
$selected_key = $_POST['animal'];
$selected_val = $animals[$_POST['animal']];

Use your $animals list to generate your dropdown list; you now can get the key & the value of that key.

Saturday, November 5, 2022

You need to write a recursive function to do this and have it call itself. I haven't tested this out, but I think it should get you started. I wouldn't endorse this function's efficiency since it runs through every item in the array and does a comparison even though you're going to only need an item or two from each run (likely).


$arr = array(...);
function output_lis($parentID = NULL){
    global $arr;
    $stack = array(); //create a stack for our <li>'s 
    foreach($arr as $a){ 
        $str = '';
            //if the item's parent matches the parentID we're outputting...

                    //Pass this item's ID to the function as a parent, 
                    //and it will output the children
            $subStr = output_lis($a['id']);

            $stack[] = $str;
    //If we have <li>'s return a string 
        return join("n",$stack);

    //If no <li>'s in the stack, return false 
    return false;

Then output this on your page. Something like:

    <?php echo output_lis(); ?>

Here is my sample array:

$arr = array(
        array('title'=>'about us','parent'=>NULL,'id'=>4), 
Wednesday, August 10, 2022



and then pluck the text with nodeValue and the attribute with getAttribute('href').

Apart from that, you can combine Xpath queries with the Union Operator | so you can use


as well.

Saturday, October 8, 2022

RecursiveDirectoryIterator should do the trick. Unfortunately, the documentation is not great, so here is an example:

$root = '/etc';

$iter = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"

$paths = array($root);
foreach ($iter as $path => $dir) {
    if ($dir->isDir()) {
        $paths[] = $path;


This generates the following output on my computer:

    [0] => /etc
    [1] => /etc/rc2.d
    [2] => /etc/luarocks
    [17] => /etc/php5
    [18] => /etc/php5/apache2
    [19] => /etc/php5/apache2/conf.d
    [20] => /etc/php5/mods-available
    [21] => /etc/php5/conf.d
    [22] => /etc/php5/cli
    [23] => /etc/php5/cli/conf.d
    [24] => /etc/rc4.d
    [25] => /etc/minicom
    [26] => /etc/ufw
    [27] => /etc/ufw/applications.d
    [391] => /etc/firefox
    [392] => /etc/firefox/pref
    [393] => /etc/cron.d
Wednesday, August 3, 2022

You have to set the selected attribute for the option that was submitted. So you have to check the submitted value for each option. In my solution I am using the ternary operator to echo the selected attribute only for the correct operator.

<select name="operator">
    <option value="add" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'add') ? 'selected' : ''; ?>>+</option>
    <option value="minus" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'minus') ? 'selected' : ''; ?>>-</option>
    <option value="divide" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'divide') ? 'selected' : ''; ?>>/</option>
    <option value="multiply" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'multiply') ? 'selected' : ''; ?>>x</option>

The code above is somewhat repetitive. It keeps repeating a lot of code and html. It would be great if we could factor out the repetitive stuff. Luckily we can do that by creating an array that stores the options and loop through them using a foreach, like this:

$options = [
    'add' => '+',
    'minus' => '-',
    'divide' => '/',
    'multiply' => 'x'

<select name="operator">
    <?php foreach ($options as $key => $label) { ?>
        <option value="<?= $key ?>" <?= (isset($_POST['operator']) && $_POST['operator'] == $key) ? 'selected' : '' ?>><?= $label ?></option>
    <?php } ?>
Sunday, November 20, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :