Home > Enterprise >  Concatenate 2 php strings for <select> option value
Concatenate 2 php strings for <select> option value

Time:07-09

I need to concatenate 2 fields, which I'm getting from mysql select query, which are name and last name,

I expect something like:

<option value="Rodrigo Perez Garcia">Rodrigo Perez Garcia</option>

Instead, i'm getting:

<option value="Rodrigo" perez="" garcia="">Rodrigo Perez Garcia</option>

This is my code:

 $sql = "SELECT * FROM instructores";
 $sql_run = mysqli_query($con, $sql);

 while($row = mysqli_fetch_array($sql_run)){
 $nombreCompleto = $row['nombre']. " ".$row['apellidos'];
 echo '<option value='.$nombreCompleto.'>'.$nombreCompleto.'</option>';

I wan't to have both fields separately, which is why I don't concatenate both from the beginning in the INSERT.

Thanks to everyone, double quotes solve the problem.

CodePudding user response:

You're missing the quotes around the value attribute value.

 echo '<option value="'.$nombreCompleto.'">'.$nombreCompleto.'</option>';

CodePudding user response:

Modify from

echo '<option value='.$nombreCompleto.'>'.$nombreCompleto.'</option>';

to

echo '<option value="'.$nombreCompleto.'">'.$nombreCompleto.'</option>';

you did not include the double quotation marks.

Cheers

CodePudding user response:

Your concatenated name has spaces in it, which is confusing the HTML renderer. You need to wrap the name in quotes.

echo '<option value="' . $nombreCompleto . '">' . $nombreCompleto . '</option>';

Which outputs:

<option value="Rodrigo Perez Garcia">Rodrigo Perez Garcia</option>
  • Related