Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the value of a textbox through PHP?

Tags:

php

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?

like image 603
Carlo Avatar asked Sep 27 '09 23:09

Carlo


2 Answers

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); ?>" />
like image 198
brianreavis Avatar answered Sep 30 '22 19:09

brianreavis


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']) ?>" />
like image 40
SeanJA Avatar answered Sep 30 '22 19:09

SeanJA