繁体   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