Código PHP:
document.write("<input type='text' class='input4' id='cantidad"+num+"' onchange=\"document.getElementById('subtotal" + num + "').value = document.getElementById('precio" + num + "').value * document.getElementById('cantidad" + num + "').value; sumatotal(); \">