私はPHPにかなり慣れておらず、JQueryにも本当に慣れていません。
だから私はいくつかの計算を行ういくつかのJQueryを書きました、私は似たようなものを以下に書きました:
//on change of a selectbox with the class item
$('.item').change(function() {
// set variable id as the id name of this id
var id = this.id;
// price variable is equal to the value of the element id 'hiddenprice'
price = $("#hiddenprice").val();
// number of items is the value of the select box
numberofitems = $(this).val();
// number of days is equal to a php variable I set on the page
numofdays = "<?php echo $length->days; ?>";
//totalprice is equal to the 'price' multiplied by 'numofdays'
totalprice = Number(price) * Number(numofdays);
//calculates final total by multiplying the 'totalprice' by 'numofitems'
finaltotal = Number(totalprice ) * Number(numofitems);
//updates the HTML with the new price
$('#'+id).html("€" + finaltotal.toFixed(2));
});
私はこれを試していましたが、動作するようになりましたが、いくつか読んだ後、このスクリプトは更新されるページのフッターにあるため、ユーザーが悪意を持っている場合は安全ではなく、操作しやすいことに気付きました。
したがって、PHPスクリプトに値を投稿してから値を返すことにより、サーバー側で計算を実行したいと思います。
// POST values to PHP Script
$id = (posted select id);
$price = (#hiddenprice variable value);
$numofitems = (posted value of the select);
$numofdays = $length->days;
$totalprice = (int)$price * (int)$numofdays;
$finaltotal = (int)$totalprice * (int)numofitems;
//Then push $finaltotal and $id back to the user viewed page
$('#'+<?php echo $id; ?>).html("€" + <?php echo $finaltotal; ?>.toFixed(2));
更新せずにページにプッシュしてから、更新せずに返す方法がわかりません。
繰り返しになりますが、これが単純な場合は申し訳ありませんが、JQueryフォームプラグインを見てきました。やりたいことに対してもっと適切な解決策があるかどうか疑問に思いました。
前もって感謝します。