我對PHP相當陌生,對JQuery來說真的很陌生。PHP/JQuery:發佈到PHP腳本,然後返回值並使用JQuery更新HTML
所以我寫詩一些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表單插件,我只是想知道是否有更多適合我想要做的解決方案。
在此先感謝。
我使用了像你說的在jQuery中的ajax,然後echo'd一個變量 – TryingToBeZen 2013-03-14 15:40:32