tags:

views:

629

answers:

4

So I have this empty textboxes in a registrationg page. The user enters some data, hits continue and then there's a confirmation page. If the data is incorrect, the user hits go back to go correct whatever was wrong. However, when he goes back, all the textboxes are empty. So the first thing that comes to my mind is to store the user data in a Session (I have a User class that holds all this data so I store the class in the session). When the user goes back I am able to retrieve the data.

I do something like this:

if($_SESSION['UserInfo'])
{
    $user = $_SESSION['UserInfo'];

    $firstName = $user->FirstName;
    $lastName = $user->LastName;
}

How would I put these variables in a textbox?

+6  A: 

To set the value, you can just echo out the content inside the value attribute:

<input type="text" name="firstname" value="<?php echo htmlentities($firstName); ?>" />
<input type="text" name="lastname" value="<?php echo htmlentities($lastName); ?>" />
brianreavis
Thanks it worked.
Carlo
+1 only answer so far to get the escaping right; shame on the others. Although `htmlspecialchars()` is just as effective, and `ENT_QUOTES` is generally prefaerable, though it doesn't matter here.
bobince
They did not ask how to escape it... they asked how to put it into the text box. Besides... what do you think that "Of course you will want to escape it but..." means?
SeanJA
@Sean: Well with that mindset, if you teach someone how to fire a gun... are you going to leave out the part about safety?
brianreavis
@brianreavis: No... I would not. I was actually more annoyed that I and a couple of others were down voted because we left something out that was not within the scope of the question.
SeanJA
@Sean: Ahh gotcha... yeah, definitely not worth a down vote. I'm with you on that.
brianreavis
A: 

Of course you will want to escape it but...

<input type="text" value="<?php echo $firstName ?>" />

or if the form is posted, it would be easier to do:

<input type="text" name="firstName" value="<?php echo $_POST['firstName'] ?>" />

fine... even though it was out of the scope of the question here is the escaped version:

<input type="text" name="firstName" value="<?php echo htmlentities($_POST['firstName']) ?>" />
SeanJA
You could incorporate an example of `htmlentities()`
Nerdling
Seriously? Voted down because of that?
SeanJA
+1 you didnt deserve the downvote from whoever
Mark
Thanks! That is what I did for all of the rest of the down voted answers
SeanJA
A: 

smth like

<input type="text" value="<?php echo $first_name;?>">

Don't forget to escape with htmlentities() or smth similar. If you don't know why - google XSS.

unknown
You could incorporate an example of `htmlentities()`
Nerdling
Seriously? Voted down because of that?
SeanJA
A: 

Questions like this should be paraphrased to "How do I print in PHP?" From PHP you only print code which is used by browsers as HTML. It does not matter whether it's a text box or a text triangle.

If you don't know how to make HTML form elements, consult HTML tutorials like http://www.w3schools.com/html/ For your problem:

<input type="text" value="PRINT IT HERE" />
zilupe
Why do you assume I don't know how to make HTML form elements? This has nothing to do with what I asked. I know how to put a value on an HTML textbox or a button myself, I only didn't know how to do it through PHP, I think my question and its scope was very clear.
Carlo
I think the point - a little rudely conveyed, perhaps - is that all you're ultimately doing in PHP (and every other web language) is generating HTML for the browser. If you know how to set a textfield's value in HTML, you know how to do it in PHP - you output the HTML you want.
ceejayoz
Why do you think this answer is only for you? If anyone else comes to this site and finds such a question, he will find a good tip. Of course, now that these kind of responses get down-voted we can expect another question of how to set value of textarea, img, div etc. through PHP.
zilupe
My comment was addressed @Carlo.
zilupe
@Zilupe: Because it was me who asked the question. You're answer would've been a lot better as a comment IMO, still a little rude, but better (since it was not addressed to me only, and it was a little off-topic).
Carlo
It won't be a good tip for anyone if someone comes by and sees it voted down to a negative score...
SeanJA