Sure.
This javascript code can go in your same html file or in a separate one but you must include it in your script in your html.
Your php code will remove it from the html and send it to another file, to that file we will refer to the request of ajax and from that file we will take what your php code returns to us and we will pass it to the input in your html, our intermediary will be the request of ajax in the javscript, do not forget that you need the jquery library to be able to handle events like click () and be able to send information to your input through the val ().
$("#generarMiCodigo").click(function(){
//Con este evento detectas el click a tu boton y llamas a la función generarCodigo.
alert("me diste click y llame a la función generarCodigo()");
generarCodigo();
});
function generarCodigo(){
alert("fui llamada.");
$.ajax({
url: 'tuArchivo.php',//este es tu archivo php donde estara tu código para generar balga la redundancia tu código.
type: 'POST',
dataType: 'JSON',
data:{
longitud: 10 //suponiendo que quisieras por ejemplo poder definir desde tu petición la longitud de tu codigo generado este parametro lo tomarias en tu archivo php a travel del $_POST['longitud'];.
}
})
.done(function(respuesta){
//Si todo salio bien enviamos lo retornado al input.
$("#codigoGenerado").val(respuesta);
})
.fail(function(){
alert("Si fallo mi consulta de este modo podre estar enterado");
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<html>
<input type="text" id="codigoGenerado" name="verificacion" value="">
<input type="button" id="generarMiCodigo" value="generar codigo" id="code" name="code">
</html>
Obviously this code if you execute it will mark you an error since there is no PHP file, but this is the general idea of the ajax request.
I hope I left it a little clearer.