echo command : how to declutter my script?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
typewriter
Forum Newbie
Posts: 7
Joined: Tue Sep 26, 2006 4:50 pm

echo command : how to declutter my script?

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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']), '" />';
typewriter
Forum Newbie
Posts: 7
Joined: Tue Sep 26, 2006 4:50 pm

Post by typewriter »

A-ha! Cheers friend.
Post Reply