2017-11-10 7 views
0

から戻って私のAJAX結果に値を取得する方法:私はこのようなPHPファイルへのポストを作っていますPHPファイル

$(document).ready(function() { 
    $(document).on('click', '.menulistnaam', function(){ 
     var menucatnaam = $(this).attr('id'); 

     $.post("include/menuinclude.php?menucat="+menucatnaam, function(result){ 
      $("#menukaart").html(result); 
     }); 
    }); 
}); 

すべてが正常に動作しますが、私はに戻って特定の結果を送信したいですこのコード。私のPHPで

は、私は、次があります。

$menucatnaam = $_GET['menucat']; 

$menu = " 
select 
    cnt.catid, cat.id, cnt.title, cnt.introtext, cnt.fulltext, cnt.ordering, cnt.images, cnt.alias, cnt.state, f.item_id, cat.id, cat.title, 
    max(case when f.field_id = 8 then f.value end) as nieuw, 
    max(case when f.field_id = 7 then f.value end) as beschrijving, 
    max(case when f.field_id = 5 then f.value end) as prijs, 
    max(case when f.field_id = 4 then f.value end) as inhoud, 
    max(case when f.field_id = 3 then f.value end) as media 
    from snm_fields_values f 
    join snm_content cnt 
    on cnt.id = f.item_id 
    join snm_categories cat 
    on cnt.catid = cat.id 
    where cnt.state = 1 
    and cat.alias = '".$menucatnaam."' 
    group by f.item_id 
    order by f.item_id, media, beschrijving, prijs, inhoud, nieuw 
"; 

にはどうすれば$menucatnaamの値を送り返すことができますか?

最後にクリックしたアンカータグを確認して、それにactiveクラスを付けたいと思います。

私はちょっとグーグルで調べて、配列を作成してからjsonでエンコードしなければならないが、それを動作させることはできなかった。

$aliasjson = json_encode(array("aliasresult"=>"$menucatnaam")); 
echo $aliasjson; 

そして、私のjs:

私の試みは、このでした

$.post('include/menuinclude.php', {"menucat": menucatnaam}, function (result) { 
    $("#menukaart").html(result); 
    console.log(result.aliasresult); 
}, 'json'); 

しかし、それは私がすべてで、私はアンカータグは何もなかったクリックしたときに望んでいた結果を得られませんでしたハプニング。

どうすればいいですか? AJAX呼び出しで

+0

を必要PARAMTERエコー任意のjqueryのコードを変更しないでください下に追加'console.log(result.aliasresult);の出力は何ですか? – 3Dos

+0

あなたはPOSTを送信していますが、 '$ _GET'を探しています – charlietfl

+0

APIの正しい使い方:[' jQuery.post(url [、data] [、success] [、dataType]) '](https://api.jquery。 com/jquery.post /#jQuery-post-url-data-success-dataType) – Xorifelse

答えて

1

パラメータ

dataType: "json",//tells jQuery that you want it to parse the returned JSON 

と応答

result = jQuery.parseJSON(result);//parsing JSON to string 
console.log(result.result); 

か、他 を取得した後、単にPHP

から
echo $menucatnaam; 
関連する問題