Preston159
Preston159

Reputation: 302

How do I place a php variable as text in a hidden input html input?

My current code is

<form action="post.php" method="post">
<input type="text" name="post"> <br />
<input type="text" name="uname" value="<?php echo $uname?>">

I need the $uname variable to become the text in the hidden input field so that I can post it to post.php, as it was posted to this page via the previous one, and needs to be carried on. Is there a better way I could do this, like post a variable directly, instead of through an input field?

Upvotes: 1

Views: 22615

Answers (4)

Hydra IO
Hydra IO

Reputation: 1557

try

<input type="hidden" name="uname" value="<?php echo $uname; ?>">

Upvotes: 6

mavili
mavili

Reputation: 3424

<input type="hidden" name="uname" value="<?php echo $uname;?>" />

an alternative would be storing session variables to carry them to other pages.

Upvotes: 0

PleaseStand
PleaseStand

Reputation: 32082

The main improvements possible are using the "hidden" input type and properly HTML escaping the value:

<input type="hidden" name="uname" value="<?php echo htmlspecialchars($uname); ?>">

Keep in mind that any malicious user can see and tamper with data passed back to the server in this way. If that is a concern, you may wish to store the data in the user's session instead.

Upvotes: 2

SeanWM
SeanWM

Reputation: 16989

Your type needs to be hidden and there is no text attribute on an input element, its value.

Full example:

<input type="hidden" name="uname" value="<?php echo $uname?>" />

Upvotes: 1

Related Questions