Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outputting text with single quotes 1

Status
Not open for further replies.

mamabird

Programmer
Dec 19, 2006
15
US
I have a form, the contents of which gets emailed to someone. The form has a textarea and several radios.

If the user enters "John O'Connor" in the textarea the outputted value is "John O\'Connor".

One of the values in one of the radios is "Don't Know". If selected the outputted value is "Don".

I have tried a variety of functions like htmlentities(), htmlspecialchars(), etc and neither problem gets corrected.

I am new to PHP but I was handed a PHP website and asked to complete this task, so I haven't had enough time to go through a bunch of tutorials. I apologize if this is a simple question but I can't figure it out and need help soon.

Thank you!!
 
To me it looks like you are enclosing the value of the radio button in single quotes, making your html confused as to what is the value and what not. Can you show us the code you have for the form?

___________________________________________________________
[small]Do something about world cancer today: PACT[/small]
 
on the first issue, i suspect that you have magic_quotes_runtime or magic_quotes_gpc switched on. turn them both off in your php.ini file.

if you cannot do this, then put this at the top of your script

Code:
set_magic_quotes_runtime(false);
function cleanse($value){
 if (get_magic_quotes_gpc()){
   return stripslashes(trim($value));
 } else {
   return trim($value);
 }
}

and run the cleanse function on each variable that you use from the external world.

on the second, i think it is the space that is causing the confusion. either, as Vragabond says, you are using single quotes to define attribute values or, more likely, you have not outputted any quotes at all. use double quotes, imo.

Code:
echo "<input type=\"radio\" name=\"someradiobox\" value=\"$somevalue\" />";

if there is any danger that your value string will itself contain a double quotes character, you should escape this too.
 
Sweet, it worked!

I don't have access to the .ini file so I used the cleanse function and that did the trick. I also am outputting my form in an echo statement so all of the attributes are single quoted. On that one radio I just changed it to a double quote and escaped it. I also figured out that I can surround my echo statement with single quotes instead of double, and keep all my form attributes in double quotes since that's my editor's default. Then I would just have to escape the single quote in the value.

Thanks again!
 
variables do not expand in single quotes. So

$foo = 'bar';
echo $foo; //outputs 'bar'
echo "<br/>"
echo '$foo'; //outputs $foo
echo "<br/>";
echo "$foo"; //outputs 'bar'

for large blocks of html within php consider using the heredoc syntax. For example

Code:
echo <<<HTML
<form method="post" action='index.php'>
Type something here<br/>
<textarea name="textarea"></textarea><br/>
<input type="text" name="{$fieldNames['textbox1']}" value='$somevalue' /><br/>
<input type="submit" name='submit' value='Press Me'/>
</form>
HTML;

you can see from the above that both types of quotes are handled seamlessly. variables are expanded within the heredoc bloc. you disambiguate arrays and arrays of object properties by enclosing them in curly braces (you can do the same for 'simple' variables too)
 
That's good to know. I haven't come across that yet in my tutorials.

Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top