简体   繁体   English

输入值后,PHP输入字段立即更新

[英]PHP input field instant update after typing in values

just trying to make a little calculator for myself in php and i wonder if its possible to make the answer instant update after i type in the values? 只是试图为自己在php中创建一个小计算器,我想知道是否有可能在我输入值后立即更新答案?

Thank you. 谢谢。

    <?php
if (isset($_POST['valuea'])) $valuea = $_POST['valuea'];  
if (isset($_POST['valueb'])) $valueb = $_POST['valueb'];

if (isset($_POST['check1'])) {
    $answer = (($valuea * $valueb) * 10)*2; 
} else {
    $answer = ($valuea * $valueb) * 10; 
}


echo <<<_END
<form method='post' action=''>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcrow">
    <td>Lenght:</td>
    <td align="center"><input type='text' name='valuea' value="$valuea"/></td>
</tr>
<tr class="calcrow2">
    <td>Width:</td>
    <td align="center"><input type='text' name='valueb' value="$valueb"/></td>
</tr>
<tr class="calcrow2">
    <td>2x test</td>
    <td align="center"><input type="checkbox" name="check1"></td>
</tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>

<tr class="calcrow">
<td><i>Answer</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td></i>
</tr>
</table>
</form>

You are looking for a thing called Jquery. 您正在寻找一种叫做Jquery的东西。 Which can fetch current Values of your form inputs or any other elements from your webpage. 它可以获取表单输入或网页中其他任何元素的当前值。 Then you can then send that data to your php file where you can perform the calculation and return the result which can again be displayed wherever you want . 然后,您可以将该数据发送到php文件中,在其中可以执行计算并返回结果,该结果可以再次显示在您想要的任何位置。

All of this happens in real time. 所有这些都是实时发生的。 This is the way you could do it if you wanted to do it in php 这是您可以在php中完成的方法

Else you could always go for plain JavaScript or a mixture of jquery + javascript, to avoid using php and ajax. 否则,您始终可以使用纯JavaScript或jquery + javascript的混合体,以避免使用php和ajax。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM