当 PHP table 更新时动态更新文本框。
Update a textbox dynamically when a PHP table is updating.
在PHPcodeigniter中,我有一个添加销售记录的界面。我 select 商品和数量,然后输入到下面的 table。
在底部有一个文本框来保存总价。所以我需要在向 table 添加记录时更新文本框。怎么做?
PHP 无法在您键入时动态更新内容。您必须提交表格,然后在 PHP 中计算总数,然后在新页面上 return 结果。如果您想在进行更改时更新总数而不必提交表单并等待新页面加载,则需要使用 Javascript.
您可以使用异步 javascript (AJAX) 在 table 或页面中进行动态更改。请参阅 Ajax & PHP Powered MySQL Table 编辑器/查看器/数据网格
你可以使用
ajax -> 更新数据库return成功
appendTo ....显示新记录
->调用函数cal price
您不能直接从 php 执行此操作,请尝试使用 jquery/javacript
程序从jquery
以某种方式,当输入字段的值发生变化时调用一个函数,该函数将再次重新计算所有字段的总和
假设您在 html 中有一个 table 喜欢
|id|Name |Price|
----------------------
|1 |Some name |10 |
|2 |Some name |20 |
|3 |Some name |30 |
|4 |Some name |40 |
----------------------
| | |100 |
and 以及表单中某处的输入字段。
该输入字段的 Id
= "priceInputField"
和包含价格的每列的 Class
= "priceValue"
并且包含总计的列的 ID 为 "totalPrice"
所以你的 jquery 函数看起来像
$("#priceInputField").on("change",function(){
//Some code to add the new row in the table
var updatedPrice = 0;
$(".priceValue").each(function(){
updatedPrice = updatedPrice + $(this).text;
})
$("#totalPrice").text("updatedPrice");
});
在PHPcodeigniter中,我有一个添加销售记录的界面。我 select 商品和数量,然后输入到下面的 table。
在底部有一个文本框来保存总价。所以我需要在向 table 添加记录时更新文本框。怎么做?
PHP 无法在您键入时动态更新内容。您必须提交表格,然后在 PHP 中计算总数,然后在新页面上 return 结果。如果您想在进行更改时更新总数而不必提交表单并等待新页面加载,则需要使用 Javascript.
您可以使用异步 javascript (AJAX) 在 table 或页面中进行动态更改。请参阅 Ajax & PHP Powered MySQL Table 编辑器/查看器/数据网格
你可以使用
ajax -> 更新数据库return成功
appendTo ....显示新记录
->调用函数cal price
您不能直接从 php 执行此操作,请尝试使用 jquery/javacript
程序从jquery
以某种方式,当输入字段的值发生变化时调用一个函数,该函数将再次重新计算所有字段的总和
假设您在 html 中有一个 table 喜欢
|id|Name |Price|
----------------------
|1 |Some name |10 |
|2 |Some name |20 |
|3 |Some name |30 |
|4 |Some name |40 |
----------------------
| | |100 |
and 以及表单中某处的输入字段。
该输入字段的 Id
= "priceInputField"
和包含价格的每列的 Class
= "priceValue"
并且包含总计的列的 ID 为 "totalPrice"
所以你的 jquery 函数看起来像
$("#priceInputField").on("change",function(){
//Some code to add the new row in the table
var updatedPrice = 0;
$(".priceValue").each(function(){
updatedPrice = updatedPrice + $(this).text;
})
$("#totalPrice").text("updatedPrice");
});