Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
Why the command echo php print badly lines html?

I am new on the php but I am trying to build a page that produces after having read something from the database the inherent checkboxes, but when these are printed with echo the formatting it is not the desired one producing therefore of the errors for the carriage return. Here is the php

<?php 

$conn = mysqli_connect("localhost", "username", "password", "database_name");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$query="SELECT * FROM ingrediente";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {

   $name = $row["id_ingrediente"];
   $value = $row["immagine"];
   $etichetta = $row["nome"];
   $prezzo = $row["costo_unitario"];
    echo "\n";
    echo "<input type='checkbox' name=$name value='$value' onchange=\"add_ing2($name,'$value','$etichetta',$prezzo,this);\">$etichetta" ;
    echo "\n";
    echo "<input type='number' name='$etichetta' style='border:0px; text-align:right;background:transparent; height:auto;' value=$prezzo readonly> €";
    echo "\n";
    echo "<br><br>";

  }
} else {
echo "0 results";
}

mysqli_close($conn);

?>

Here is the produced html

<input type='checkbox' name=1 value='/images/ingredienti/pomodoro.png' onchange="add_ing2(1,'/images/ingredienti/pomodoro.png','Pomodoro',0.3,this);">Pomodoro
<input type='number' name='Pomodoro' style='border:0px; text-align:right;background:transparent; height:auto;' value=0.3 readonly> €
<br><br>
<input type='checkbox' name=2 value='/images/ingredienti/formaggio.png
' onchange="add_ing2(2,'/images/ingredienti/formaggio.png
','Formaggio',0.2,this);">Formaggio
<input type='number' name='Formaggio' style='border:0px; text-align:right;background:transparent; height:auto;' value=0.2 readonly> €
<br><br>
<input type='checkbox' name=3 value='/images/ingredienti/hamburger.png
' onchange="add_ing2(3,'/images/ingredienti/hamburger.png
','Hamburger',1,this);">Hamburger
<input type='number' name='Hamburger' style='border:0px; text-align:right;background:transparent; height:auto;' value=1 readonly> €
<br><br>

As you can see the correct way to print has been applied only to the first element (Pomodoro (line 1:2 in html code)). Since this causes me more than few problems when then use the checkboxes, as can I resolve?

Thank you so much

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Nothing wrong with PHP - the data you're pulling out of the database is just bad. Some of your image paths have a new-line character at the end.

You should ultimately fix the data (validating user input, adjusting scripts that pull it in automatically, etc.), but you can apply trim() to the values to strip off the extra character(s).

over 4 years ago · Santiago Trujillo Denunciar

0

Wrong assumption! Your $value seems too have an line-break at the end. You can see that the line breaks after the image url. So look for an \n at the image value. Tip: $value = trim($value); [update] Do trim() on all used variables. – @JustOnUnderMillions

Looks like you've got bad data with newlines in it. Take a look in the database - I'll bet you'll see them there in the raw data. You can try trim()ing the values, but better to fix the underlying issue. – @ceejayoz

I applied the trim() to all the variables and it works!!! Thx so much JustOnUnderMillion and ceejayox <3

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda