How to make select mandatory? and also select by default the first available option?

Asked

Viewed 109 times

0

I have a system in php that generates selects according to the database information, in case I would like to make these selects mandatory because if they do not go unnoticed and also always leave the first option selected as default.

below the code tree where the selects are created:

if(get_field('atributos')){
    $selects = array();
    $i=0;
    $atributos = explode(PHP_EOL,get_field('atributos'));

    foreach($atributos as $atributo){
        $opcoes = explode(':',$atributo);
        $name = $opcoes[0];
        if(!empty($opcoes[1])){
            $opcoes = explode(';',$opcoes[1]);
        }
        $selects[$i] = array('name'=>$name,'options'=>$opcoes); 
        $i++;
  • I don’t know how it works in wordpress but would try something like $selects[$i] = array('name'=>'teste' ,'options'=> 'algum valor', 'seletected' => true); before the foreach.

  • I’ll try! Thank you

  • Normally you have to put 'required' in the <select> tag, so giving Submit will validate which 'select' is empty

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.