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>
</select>

if($_POST['submit'])
{
$animal=$_POST['animal'];
}

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.

 Answers

5
$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
4

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

PHP:

$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...
        if($a['parent']===$parentID){ 
            $str.='<li>'.$a['title'];

                    //Pass this item's ID to the function as a parent, 
                    //and it will output the children
            $subStr = output_lis($a['id']);
            if($subStr){
                $str.='<ul>'.$subStr.'</ul>';
            }

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

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

Then output this on your page. Something like:

<ul>
    <?php echo output_lis(); ?>
</ul>

Here is my sample array:

$arr = array(
        array('title'=>'home','parent'=>NULL,'id'=>1), 
        array('title'=>'sub1','parent'=>1,'id'=>2), 
        array('title'=>'sub2','parent'=>1,'id'=>3), 
        array('title'=>'about us','parent'=>NULL,'id'=>4), 
        array('title'=>'sub3','parent'=>4,'id'=>5), 
        array('title'=>'sub4','parent'=>4,'id'=>6), 
    );
Wednesday, August 10, 2022
2

Fetch

//td[@class='name']/a

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

//td[@class='name']/a/@href|//td[@class='name']

as well.

Saturday, October 8, 2022
 
caprica
 
3

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::SELF_FIRST,
    RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);

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

print_r($paths);

This generates the following output on my computer:

Array
(
    [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
 
2

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>
</select>

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:

<?php
$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 } ?>
</select>
Sunday, November 20, 2022
 
ayzen
 
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 :