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); }
json_encode, the argument is of typemixed, so you can pass a pre-created object, an array, etc. php.net/manual/ru/function.json-encode.php - XelaNimed$response = json_encode($ob)in awhileand then output itsecho $responseinstead of a heap of conditions? - XelaNimedresult1,2,3,n. The names of the returned results you will not talk about anything on the client side. - XelaNimed