PHP - Pattern in Datalist

Asked

Viewed 117 times

2

I want to make a datalist field where the user can type to find what they want faster, but I do not want the user to be able to put something outside the suggested values, so I am using Pattern, but the code is not working and I could not find the reason.

<?php 
        $select = "SELECT cod_categoria, desc_categoria
        FROM categoria
        ORDER BY cod_categoria";
        $result = $mysqli->query($select);

        echo "<input list='categoria' name='cat_prod' class='form-control' pattern='";                                                                                                                                              
        while($row = $result->fetch_assoc()){
              echo "".$row['desc_categoria']."|";                                                           
        }                                                   
        echo "' required>";                                                                                                                                                                                                                                                                             

        echo "<datalist id='categoria'>";
        while($row = $result->fetch_assoc()){
            echo "<option value=".$row['desc_categoria']."/>";                                                          
        }   
        echo "</datalist>";
?>

Another question is regarding the second while, I want you to repeat the same amount of times as the first, could put just while($Row){ } ?

1 answer

1


Datalist is not yet compatible with all browsers. As you want to list predefined categories, the best option would be to use the tag select, instead of input with datalist. Your code would look like this:

<?php 
    $select = "SELECT cod_categoria, desc_categoria
    FROM categoria
    ORDER BY cod_categoria";
    $result = $mysqli->query($select);

    echo "<select name='cat_prod' class='form-control'>";                                                                                                                                              
    while($row = $result->fetch_assoc()){
          echo "<option value='" . $row['desc_categoria'] . "'>" 
               . $row['desc_categoria'] . "</option>";                                                           
    }                                                   
    echo "</select>";
?>

One possible reason why your code didn’t work was the lack of (' ') delimiters in options, causing categories with spaces to cause problems. The second problem has to do with your second question, as you are trying to use the result of your query twice. There are numerous solutions, but I will save in an array called $categories and then present the values using the implode and the foreach:

<?php 
    $select = "SELECT cod_categoria, desc_categoria
    FROM categoria
    ORDER BY cod_categoria";
    $result = $mysqli->query($select);

    while($row = $result->fetch_assoc()){
          $categorias[] = $row['desc_categoria'];                                                                                                             
    }

    echo "<input list='categoria' name='cat_prod' class='form-control' pattern='" 
         . implode('|', $categorias) . "' required>";                                                                                                                                                                                                                                                                             

    echo "<datalist id='categoria'>";
    foreach($categorias as $categoria){
        echo "<option value='".$categoria."'/>";                                                          
    }   
    echo "</datalist>";
?>
  • Thank you very much, I had already done with the select that I knew worked, as in your first example. But I tested the second example using foreach and implode and it worked. The problem was just the lack of ""'separators.

Browser other questions tagged

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