繁体   English   中英

采取JavaScript的总和答案,并将其放在MySQL字段

[英]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.

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