簡體   English   中英

如何從PHP文件獲取返回到我的Ajax結果的值

[英]How to get a value back to my ajax result from a PHP file

我正在向這樣的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調用中添加以下參數

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);

否則不要更改任何jquery代碼,只是從PHP回顯所需的參數

echo $menucatnaam;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM