使用php“实时更新”表单字段

时间:2013-01-10 13:02:48

标签: php forms auto-update

如果我有非常基本的形式;

<input type = 'text' name = 'input1' value = '2'>
<input type = 'text' name = 'input2'>
<input type = 'text' name = 'answer'>

有没有办法使用php我可以将answer框设为'实时',以便在我放入10秒框后立即显示20作为值?

我很熟悉如何使用提交来完成此操作。然而,我的目标是不必刷新页面

提供更详细的表格:

根据设定值计算棱镜体积,并根据长度

计算用户输入
$sideOfSquare;
echo "<input type = 'text' name = 'input1'>";
ehco "<input type = 'text' name = 'squareSide' value ='.$sideOfSquare.'>;
echo "<input type = 'text' name = 'answer' value = 'INPUT1 * $sideOfSqaure * $sideOfSquare'>;

2 个答案:

答案 0 :(得分:1)

你也可以在Jquery中做到这一点

$("#input2").keyup(function() {
   var input2 = $(this).val();
   var answer = parseInt(input2) * parseInt($("#input1").val());

   $("#answer").val(answer);    

});

确保两件事。

1)您应该将这些ID保存在适当的文本框中。

2)验证输入以仅获取数值

答案 1 :(得分:1)

以下是如何使用jQuery ajax在PHP中执行所需的操作: (http://api.jquery.com/jQuery.ajax/

// current file
$("#input2").keyup(function() {
    var input2 = $(this).val();
    jQuery.ajax({
        url: 'myfile.php?input2=' + input2
    }).done(function( data ){
        $(this).val(data);
    });

});


 // myfile.php
 <?php $input2 = $_GET['input2'];
 echo $input2*2; //what you echo here will be sent to data in the success function above.