Forum Moderators: coopster
I am using the htmlentities() function to protect a white space that is in a string variable's value retrieved from a mysql table.
The retrieved values are gotten from a loop as follows:
echo '<select name = \'payment_means\'>'.'<br>';
while(list($means) = mysqli_fetch_row($result))
{
print("<option value =".htmlentities($means).">".htmlentities($means)."</option>")."<br>";
}
That loop retrieves values as follows:
Bancontact
Proton
Visa Card
Cash
Library card
As those values are within a select html field, and the htmlentities() function is used to protect the white space that is for example in "visa Card" value, when I select "Visa card" to be sent to another table's column, I am surprised to see that the value is truncated to only "Visa". I see this when I echo the value of the variable used to collect the selected element to be submitted to the database. And as a result the value is not inserted in the destination table's column since it's an ENUM one with a legal value of "Visa Card" causing the truncated value of "Visa" to fail the insertion
Any suggestion of how to solve this? which function should perform better?
Any support is highly appreciated.
print("<option value =\"".htmlentities($means)."\">".htmlentities($means)."</option>")."<br>";
btw, htmlentities does not change the whitespace character to anything different.
I need a way that would help me to keep the empty space within the "Visa Card" value.
I tried a part of your solution by writing str_replace(' ', ' ', $means) and when I echo what from "Visa Card" being sent to the database i get still ONLY "Visa", the white space and the " card" part being truncated.
So i still need a way out here.
Any suggestion?
Sorry that you sent your solution while i was making my comment.
For your solution, not only it is correct but it's also INCREDIBLY EFFICIENT. Can't thank you enough. It is working as really expected.
The multiple double quotes you used are a bit difficult to feed into my understanding. (i know the usage of quotes might be a problem to some PHP programmers)
Basically, the code that you had there output the following HTML code to the browser:
<option [b]value =Visa Card[/b]>Visa Card</option>
What I did was add the quotes to the string so that they are echoed properly:
<option [b]value ="Visa Card"[/b]>Visa Card</option>
When you don't surround the value attribute with quotes and it has a space, it will trim off the remaining characters after the space. This is why you should check the generated html source when dealing with something like this because it can be easy to miss if you are looking directly at the php code.