2016-09-12 3 views
1

동일한 페이지에 두 개의 양식 제출. 두 가지 양식과 필드가 있으며 ID와 이름이 다릅니다. 두 형식 중 하나가 잘 작동하지만 두 번째 문제가 있습니다 잘못된 항목이 있으면 정상적으로 오류 메시지를 받아야하며 모든 내용이 정상이면 배열 데이터가 필요합니다. 양식 2 단계에서 메시지를 코딩해야합니다. 다른 형식의 데이터 검색

내 PHP 코드 :

//To get errors messages normally 
if(!empty($errors2)) { 
      $data != "noPassed"; 
    echo display_errors2($errors2); 

} 
else { 
    //To retrieve data I need to code my message in step 2 
     $data2 = array(); 
     $data2['client_id'] = $client_id; 
     $data2['client_civilite'] = $client_civilite; 
     $data2['client_name'] = $client_name; 
     $data2['res2'] = "passed"; 


     header('Content-Type: application/json'); 
     echo json_encode($data2); 
} 

스크립트 : g에

jQuery.ajax({ 
     url : '/myfolder/parsers/check.php', 
     method : 'POST', 
     type : 'POST', 
     data : data, 
     success : function(data){ 
     //I presume errors in followed condition 
     if (data != 'passed') { 
     jQuery('.messages_erreurs').html(data); 
     } 
     if(data2.res2 == 'passed') { 
     $(".frm").hide("fast"); 
     $("#step2").show("slow"); 
     $(".open1").css("display","none"); 
     $(".open2").css("display","inline-block"); 

     // accès à data.client_civilite, data.client_id, pour message d'identification . 

     $('#clt_id').val(data2.client_id); 
     $("#check_ok").html(data2.client_civilite+" "+data2.client_nom+" "+': identification réussie.'); 

     } 

기능 등의 오류 메시지를 텍스트, HTML 또는 JSON을 :

function display_errors2($errors2) { 
$display2 = '<ul class="bg-danger">'; 
foreach ($errors2 as $error2){ 
    $display2 .= '<li class="text-danger">'.$error2.'</li>'; 
}  
    $display2 .= '</ul>'; 

return $display2; 
} 

답변

0

먼저 당신은 당신이 쉽게 자바 스크립트 쪽을 구문 분석 할 수 있도록 다시 PHP 스크립트에서 보낼 것을 결정해야합니다.

요청이 성공하면 json을 보내지 만 오류가 발생하면 해당 헤더가 표시되지 않으므로이 경우 일반 텍스트를 보내고있는 것으로 의심됩니다.

json을 다시 보내면 사용하기 전에 구문 분석해야합니다. dataType 속성을 설정하면됩니다. 그런 다음 data 변수 (... PHP 스크립트를 기반으로) 대상이 될 것입니다 그래서 당신이 값에 액세스 할 수 있습니다

jQuery.ajax({ 
    url : '/myfolder//parsers/check.php', 
    method : 'POST', 
    // Set the correct data type 
    dataType : 'json', 
    data : data, 
    success : function(data){ 
     // Access the correct values 
     if (data.res2 != 'passed') { 
      // You need to set the errorText property (or similar) in php 
      jQuery('.messages_erreurs').html(data.errorText); 
     } 
     // etc. 
+0

메시지 오류 데이터를 기반으로 ... (나는 아래의 코드를 추가). 나는 2 단계에서 3e 데이터를 검색하기 위해 제이슨 에코와 res2를 만들었습니다. – juliet

+0

혼란 스러웠습니다 : 그것은 단지 잘못된 배치 포옹이었습니다 .... – juliet

관련 문제