I wrote a function that, when clicked on the ajax button, accesses the file with a request and pulls the result from it in json format. This is either result: fail or result: success . Over time, the task became more difficult and now it is necessary to extrude several results with one query and make several conditions for them (in the example one condition is indicated). How to do this?

 //Функция обработчик кнопки function butt() { $.ajax({ type: 'post', url: 'phpOsago.php', data: $('.passegAdd').serialize(), dataType: 'json', success: function (data) { var json = JSON.parse(data); console.log(json); if (json.result === 'success') { document.getElementById("sex").style.visibility = "visible"; } else { document.getElementById("sex").style.visibility = "hidden"; $('#00015NUMBEROSAGO').removeClass('rfield'); $('#from1').removeClass('rfield'); $('#to1').removeClass('rfield'); $('#fileOSAGO').removeClass('rfield'); } } }); return true; } 

 //phpOsago.php Ajax запросом обращаемся к нему $arSelect = Array("ID", "IBLOCK_ID", "NAME", "PROPERTY_NEED_OSAGO", "PROPERTY_OWNER_AUTO", "PROPERTY_DOGOVOR_ARENDI", "PROPERTY_DATE_NUMBER"); $arFilter = Array("IBLOCK_ID" => "3", "ACTIVE" => "Y", "ID"=> $_POST["99999company"]); $res = CIBlockElement::GetList(Array(), $arFilter, false, Array("nPageSize" => 300), $arSelect); while ($ob = $res->Fetch()) { $result[] = $ob["PROPERTY_NEED_OSAGO_VALUE"]; $result[] = $ob["PROPERTY_OWNER_AUTO_VALUE"]; $result[] = $ob["PROPERTY_DOGOVOR_ARENDI_VALUE"]; } if($result[0]["PROPERTY_NEED_OSAGO_VALUE"] == "1"){ $response = '{"result": "success"}'; echo json_encode($response); }else{ $response = '{"result": "fail"}'; echo json_encode($response); } if($result[1]["PROPERTY_OWNER_AUTO_VALUE"] == "1"){ $response1 = '{"result1": "success1"}'; echo json_encode($response1); }else{ $response1 = '{"result1": "fail1"}'; echo json_encode($response1); } if($result[2]["PROPERTY_DOGOVOR_ARENDI_VALUE"] == "1"){ $response2 = '{"result2": "success2"}'; echo json_encode($response2); }else{ $response2 = '{"result2": "fail2"}'; echo json_encode($response2); } 

  • one
    In json_encode , the argument is of type mixed , so you can pass a pre-created object, an array, etc. php.net/manual/ru/function.json-encode.php - XelaNimed
  • @XelaNimed and how do I transfer the results of the conditions in one array? - Evgeny Pivovarov
  • one
    Can't you do $response = json_encode($ob) in a while and then output its echo $response instead of a heap of conditions? - XelaNimed
  • @XelaNimed conditions are somehow needed, the presence of the value 1 or 0 for the variable is checked. - Evgeny Pivovarov
  • one
    Well, why not interpret it on the client side? And so you can get confused as a result1,2,3,n . The names of the returned results you will not talk about anything on the client side. - XelaNimed

1 answer 1

I suggest the following scheme file handler phpOsago.php

 <?php define("NO_KEEP_STATISTIC", true); define("NO_AGENT_STATISTIC", true); define("NOT_CHECK_PERMISSIONS", true); require_once($_SERVER['DOCUMENT_ROOT']. "/bitrix/modules/main/include/prolog_before.php"); $context = \Bitrix\Main\Application::getInstance()->getContext(); $response = new \Bitrix\Main\HttpResponse($context); $response->addHeader("Content-Type", "application/json"); $request = $context->getRequest(); $request->addFilter(new Bitrix\Main\Web\PostDecodeFilter); if ($result[0]["PROPERTY_NEED_OSAGO_VALUE"] == "1"){ $arResult['PROPERTY_NEED_OSAGO_VALUE'] = [ 'result' => 'success' ]; }else{ $arResult['PROPERTY_NEED_OSAGO_VALUE'] = [ 'result' => 'fail' ]; } if($result[2]["PROPERTY_DOGOVOR_ARENDI_VALUE"] == "1"){ $arResult['PROPERTY_DOGOVOR_ARENDI_VALUE'] = [ 'result' => 'success' ]; }else{ $arResult['PROPERTY_DOGOVOR_ARENDI_VALUE'] = [ 'result' => 'fail' ]; } // возвращаем результат $response->flush(Bitrix\Main\Web\Json::encode($arResult));