How to make Select option save the entire string of characters and not just the first word?

2

I use this code

<select name="escuelaprocedencia" class="form-control">
    <option value="">- Selecciona -</option>
    <?php
    $conn = mysql_connect("localhost","radx","sands");
    mysql_select_db("softn_cms",$conn);
    $consulta_mysql='select * from escuela_proce';

    $resultado_consulta_mysql=mysql_query($consulta_mysql,$conn);

    while($fila=mysql_fetch_array($resultado_consulta_mysql)) {
        echo "<option value=".$fila['nombre_escuela']."'";
        if($fila['nombre_escuela']== $ni) {
            echo " selected='true'";
        }
        echo">".$fila['nombre_escuela']."</option>";
?>
</select>

What I do is load the select option with the database. To edit the select the option previously chosen must be shown by a condition, only to choose again only saves the first word and not the entire chain.

    
asked by Radx 28.11.2018 в 07:25
source

2 answers

1

I strongly suggest that you give up the horrible mix of PHP / HTML code. Honestly it is something that should be forbidden.

When you need HTML code inside a PHP block it is better to use a concatenation variable to build all the content in it. You will gain clarity in the code.

Is that you have to break your head well to analyze this and see where the error is:

 echo "<option value=".$fila['nombre_escuela']."'";
 if($fila['nombre_escuela']== $ni){echo " selected='true'";}
 echo">".$fila['nombre_escuela']."</option>";

I propose this solution in which:

  • We are going to work all the time inside a PHP block, to avoid that horrible, confusing and unanalizable PHP / HTML mix
  • Let's capture the value of the nombre_escuela column in a variable
  • We are going to use a ternary to add or not selected to the option that should take it. Just put selected , it is not necessary to put selected=true . When selected is omitted it equals false and when it is set equals true ( see W3C Recommendation about this.)
  • We're going to use fetch_assoc , because it's more specific that fetch_array , since this one brings you two results for each column, loading much more memory ( see note at end of this answer ).

This is the code, I think you can see the difference in terms of clarity:

<?php
    $html='<select name="escuelaprocedencia" class="form-control">';
    $html.='<option value="">- Selecciona -</option>';
    $conn = mysql_connect("localhost","radx","sands");          
    mysql_select_db("softn_cms",$conn);
    $consulta_mysql='select * from escuela_proce';
    $resultado_consulta_mysql=mysql_query($consulta_mysql,$conn);
    while($fila=mysql_fetch_array($resultado_consulta_mysql)){
        $nombre=$fila['nombre_escuela'];
        $value=( $nombre == $ni ) ? "value=\"$nombre\" selected" : "value=\"$nombre\"";
        $html.="<option $value>$nombre</option>";
    }
    $html.="</select>";
    echo $html;
?>
    
answered by 28.11.2018 / 16:42
source
1

I see that you have a closing quote at the end of the value on this line, but it does not have the opening quote:

echo "<option value=".$fila['nombre_escuela']."'";

Maybe that's what makes the data go wrong, could you correct it like that and try?

Add a single quote next to the = symbol.

    
answered by 28.11.2018 в 17:11