0

In input type text i am getting just one string. for ex if in database name field have "abc xyz". So its only showing abc not full name this happen for all the fields. Please check my code also.

        while($row = mysqli_fetch_array($result))  
        {
            //print_r($row['name']);
            echo "<tr>";
            echo "<td> ". $row['id'] . "</td>";
            echo "<td> <input type=text placeholder = name onchange= nameValidation(this) value = ".$row["name"]." disabled> </td>";

            echo "<td> <input type='email' placeholder = 'email' value = ". $row['email'] ." onchange= nameValidation(this) disabled> </td>";

            echo '<td> <input type="button" value="edit" onclick="edit(this)">
                <input type="button" value="update" onclick="update(this)" style="display:none;">
                <input type="button" value="Delete" onclick="delete1(this)" style="display:none;">
            </td>';

            echo "</tr>";
        }
        echo "</table>";
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

4

The HTML specification says:

Attributes are placed inside the start tag, and consist of a name and a value, separated by an "=" character. The attribute value can remain unquoted if it doesn't contain ASCII whitespace or any of " ' ` = < or >. Otherwise, it has to be quoted using either single or double quotes. The value, along with the "=" character, can be omitted altogether if the value is the empty string.

In this case, the text "abc xyz" does contain a white space and must be quoted. But it is not in your code. After PHP is done with parsing, your input tag will be:

<input type=text placeholder = name onchange= nameValidation(this) value = abc xyz disabled>

Where, "abc" will be interpreted as value and "xyz" will be interpreted an another unknown attribute.

To rectify this, you need to quote the value with \" as follows:

echo "<td> <input type=text placeholder=name onchange=nameValidation(this) value=\"" . $row["name"] . "\" disabled> </td>";

Or you can switch to single quotes:

echo '<td>';
echo '<input type="text" placeholder="Enter your name" onchange="nameValidation(this)" value="' . $row['name'] . '" disabled>';
echo '</td>';
Jomoos
  • 12,823
  • 10
  • 55
  • 92
  • 1
    such a simple solution but I never realized it. till now I use to think $row['name']; is equal to " '.$row['name'].' " and that its just the convention to write it in different ways depending upon the quotes. thanks for explaing the difference. – BILAL MALIK Feb 21 '19 at 05:20
  • @Jomoos if you'd like more people to see your excellent explanation, you might post the crux of your answer to the canonical that I've just closed this page with. If you answer that page, please taylor your answer to that specific question context. – mickmackusa Apr 10 '22 at 11:13