2016-11-24 6 views
1

私は電子商取引のWebサイトのページを表示しています。製品は、顧客によってチェックされたブランドに基づいてブランドによってフィルタリングされる。このために、私はブランドがチェックされるたびにajaxリクエストを使用しました。問題は、ページが同じページに送信しているget変数を受け取れないことです。 ajaxリクエストはエラーを出さず、クロムデバッガ側もエラーを表示していません。これはページです: snapshot of the products page同じPHPページへのAjaxリクエストはエラーは発生しませんが動作しません

そして、これはページのコードです:私は、AJAXリクエストでbeforeSendでアラートを()を使用していますし、予想通り、それは正しいデータを返す

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd"> 
<?php 
    session_start(); 
    include('includes/pdo_connect.php'); 
    include('includes/pdo_functions.php'); 


    $logged_in; 
    $user_first_name; 

    if(isset($_SESSION['user'])){   //Determining if the user is logged in or not. 
     if($_SESSION['user']=='user'){ 
      $user_id = $_SESSION['user_id']; 
      global $logged_in; 
      $logged_in = true; 
      global $user_first_name; 
      $user_first_name = $_SESSION['user_first_name']; 
     } 
    } else { 
     $_SESSION['user'] = 'guest'; 
     $user_id = $_SERVER['REMOTE_ADDR']; 
    } 

    $cat; 

    if(isset($_GET['cat'])){ 
     global $cat; 
     $cat = $_GET['cat']; 
    } 


    include('includes/logged_in.php'); 



    if(isset($_GET['brand_list'])){ 
     $brand_list = $_GET['brand_list']; 

    } else { 
     echo "<script>alert('value not received');</script>"; 
    } 


?> 
<html> 
<head> 
    <title></title> 


    <link rel="stylesheet" type="text/css" href="styles/list_style.css?<?php echo time(); ?>"> 
    <link rel="stylesheet" type="text/css" href="styles/thickbox.css" media="screen"> 

    <script type="text/javascript" src="js/jquery-3.1.1.js"></script> 
    <script type="text/javascript" src="js/thickbox.js"></script> 

    <?php 


    $where = array(); 

    if(!empty($brand_list)){ 
     echo "ajax working"; 
    if(strpos($brand_list, ',')!==false){ 
     $brand_choices = explode(',', $brand_list); 
     $barray = array(); 
     foreach($brand_choices as $value) { 
      $barray[] = "brand_id = $value"; 
     } 
     $where[] = '('.implode(' OR ', $barray).')'; 
    } else { 
     $where[] = '(brand_id= '.$brand_list.')'; 
    } 
    } else { 
    //echo "ajax not working "; 
    } 

    $w = implode(' AND ', $where); 

    $w = "where product_cat=$cat ".$w; 


    $filter_query = "select * from products $w "; 
    echo "filter query: ".$filter_query; 

    $first_load = 'filter'; 

    function show_filtered(){ 
     //echo "<script>alert('filter query working');</script>"; 
    global $filter_query; 
    global $con; 
    global $brand_name; 

    try{ 

     $stmt = $con->prepare($filter_query); 

     $stmt->execute(); 

     $result = $stmt->fetchAll(); 

     foreach ($result as $data) { 

      $product_id = $data['product_id']; 
      $product_cat = $data['product_cat']; 
      $product_brand = $data['product_brand']; 
      $product_title = $data['product_title']; 
      $product_price = $data['product_price']; 
      $product_desc = $data['product_desc']; 
      $product_image = $data['product_image']; 

      echo " 
        <div class='product_container $brand_name'> 
         <a href='details.php?pid=".$product_id."' alt='".$product_title."'> 
          <div class='img_div'> 
          <img src='admin/product_images/".$product_image."?".time()."' alt='".$product_title."'/> 
          </div> 
          <div class='index_product_desc'>".$product_title."</div> 
          <div class='index_product_price'>&#8377;".$product_price."</div> 
         </a> 
        </div> 
      "; 
     } 

    } catch(PDOException $e){ 
     echo "Error in show_list(): ".$e->getMessage(); 
    } 

    } 


    function show_brands(){ 
     global $con; 
     global $cat; 
     global $brand_name; 

     try{ 
      $query = "select * from cat_brand where cat_id = $cat"; 

      $stmt = $con->prepare($query); 

      $stmt->execute(); 

      $result = $stmt->fetchAll(); 

      //$brand = array(); 

      foreach ($result as $data) { 
       $brand_id = $data['brand_id']; 

       //echo "<script>alert('$brand_id');</script>"; 

       $query1 = "select * from brands where brand_id = $brand_id"; 

       $stmt1 = $con->prepare($query1); 

       $stmt1->execute(); 

       $result1 = $stmt1->fetchAll(); 

       echo "<ul>"; 

       foreach ($result1 as $data1) { 
        $brand_name = $data1['brand_title']; 

        echo "<li><input type='checkbox' value='$brand_id' id='$brand_name' class='brand_check' name='brandchoice'>&nbsp;$brand_name</li>"; 

       } 

       echo "</ul>"; 
      } 
     } catch(PDOException $e){ 
      echo "Error in show_brands: ".$e->getMessage(); 
     } 
    } 

    function show_price(){ 

    } 
?> 


</head> 
<body> 

    <div class="wrapper"> 
     <header> 
       <div class="home_logo"> 
        <a href="index.php"> 
         <img src="images/skyshop_sumopaint.png" alt="Site Home"> 
        </a> 
       </div> 
       <div class="login"> 
        <a href=""><?php user();?></a>&nbsp;&nbsp;&#124;&nbsp; 
        <a href="register.php"><?php login_status(); ?></a> 
       </div> 
       <div class="form"> 
        <form method="get" target="" name="searchbar_form"> 
         <input type="text" name="searchbar" id="searchbar"> 
         <input type="submit" id="search_button" value="Search"> 
        </form> 
       </div> 
     </header> 
     <div class="menubar"> 

        <div class="dropdown"> 
         <button onclick="dropdownToggle()" class="dropdown-button">Shop By Category</button> 
         <ul class="dropdown-content" id="dropdownContent"> 
          Categories 
          <?php getcats(); ?> 
         </ul> 
        </div> 
       <div class="menubar-div"> 
        <ul class="menu-items"> 
         <?php getcats(); ?> 
        </ul> 
       </div> 
       <div class="cart"> 
        <a href="cart.php">Cart (0)</a> 
       </div> 
     </div> 

     <div class="content"> 
      <div class="nav"> 

      </div> 
      <div class="list_wrapper"> 

<!--/////////////////////////////////////////////// Filter div /////////////////////////////////////////////////////--> 

       <div class="filter"> 


        <span class="filter_heading">Select Brands</span> 
        <a href="" class="clear" id="clear_brands">Clear<a> 

        <div class="brand_div"> 
         <?php 
          show_brands(); 
         ?> 
        </div> 

        <div class="price_div"> 

        </div> 
       </div> 

<!--/////////////////////////////////////////////// List div ///////////////////////////////////////////////////////--> 

       <div class="list"> 
        <div class="loading"> 
         <img src="images/loadingAnimation.gif"> 
        </div> 
        <?php 
         show_filtered(); 
        ?> 
       </div> 
      </div> 


      <div class="footer"> 
       FOOTER 
      </div> 
     </div> 
    </div> 


<?php 


?> 
<script type="text/javascript"> 

    $(window).on('load', function(){ 

     function filter(){ 
      //alert("filter called"); 

      $('.filter .list').css('opacity', 0.5); 
      $('.loading').css('visibility', 'visible'); 

      var brandchoice = new Array(); 
      $('input[name="brandchoice"]:checked').each(function(){ 
       brandchoice.push($(this).val()); 
       $('#clear_brands').css('visibility', 'visible'); 
      }); 
      if(brandchoice==""){ 
       $('#clear_brands').css('visibility', 'hidden'); 
      } 
      var brand_list = '&brand_list='+brandchoice; 


      var data = brand_list.substring(1, brand_list.length); 
      //alert(data); 

      $.ajax({ 
       url: "list.php", 
       type: "GET", 
       data: data, 
       cache: false, 
       success: function(result){ 

        $(".filter .list").css("opacity", 1); 
        $(".loading").css("visibility", "hidden"); 
       }, 
       error: function(jqxhr, exception){ 
        console.log(jqxhr); 
       }, 
       beforeSend: function(){ 
        console.log("before send: "+data); //This is showing "brand_list=1" which is correct. 
       } 
      }); 

     } 

     $('input[type="checkbox"]').on('change', filter); 

     $('#clear_brands').on('click', function(){ 
      $('.brand_check').removeAttr('checked'); 
      filter(); 
      $('#clear_brands').css('visibility', 'hidden'); 
     }); 


    }); //end of jquery 

</script> 
</body> 
</html> 

。しかし、ページのPHPセクションは値を受け取っていません。 PHP側またはブラウザのデバッグウィンドウにエラーはありません。 Chromeの場合

+0

は、開発者tools' 'の下で'ネットワークpanel'をチェックして、変数が渡されたか取得している場合を確認してくださいチェックアウト。 –

+0

@Punit .... $(window).on( 'load')を$(document).on( 'ready')に変更すると、ajaxは完全に機能しなくなります – Vinay

+0

@Vinayあなたのajax URLがない。 [ネットワーク内]タブをクリックします。 。 –

答えて

0

私はラインであなたのコードの行をチェックします。 "filter"という名前の関数の中にajax用のコードがあることを理解してくださいfunction filter()。今

あなたはID clear_brands

$('#clear_brands').on('click', function(){ 
    $('.brand_check').removeAttr('checked'); 
      filter(); 
    $('#clear_brands').css('visibility', 'hidden'); 
}); 

を持つ要素のonclickイベントによって、このフィルタ機能を上呼び出していると、このコードによって、私はあなたがクリックするので、最後に、あなたのAJAX呼び出しが行われていないことを知るようになりましたイベントがトリガーされませんでした。

したがって、ドキュメント上でこのイベントをトリガするか、その要素をクリックして行う必要があります。

フローを一度考えてください。

ローカルと であなたのスクリプトをテストしていましたが、いくつかの変更を加えて動作させました。

は、私はちょうどこのコードの下パットスクリプトの末尾に

のようないくつかの変更を行いました。

$(document).ready(function(){ 
    $('#clear_brands').trigger("click"); 
}); 

とAjax呼び出しが実行された。..

私の全体のHTML

<!DOCTYPE html> 
<html> 
    <head> 
     <title></title> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    </head> 
    <body> 
     <a href="" class="clear" id="clear_brands">Clear<a> 
     <script type="text/javascript"> 

      $(window).on('load', function(){ 

       function filter(){ 
        //alert("filter called"); 

        $('.filter .list').css('opacity', 0.5); 
        $('.loading').css('visibility', 'visible'); 

        var brandchoice = new Array(); 
        $('input[name="brandchoice"]:checked').each(function(){ 
         brandchoice.push($(this).val()); 
         $('#clear_brands').css('visibility', 'visible'); 
        }); 
        if(brandchoice==""){ 
         $('#clear_brands').css('visibility', 'hidden'); 
        } 
        var brand_list = '&brand_list='+brandchoice; 


        var data = brand_list.substring(1, brand_list.length); 
        //alert(data); 

        $.ajax({ 
         url: "list.php", 
         type: "GET", 
         data: data, 
         cache: false, 
         success: function(result){ 

          $(".filter .list").css("opacity", 1); 
          $(".loading").css("visibility", "hidden"); 
         }, 
         error: function(jqxhr, exception){ 
          console.log(jqxhr); 
         }, 
         beforeSend: function(){ 
          console.log("before send: "+data); //This is showing "brand_list=1" which is correct. 
         } 
        }); 

       } 

       $('input[type="checkbox"]').on('change', filter); 

       $('#clear_brands').on('click', function(){ 
        $('.brand_check').removeAttr('checked'); 
        filter(); 
        $('#clear_brands').css('visibility', 'hidden'); 
       }); 


      }); //end of jquery 

      $(document).ready(function(){ 
        $('#clear_brands').trigger("click"); 
      }); 
     </script> 
    </body> 
</html> 
+0

ねえ!感謝万円。アヤックスは今働いています。バグ全体には何かが含まれていましたが、今ではアヤックスが機能しているので、物事を先に進めることができます。時間をかけて努力してくれてありがとう。あなたは最高です。 – Vinay

+0

@Vinayバグは何だったのか教えていただけますか? –

+0

バグは、ページの最初の読み込み時に 'show_filtered();'関数が呼び出されるということです。しかし、それはajaxリクエストの後では呼び出されません。これは、データベースからクエリを実行した後に製品を表示する機能です。 PHPコードでajax値を受け取った後に関数を呼び出せる方法はありますか? – Vinay

0

は、開発者ツールを開いて、エラーやデータは、要求に渡されたかどうかを確認するためにネットワークタブのコンソールをチェックするはCtrl + Shiftキー+ Iを行います。

enter image description here

+0

'http:// localhost:8080/site1/list.php?brand_list = 2&_ = 1479972796981' GETリクエストで渡される2番目のパラメータは何ですか? – Vinay

+0

私は 'brand_list = 2'しか期待していませんが、2番目のパラメータは混乱しています。どこから来るのかわかりません。 – Vinay

関連する問題