Forum Moderators: coopster

Message Too Old, No Replies

htmlentities() function not performing well

         

dbarasuk

7:09 am on Mar 21, 2008 (gmt 0)

10+ Year Member



Hi!

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.

omoutop

9:05 am on Mar 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you can use str_replace() function.

Example:
$my_value = "Visa Card";

$a = str_replace(" ", "_", $my_value) // this will give you Visa_Card

and also

$b = str_replace("_", " ", $a) // this will give you Visa Card (back ot original form)

eelixduppy

7:46 pm on Mar 21, 2008 (gmt 0)



Your problem has nothing to do with you using htmlentities. It is because you aren't surrounding your value attribute with quotes that it breaks at the space. Sloppy HTML always gets you in the end :) Try the following:

print("<option value =\"".htmlentities($means)."\">".htmlentities($means)."</option>")."<br>";

btw, htmlentities does not change the whitespace character to anything different.

dbarasuk

7:52 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



Your solution wouldn't work since Visa_card would not match the legal ENUM value "Visa Card" mentionned during the table definition.

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?

eelixduppy

9:36 pm on Mar 21, 2008 (gmt 0)



Did you actually try my suggestion? If so, did it work properly?

dbarasuk

11:49 pm on Mar 21, 2008 (gmt 0)

10+ Year Member



Dear EELIXDUPPY,

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)

eelixduppy

12:15 am on Mar 22, 2008 (gmt 0)



If you are having trouble understanding what I actually did to the string, you might want to refer to the documentation on strings at php.net: [php.net...]

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.