简体   繁体   中英

String cut, trim at first space character

i'm trying to select values from the database and place each and every one on a separate text input field . my code works properly, however, when i tried to display a string on a text field, the string is cut or trimmed on its very first space character. for example:

value #1: "my-picture.jpg"

value #2: "my name"

if i were to place the two values above and insert them inside a text field, the output would be like this:

value #1: my-picture.jpg
value #2: my

this is the code i'm working on:

<?php 
$counter = 0;
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
echo "
<input type='text' value=".$newArray['my_picture'].">
<input type='text' value=".$newArray['my_name'].">
";
?>

What is wrong? Thanks for any help.

You need to quote your values in your HTML form:

echo '
<input type="text" value="'.$newArray['my_picture'].'">
<input type="text" value="'.$newArray['my_name'].'">
';

Also for good measure it would be a good idea to use htmlentities() or htmlspecialchars() on the values in your variables to encode special characters:

echo '<input type="text" value="' . htmlentities($newArray['my_picture']) . '">
      <input type="text" value="' . htmlentities($newArray['my_name']) . '">';

Try viewing the source.. That says:

<input type='text' value=the content of your variable>

Where 'the content of your variable' needs to be surrounded by quotes, naturally. So; change it into:

<?php 
$counter = 0;
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
    echo '<input type="text" value="' . $newArray['my_picture'] . '">';
    echo '<input type="text" value="' . $newArray['my_name'] . '">';
}
?>

You need to surround your value with quotes. Something like

echo "
  <input type='text' value='".$newArray['my_picture']."'>
  <input type='text' value='".$newArray['my_name']."'>
";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM