Page 1 of 1

echo command : how to declutter my script?

Posted: Tue Sep 26, 2006 4:56 pm
by typewriter
Hello,

I am a relative newbie to all this. Is there a way to reduce the following to a single echo?

echo "<form method=post action='$self?page=2'><input type=hidden name=id value=";
echo $_REQUEST["id"];
echo "><input type=hidden name=term value=";
echo $_SESSION['term'];
echo "><input type=hidden name=category value=";
echo $_SESSION['cat'];
echo ">";

Everything I tried causes problems, with the $_SESSION and $_REQUEST either being interpreted as literal, or bad placements of single and double quotes causing errors.

Thank you and sorry if this is considered a :roll: type question.

Posted: Tue Sep 26, 2006 5:05 pm
by volka
echo can take multiple arguments.
$_SESSION['term'] and esp. $_REQUEST['id'] can contain characters ( like " ) that shouldn't be allowed here.

Code: Select all

echo '<form method="post" action="', $self, '?page=2">
		<input type="hidden" name="id" value="', (int)$_REQUEST['id'], '" />
		<input type="hidden" name="term" value="', str_replace('"', '"', $_SESSION['term']), '" />
		<input type="hidden" name="category" value="', str_replace('"', '"',$_SESSION['cat']), '" />';

Posted: Tue Sep 26, 2006 5:07 pm
by typewriter
A-ha! Cheers friend.