简体   繁体   English

如何从PHP文件获取返回到我的Ajax结果的值

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

I am making a post to a php file like this: 我正在向这样的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);
      });
  });
});

All works fine but I would like to send a specific result back to this code. 一切正常,但我想将特定结果发送回此代码。

In my PHP I have the following: 在我的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
";

How can I send back the value of $menucatnaam ? 如何寄回$menucatnaam的价值?

In the end I want to check what anchor tag was clicked and give it an active class. 最后,我想检查单击了哪个锚标记并为其提供active类。

I googled a bit and found out I had to create an array and then encode it with json but I couldn't get it to work. 我用谷歌搜索了一下,发现我必须创建一个数组,然后用json对其进行编码,但是我无法使其工作。

My attempt was this: 我的尝试是这样的:

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

And then my js: 然后我的js:

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

But that didn't give me the result I wanted at all, when I clicked an anchor tag nothing was happening. 但这根本没有提供我想要的结果,当我单击锚标记时,什么也没有发生。

How can I do this? 我怎样才能做到这一点?

In ajax call add below parameter 在ajax调用中添加以下参数

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

and after getting response 得到回应后

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

or else don't change any jquery code just echo required paramter from PHP 否则不要更改任何jquery代码,只是从PHP回显所需的参数

echo $menucatnaam;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM