Is it possible to change what the user is going to insert into a form?

0

Is it possible to make what the user inserts into the form already modified?

Example: It inserts "Hi" in <textarea> and in my results I have a "5 Hi", being '5' entered 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" 

It did not work, just filled in with the data even put, without adding

    
asked by anonymous 30.10.2017 / 18:58

3 answers

0

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

Try this

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

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

       echo $idadeAtual;

It will have the following output in this case

hue - 5
    
30.10.2017 / 19:10
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 5 in front, then you can do it any way you like.

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

In this way you only have to get in PHP like this:

$campo = $_POST['campo'];

If it is only with PHP simply concatenate

$campo = "5" . $_POST['campo']; // Valor que foi digitado no campo
    
30.10.2017 / 19:15
0

So I understand you want to change the record while the user types in the field (I believe that this prewritten text will come from a select in the bank)
In case you are using some php framework, you can use the ajax method, so you make a request without updating the page
Or if it is pure php, just make a select in the database and concatenate the value using the jquery .keyup function. 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
};
    
30.10.2017 / 19:25