Search code examples
phphtmlformspostform-submit

Php Post method not passing string after space character


I am having trouble with the POST method after submitting a form. It is not passing the complete string after submitting the form, but it shows correctly in the "select" & "option" html.

in the database i have a table called "administradores" with a column called "id" and other called "administrador": id: 1 administrador: "Alejandro Salgado"

this is the php code:

function listAdministradores(){
global $conn;
$result = $conn->query("SELECT * FROM administradores");
while($row = $result->fetch_assoc()){
    echo '<option value='.$row["administrador"].'>'.$row["administrador"].'</option>';
}

}

this is the html code:

    <form action="./includes/crearpropuesta.inc.php" method="post">
    <label for="administrador" class="form-label pt-3">Administrador:</label>
    <select name="administrador" id="administrador" class="form-select">
        <?php listAdministradores() ?>
    </select>

And it shows the name "Alejandro Salgado" correctly:

html result

but when I submit the form and var_dump($_POST["administrador"]), it returns only the first name "Alejandro":

string(9) "Alejandro"

What is going on?


Solution

  • To include characters separated by spaces, you need to enclose the data with quotation marks (in your case double-quotation)

    Hence, please change

    echo '<option value='.$row["administrador"].'>'.$row["administrador"].'</option>';
    

    to

    echo '<option value="'.$row["administrador"].'">'.$row["administrador"].'</option>';