[英]taking a javascript sum answer and putting it in a mysql field
我已經使用javascript(cart.js)創建了一個購物車,我想知道您是否可以接受P&P,訂購的總額和金額並將其放置在使用php的mysql數據庫中。 我還沒有嘗試過任何東西,因為我不確定是否有可能,我在谷歌搜索時找不到任何東西(除非我正在尋找錯誤的東西)
--cart.js ---
function clearitems(){
document.itemsform.num1.value=0;
document.itemsform.num2.value=0;
document.itemsform.num3.value=0;
document.itemsform.total1.value=0;
document.itemsform.total2.value=0;
document.itemsform.total3.value=0;
document.itemsform.PPTotal.value=0;
document.itemsform.overalltotal.value=0;
}
function totalcost(){
var total=0
number=document.itemsform.num1.value
if (isNaN(number)||number<1) number=0;
if (number>4) price=15.00
else price=20.00;
document.itemsform.price1.value=currency(price)
document.itemsform.total1.value=currency(price*number)
total=total+price*number
number=document.itemsform.num2.value
if (isNaN(number)||number<1) number=0;
if (number>4) price=7.50;
else price=10.00
document.itemsform.price2.value=currency(price)
document.itemsform.total2.value=currency(price*number)
total=total+price*number
number=document.itemsform.num3.value
if (isNaN(number)||number<1) number=0;
if (number>4) price=18.00;
else price=24.00
document.itemsform.price3.value=currency(price)
document.itemsform.total3.value=currency(price*number)
total=total+price*number
if (total<50){
document.itemsform.PPTotal.value=currency(2.5)
total=total+2.5
}else document.itemsform.PPTotal.value=currency(0);
return(currency(total))
}
function currency(inputnum){
var outputstring=""
outputstring="£"+inputnum
if(outputstring.charAt(outputstring.length-2)==".")
{outputstring=outputstring+"0"; return(outputstring)}
if(outputstring.charAt(outputstring.length-3)!=".")
{outputstring=outputstring+".00"; return(outputstring)}
}
將會是這樣的:
在您的JS文件中:
function saveToDatabase(cartValue) {
$.ajax({
type: "GET", //the method/type of the information you are sending
url: "yourwebsite.com/somefile.php?data="+cartValue, //the url where you will fetch the data you need
dataType: 'text', //the type of the data = text
success: function(result){ //if function succeed
//do something
}
});
}
在您的PHP
文件^somefile.php
:
extract($_GET);
//$data
//^ this variable is the cartValue from JS
//do something / save in the database
可以使用AJAX將購物車數據發布到PHP服務器,如下所示:
var cartData = "pandp=" + pandP + "&total=" + total; //build a query string for the cart data
var xmlhttp = new XMLHttpRequest(); //instantiate the request object
xmlhttp.open("post", "/somefile.php"); //initialise a post request to the server side script
xmlhttp.send(cartData); // send the cart data
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText); //output a successful response to the console
}
}
在服務器端,您的PHP腳本可以通過$_POST
超全局變量訪問數據:
<?php
var_dump($_POST);
不要提取$_POST
(@holpducki建議),這會帶來安全風險。 $ _POST的內容必須在使用前經過清理。
有關XMLHttpRequest的更多信息, 請參見https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#open()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.