How to prevent the user from making changes to the input?

Asked

Viewed 1,420 times

4

I have a input in a form, however it should not have its value changed:

<input type="text" name="pais" value="">

How can I stop the value from being changed?

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  Pais: <input type="text" name="pais" value=""><br>
  Estado: <input type="text" name="estado" value=""><br>
  <input type="submit" value="Submit">
</form>

</body>
</html>
  • 2

    Placing the property readonly?

  • Use readonly="true"

  • pq was put the tags PHP and javascript ? vc qer a solution with these languages ? or readonly is enough ?

  • could have a solution in php and javascript too !!!

2 answers

7


Use the attribute readonly of the element input for that reason:

<input type="text" name="pais" value="" readonly />

2

The other answers already contemplate what was requested, however I would like to leave an alternative using Javascript:

window.onload = function() {
  document.getElementById('input-disabled').disabled = true;
  document.getElementById('input-readonly').readOnly = true;
}
<input type="text" id="input-disabled">
<input type="text" id="input-readonly">

Through the onload, as the page loads its content, create a function to catch the element with id equal to input-disabled and add the attribute :disabled as true. Same goes for element with id equal to input-readonly, which I add the attribute :readOnly as true.

About the :disabled and :readonly:

  • Disabled does not pass the value to the user, and can not edit.

  • Readonly sends the value to the form and also cannot edit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.