获取javascript总和答案并将其放入mysql字段中

时间:2015-06-17 16:22:48

标签: javascript php mysql

我用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)}
}

2 个答案:

答案 0 :(得分:0)

会是这样的:

在你的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

答案 1 :(得分:0)

可以使用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 superglobal:

访问数据
<?php
     var_dump($_POST);

不要提取$_POST(由@holpducki建议),这会带来安全风险。 $ _POST的内容必须在使用前进行清理。

有关XMLHttpRequest的更多信息,请访问https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#open()