Jump to content

[SOLVED] Escaped string value disapears?


Boo-urns

Recommended Posts

When processing a form with double quotes and the validation fails the value is just the '\' that is added in. However when I echo it out right after escaping it it displays properly.

 

My question is how would you put their inputted value back? (Everything besides double quotes returns correctly to the form when validation fails)

 

Posting variable and running function

<?php
// inside the form processing
$fName   = $db->check_n_escape($_POST['fName'], 'first name');

 

Function in the db class

<?php
// check n escape function in the db class
function check_n_escape($str, $checkName) {		
	if($str == NULL) {
		$this->fieldError  = 1;
		$this->fieldErrMsg .= ($this->fieldErrMsg != NULL) ? $checkName . ", " : $this->fieldErrMsgStart . $checkName . ", ";

	} else 
		{ return $this->escape_str($str); }

}

function escape_str($str) {							// escaping strings
	if(get_magic_quotes_gpc())
		{$str = stripslashes($str);} 

	if(function_exists("mysql_real_escape_string"))
		{$str = mysql_real_escape_string($str);} 
	else
		{$str = addslashes($str);} 

	return $str;		
}

?>

 

Or is there a better way? Thanks for your help!

-Corey

Link to comment
https://forums.phpfreaks.com/topic/140825-solved-escaped-string-value-disapears/
Share on other sites

You need to use the htmlentities function when displaying the data in the value attribute or the double quote in the data will end the value. If you had looked at the generated source of the form, you would have seen something like:

<input type="text" name="fName" id="fName" value="this string has a " in it />

if the value of $fName had been 'this string has a " in it'

 

You need to use:

<input type="text" name="fName" id="fName" value="<?php echo htmlentities(stripslashes($fName),ENT_QUOTES); ?>" />

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.