1

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?

1
  • It sounds like you should be using sessions, rather than repeatedly POST'ing the variable. Commented Jan 18, 2013 at 23:49

4 Answers 4

6

try

<input type="hidden" name="uname" value="<?php echo $uname; ?>">
Sign up to request clarification or add additional context in comments.

3 Comments

Short tags are not available on all versions of PHP.
you are correct, i have edited to fix that....apologies. Not sure if that was necessarily worth a down vote, but fair enough.
I got you back to 0, @HydraIO
2

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.

Comments

1

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?>" />

3 Comments

you forgot the ending ';' :)
It's not necessary here. From the PHP docs: The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.
it does however let younger devs think it's ok to not use it. Not saying it's not needed, just saying it promotes etiquette that some devs may not care for.
0
<input type="hidden" name="uname" value="<?php echo $uname;?>" />

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

6 Comments

he can't $_POST his value if it has no name.
you have no name on the input. You cant access the hidden value with $_POST because you have no name to ask for. such as $_GET['uname']
oh, I see what you meant. I was only trying to say that it needs to be a hidden value, but anyway.
someone already downvoted my answer for not being complete. I don't see a reason why downvote again for the same reason?
Why do you assume the downvote is for the same reason? I haven't voted on this one, but if an answer isn't going to work, in general, I'll downvote it. Your answer clearly won't work (and you haven't made any attempt to edit and fix it so it would), so it should be downvoted. Think of it this way, if someone ends up here while googling to solve their problem, your answer gives bad information, while other answers with higher scores actually work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.