0

I looked for this everywhere but I couldn't find anything useful. I need a way to put a default text (preferably a php variable) in a HTML text field whenever I load the page. It's like a placeholder (I'm aware of the placeholder attribute, I don't want that) that doesn't disappear so that the user can submit that value if he wants.

I'm guessing it can be done with a simple javascript script but I suck at that.

Thank you for your help.

3 Answers 3

2

You're looking for the value attribute then: value="text here"

With a PHP variable, it would be something like this:

<form action="path-here">
  <input type="text" name="field-name" value="<?php echo $variable; ?>"><br />
  <input type="submit" value="Submit">
</form>
1

Here are two ways to put a default value in a text field.

The first way is the simplest. All it involves is setting the value of the text field.

<input type="text" id="myTextBox" value="Some default text">

The second was uses a JavaScript function that runs when the page loads.

<!-- This solution is a little more complicated. -->
<body onload="setDefaultText()"> 
   ...
   <input type="text" id="myTextBox">
</body>
<script>
  function setDefaultText() {
     document.getElementById("myTextBox").value = "Some default text";
  }
</script>
0

JSFIDDLE Example

<form>
  First name: <input type="text" name="fname" value="John"><br>
  Last name: <input type="text" name="lname" value="Doe"><br>
  <input type="submit" value="Submit form">
</form>

You are looking for the value="Text Here"

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.