Is it possible to modify what the user will enter in a form?

Asked

Viewed 32 times

0

It is possible to make that what the user enters in the form is already modified?

Example: It insert "Hi" in <textarea> and in my results I have a "5 Hi", being the '5 ' inserted by myself through code.,

Excerpt from what I tried for the answers:

let comment = document.getElementById('comment')
let valorAdicional = '5'
let valorDigitado = ''
comment.onkeyup = function(){
  comment.value = valorAdicional + comment.value
}

textarea id="comment" maxlength="500"
input type="submit" name="post_comment" value="Enviar" 

Didn’t work, just filled in the data even put, without adding

  • If you want this in real time, type, the user type and already replaces on time, only with PHP there is no way, but if you want to do after sending the form, there is how, want to update.

  • @Anthraxisbr has a tear.. If it is real time it is necessary to use javascript

  • I want that before arriving the answer it is already with something preadded before, I can yes use javascript if they have a solution how to do

3 answers

0

Only you return to the variable you want, the value of the textbox the user typed by adding another string. A method called concatenation.

Try this

    $idade = "5";
    $valorAdd = "hue";

       $idadeAtual = $valorAdd." - ".$idade;

       echo $idadeAtual;

You’ll get the next exit in that case

hue - 5

0

You can do this in real time with JS and already receive modified in PHP

A simple example, with each character typed, it puts a 5 in front, then you can do it the way you prefer.

let campo = document.getElementById('campo')
let valorAdicional = 5
let valorDigitado = ''
campo.onkeyup = function(){
  campo.value = valorAdicional + campo.value
}
<textarea id="campo"></textarea>

That way you just get on PHP thus:

$campo = $_POST['campo'];

If only with PHP just concatenate

$campo = "5" . $_POST['campo']; // Valor que foi digitado no campo
  • I think this answer was the closest to understand even but with me it didn’t work :/ Follow code excerpt, ta returning only what I insert, see in the answers I will post

  • @Giovanyhenrique You put the part of JS at the end of the file inside <script></script> ?

  • I put before the form the js script that you passed yes, inside <script></script>, follows the question as is the code

  • @Giovanyhenrique Da alguem erro no console? because in the example I posted is working, in case there is already some error in the console, could you pass me?

  • No error but also not adc the characters, there would be a way to put something fixed before the answer as explained in the topic?

  • In case you want something already on textarea before the user even starts typing, you may have <textarea value="5"></textarea>, but it should be working the code I passed, you could post your complete code on JSFiddle ?

  • Yes it is already functional but not satisfied what intended, I wanted to leave fixed something along with what the user would type but I think there is no possibility

Show 2 more comments

0

From what I understand you want to change the record while the user type in the field (I believe this pre-written text will come from a select in the database)
If you are using a php framework, you can use the ajax method, so you make a request without updating the page
Or if it is pure php, just select it in the database and concatenate the value using the . keyup function of jquery. Example:

var txtCampo = document.getElementById('id_do_input');
txtCampo.onkeyup = function(){
  //chame aqui um script php ou faça uma requisição ajax
  campo.value = "o valor que vc quer" + txtCampo.value
};

Browser other questions tagged

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