Ajax在提交数据后重新加载购物卡

时间:2014-09-21 09:25:44

标签: php jquery ajax

我正在为餐馆建立一个小型在线订购系统。我的购物卡代码如下:

<?php
// Košarica
function ShopKosarica(){
global $link;

$UkupnoZbroj = 0;
$KosaricaSession = $_SESSION['ime'];
$rezultat = mysqli_query($link, "SELECT * FROM shop_kosarica WHERE KosaricaSession='$KosaricaSession' AND KosaricaKolicina<>0 ORDER BY KosaricaID ASC");
$num_results = mysqli_num_rows($rezultat);
if ($num_results==0){
    echo "<h2><strong>Košarica je prazna</strong></h2>";
}else{
    while ($redak = mysqli_fetch_array($rezultat)){
        $ArtikalID = $redak['KosaricaArtikal'];
        $rezultat_artikal = mysqli_query($link, "SELECT * FROM shop_artikal WHERE ArtikalID='$ArtikalID'");
        $redak_artikal = mysqli_fetch_array($rezultat_artikal);
        if ($redak['KosaricaVelicina']=='jumbo'){
            $Cijena = $redak_artikal['ArtikalCijena2'];
        } else {
            $Cijena = $redak_artikal['ArtikalCijena1'];
        }
        $Kolicina = $redak['KosaricaKolicina'];
        $Zbroj = $Cijena * $Kolicina;
        $Zbroj = number_format((float)$Zbroj, 2, '.', ''); 
        $UkupnoZbroj += $Zbroj;
        $UkupnoZbroj = number_format((float)$UkupnoZbroj, 2, '.', ''); 
?>
<form class="ShopKosaricaBox" method="post">
  <input type="hidden" id="KosaricaID" name="KosaricaID" value="<?=$redak['KosaricaID']?>">
  <div class="MarginBottom15">
    <input type="text" id="KosaricaKolicina" name="KosaricaKolicina" value="<?=$redak['KosaricaKolicina']?>" maxlength="2"> x <?=$redak_artikal['ArtikalNazivHr']?> (<?=$redak['KosaricaVelicina']?>) - <?=$Zbroj?> kn
  </div>
  <div class="right MarginBottom15">
    <a onclick="ShopPromjena();">Promjeni</a>&nbsp;&nbsp;&nbsp;<a style="background:#c94e11;" onclick="ShopBrisanje();">Obriši</a>
  </div>
  <div class="clear"></div>
</form>
<script type="text/javascript">
function ShopPromjena() {   
    $(document).ready(function(){
        var str = $(".ShopKosaricaBox").serialize();
           $.ajax({
           type: "POST",
           url: "/funkcije?akcija=promjena&KosaricaID=<?=$redak['KosaricaID']?>",
           data: str,
           success: function(str){
               alert( "Uspješno ste promjenili količinu!" );
                }
            });
            return false;
        });
}
function ShopBrisanje() {   
    $(document).ready(function(){
        var str = $(".ShopKosaricaBox").serialize();
           $.ajax({
           type: "POST",
           url: "/funkcije?akcija=brisi&KosaricaID=<?=$redak['KosaricaID']?>",
           data: str,
           success: function(str){
               alert( "Uspješno ste obrisali jelo!" );
                }
            });
            return false;
        });
}
</script>
<?php
    }  ?>
<h1 class="MarginBottom25" style="font-size:25px;">Ukupno: <strong><?=$UkupnoZbroj?> kn</strong></h1> 
<?php } 
}
?>

我通过Ayax将数据放入mysql,这是javascript:

$(document).ready(function(){
    $(".ShopPonudaBox").submit(function(){
    var str = $(this).serialize();
       $.ajax({
       type: "POST",
       url: "/funkcije?akcija=dodaj",
       data: str,
       success: function(str){
           alert( "Uspješno ste dodali jelo!" );
           $('#KosaricaBox').load("/include/funkcije.php?funkcija=ShopKosarica"); 
            return false;
          }
        });
        return false;
    });
});

和php代码;

if ($_GET['akcija']=="dodaj")   {
    if ($_POST['KosaricaKolicina']<>0){
        $KosaricaSession = $_SESSION['ime'];
        $KosaricaArtikal = clean($link, $_POST['ArtikalID']);
        $KosaricaKolicina = clean($link, $_POST['KosaricaKolicina']);
        $KosaricaVelicina = clean($link, $_POST['KosaricaVelicina']);
        $provjera = mysqli_query($link, "SELECT * FROM shop_kosarica WHERE KosaricaSession='$KosaricaSession' AND KosaricaArtikal='$KosaricaArtikal' AND KosaricaVelicina='$KosaricaVelicina'"); 
        $num_results = mysqli_num_rows($provjera);
        if ($num_results==0){
            $result = mysqli_query($link, "INSERT INTO shop_kosarica (KosaricaSession, KosaricaArtikal, KosaricaKolicina, KosaricaVelicina) VALUE ('$KosaricaSession', '$KosaricaArtikal', '$KosaricaKolicina', '$KosaricaVelicina')");
            //header("Location: /online-narudzba#Shop");
        } else { 
            $redak_provjera = mysqli_fetch_array($provjera);
            $KosaricaID = $redak_provjera['KosaricaID'];
            $result = mysqli_query($link, "UPDATE shop_kosarica SET KosaricaKolicina=KosaricaKolicina+$KosaricaKolicina WHERE KosaricaID='$KosaricaID'");
            //header("Location: /online-narudzba#Shop");
        }       
    } else {
        //header("Location: /online-narudzba#Shop");
    }
}

我试过这个我在这里找到的方法

 $('#KosaricaBox').load("/include/funkcije.php?funkcija=ShopKosarica"); 

   $funkcija = $_GET["funkcija"]; 
    if ($funkcija == "ShopKosarica") {
        echo ShopKosarica();
    }

但不断收到错误

  

注意:未定义的变量:_SESSION in   第47行的H:\ Dropbox \ htdocs \ include \ funkcije.php

     

警告:mysqli_query()期望参数1为mysqli,null给定   在第48行的H:\ Dropbox \ htdocs \ include \ funkcije.php

1 个答案:

答案 0 :(得分:1)

添加

session_start();

并连接数据库 在任何HTML之前的页面开头

你会有类似的东西:

$con=mysqli_connect("localhost","xxxx","xxxx","xxxxx");

//check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}

session_start();
        include("inc/incfiles/header.inc.php")?>
        <html>
        <head>
    <meta http-equiv="Content-Type" conte...

不要忘记删除之前的空间