user1832628
user1832628

Reputation: 3929

How to put default data in html form field

I have a question about html form fields. I'm trying to create a form to allow users to edit their profile. I want the form to have their information already in the fields when they access the page. So like Username [ken] Address [12345] City [NJ]. Then the user can edit any info they want and what they don't want to edit will have their old information sent along with their new info. I'd like to know how I can put their info in the form fields.

I apologize if my question has been answered before but I'm not sure how to word it so it's difficult to search for.

Thank you.

Upvotes: 0

Views: 10598

Answers (2)

Sturm
Sturm

Reputation: 4285

Essentially all you're trying to do is add a value to the field.

 <input type="text" value="hello" />

will set the the text that is in the field when it loads to "hello". For php you'd simply be using

 <input type="text" value=<?php json_encode($variable); ?> />

Or at least something similar, depending on your exact needs.

For this particular example, try something similar to:

<input type="text" value= <?php json_encode($username); ?> />
<input type="text" value= <?php json_encode($address); ?>  />
<input type="text" value= <?php json_encode($city); ?>     />

The reason I'm using json_encode in this particular example is that it will print to the source in quotes, rather than doing some muddling '"'.$variable type nonsense.

Upvotes: 2

stefan bachert
stefan bachert

Reputation: 9606

You probably will use tag input. Just set the value attribute

<input type="text" value="default">

Something like this will be the result

<form ...>
<div>
<input name="user" type="text" value="ken">
<input name="address" type="text" value="12345">
<input name="city" type="text" value="NJ">
</div>
</form>

Upvotes: 0

Related Questions