Page 1 of 1

Variable transformations using included function

Posted: Wed May 14, 2003 12:01 pm
by Crashin
Hidee-ho, ya'll! Here's what I'm doing...what am I doing wrong??? I've got this function in an include file:

Code: Select all

//prepare variables from text fields for DB entry
function text_prep($text) {
	$text = strip_tags($text);
	$text = htmlentities($text);
	$text = addslashes($text);
	$text = nl2br($text);
}
And I've got this form/code on a separate page to call the function and make the appropriate transformations:

Code: Select all

<form action="test.php" name="new_user" method="post">
	Text: <input type="text" name="practice" size="30" maxlength="100">
	<input type="submit" name="submit" value="Submit">
</form><br>
<?php if(isset($_POST&#1111;'practice'])) echo "Transformed: " . text_prep($_POST&#1111;'practice']) . "<br>Original: " .  $_POST&#1111;'practice']; ?>
I get nothing for the transformed variable. If it's something obvious forgive me ahead of time...chide me if you must! :)

Posted: Wed May 14, 2003 12:11 pm
by twigletmac
You need to return a value from the function:

Code: Select all

function text_prep($text) { 
   $text = strip_tags($text); 
   $text = htmlentities($text); 
   $text = addslashes($text); 
   $text = nl2br($text); 
   // return the value of $text
   return $text;
}
and then have something like:

Code: Select all

<form action="test.php" name="new_user" method="post"> 
   Text: <input type="text" name="practice" size="30" maxlength="100"> 
   <input type="submit" name="submit" value="Submit"> 
</form><br> 

<?php

if(isset($_POST['practice'])) {
	$transformed = text_prep($_POST['practice']);
	echo 'Transformed: '.$transformed.'<br />Original: '.$_POST['practice'];
}	
?>
Mac

Posted: Wed May 14, 2003 12:17 pm
by Crashin
Thanks, Twig! I knew it was something simple that I was overlooking. :oops: Kind of hard for PHP to tell me something if I don't tell it to tell me something!