在其他请求AJAX之后请求不工作

时间:2014-03-31 15:21:06

标签: jquery ajax

Jquery的:

    $(".sortimente").click(function(){

    var sort = $(this).attr('value');
    var dataId = "st="+ sort;

    $.ajax ({
    type: "POST",
    url: "functii.php",
    data: dataId,
    cache: false,
    success: function(html)
        { $("#text").html(html); }
    });
});

$(".but-comanda").click(function(e){
    e.preventDefault();
    var produs = $(this).attr('value');
    dataId = 'produs='+ produs; 

    $.ajax ({
    type: "POST",
    url: "cos.php",
    data: dataId,
    cache: false,
    success: function(html)
        { $("#cos").html(html);}
    });
});

.but_comanda正在开始工作,但是,如果我按下.sortimente,他就会停止工作,并且在cos.php数组中保存收到的数据时不会添加任何内容。

cos.php

session_start();
if(!isset($_SESSION['cos']))
    $_SESSION['cos'] = array();

if(!in_array($_POST['produs'],$_SESSION['cos']))
    array_push($_SESSION['cos'], $_POST['produs']);

foreach($_SESSION['cos'] as $key)
    echo $key."<br />";

如果我在php文件中有类.but-comanda并且此文件是带有ajax的请求,则可能有问题?

<?php

function AfisareProduse($c = NULL, $p = NULL, $o = NULL, $d = NULL) {

    include('configurare.php');

    $pret = explode('-', $p);

    if( $c != '')
        $interogare = "SELECT * FROM `produse`  WHERE `categorie` = '". $c ."' AND (`pret` BETWEEN " . $pret[0] . " AND " . $pret[1] . ") ORDER BY ". $d . " " . $o ; 
    else 
        $interogare = "SELECT * FROM `produse`  WHERE `pret` BETWEEN " . $pret[0] . " AND " . $pret[1] . " ORDER BY ". $d . " " . $o ; 

    echo $interogare . "<br />";

    $rezultat = $conexiune->query($interogare);

    if($rezultat->num_rows > 0)
        while($rand = $rezultat->fetch_array())
        {
            $id = $rand['id'];
            $nume = $rand['nume'];
            $categorie = $rand['categorie'];
            $pret = $rand['pret'];
            $imag = $rand['imag'];

            echo "<div id='produs'> <div id='img-produs'><img src='Poze/".$categorie."/".$imag."'/></div>" .
                " <div id='det-produs'> Produs: <span id='nume-produs'>" . $nume . "</span>". 
                " <br />Pret: " . $pret . "lei/Kg". 
                " <br />Categorie: " . $categorie . 
                " <p class='but-comanda' value='".$nume."' ><a href='##'>Comanda!</a></p></div></div>";
        }

}
?>

请问,好吗?

1 个答案:

答案 0 :(得分:0)

In&#34; .but-comanda&#34;点击您正在定义的事件&#34; dataId&#34;作为全球变量。那背后有什么特别的原因吗?

您可以尝试使用var?

作为前缀
$(".but-comanda").click(function(e){
e.preventDefault();
var produs = $(this).attr('value');
var dataId = 'produs='+ produs; 

$.ajax ({
type: "POST",
url: "cos.php",
data: dataId,
cache: false,
success: function(html)
    { $("#cos").html(html);}
});
});