2014-09-12 4 views
0

내 목표 : 누군가가 재정 지원을받는 상자를 확인한 경우받는 유형을 확인했는지 확인해야합니다.양식에 PHP 유효성 확인 구문이 잘못되었습니다.

여기까지 제가 PHP 유효성 검사 부분에 있습니다.

if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))): 

    if (isset($_POST['financial_aid'])) { $financial_aid = $_POST['financial_aid']; } 

if ($financial_aid === '') : 
     $err_financial_aid = '<div class="error">Sorry, this is a required field</div>'; 
    endif; // input field empty 

양식 코드 :

<p><strong>Are you receiving Financial Aid? </strong><br /> 
<select name="Financial_Aid" size="1" tabindex="8"> 
<option value="blank" selected>Please select one</option> 
<option value="Yes">Yes, I Receive Financial Aid</option> 
<option value="No">No, I Do Not Receive Financial Aid</option> 
</select></p> 
<p>Please indicate all forms of Financial Aid that you receive:</p> 
<p> 
<input type="checkbox" name="financial_type" value="<?php ((isset($financial_type)) && (in_array("hope", $financial_type))) { echo "checked"; } ?> " tabindex="9">HOPE Scholarship/HOPE Grant<br> 
<input type="checkbox" name="financial_type" value="<?php ((isset($financial_type)) && (in_array("pell", $financial_type))) { echo "checked"; } ?>" tabindex="10">Pell Grant<br> 
<input name="financial_type" type="checkbox" value="Yes" tabindex="11">Student Loan<br> 
<input type="checkbox" name="financial_type" value="<?php ((isset($financial_type)) && (in_array("loan", $financial_type))) { echo "checked"; } ?>" tabindex="12">Veterans Benefits<br> 
<input type="checkbox" name="financial_type" value="<?php ((isset($financial_type)) && (in_array("tuition_reimbursement", $financial_type))) { echo "checked"; } ?>" tabindex="13">Tuition Reimbursement<br> 
<input type="checkbox" name="financial_type" value="<?php ((isset($financial_type)) && (in_array("tuition_waiver", $financial_type))) { echo "checked"; } ?>" tabindex="14">Tuition Waiver<br> 
<input type="checkbox" name="financial_type" value="<?php ((isset($financial_type)) && (in_array("other_scholarships", $financial_type))) { echo "checked"; } ?>" tabindex="15"> Other Scholarships </p> 

내 첫번째 생각은 내가 문을 다른 필요하다는 것입니다.

if ($financial_aid === 'yes') { 

} elseif ($hope) { ((isset($financial_type)) && (in_array("hope", $financial_type))) 
    return true; 
} elseif ($pell) { ((isset($financial_type)) && (in_array("pell", $financial_type))) 
    return true; 

} elseif ($loan) { ((isset($financial_type)) && (in_array("loan", $financial_type))) 
    return true; 

} elseif ($veterns) { ((isset($financial_type)) && (in_array("veterns", $financial_type))) 
    return true; 

} 
    elseif ($tuition_reimbursement) { ((isset($financial_type)) && (in_array("tuition_reimbursement", $financial_type))) 
    return true; 

} 
    elseif ($tuition_waiver) { ((isset($financial_type)) && (in_array("tuition_waiver", $financial_type))) 
    return true; 

} 
    elseif ($other_scholarships) { ((isset($financial_type)) && (in_array("other_scholarships", $financial_type))) 
    return true; 

} else { 
    !empty $err_finanical_type=please select one 
return false; 
} 

} 

내가 뭔가를 잃어 버렸어. 나는 단지 무엇을 모른다. 누군가 나를 도울 수 있습니까?

+0

첫 번째 if 문에 닫는 'endif;'가 없으므로 입력 태그를'/> '및'else {! empty $ err_finanical_type = 하나만 선택하십시오. false로 닫아야합니다. }'의미가 없습니다. – iswinky

답변

0

먼저 입력 한 내용이 많은 사용자 경험을 향상시키는 것이라고 가정 해 보겠습니다.

1) 당신은 사용하여 양식 코드를 보여줍니다 :

재정 지원과 예

하지만 문이

를 보여줍니다 귀하의 경우

는 는

PHP 검증에 대한 귀하의 질문은 응답의 3 개 영역이 보인다

$financial_aid === 'yes' 

낙타 텍스트는 동일한 변수를 식별하기 위해 if 코드에서 동일해야하며 === 동일 함은 예와 동일하지 않습니다 - 모든 사항 시계를 조화롭게 유지하십시오.

2) 각 입력 양식 필드의 id 구성 요소가 누락되었습니다. 'name = "financial_type"'만 사용하고 있습니다. 이름은 양식 제출을 통해 데이터 항목을 전송하기위한 것이지만 id = "financial_type"을 사용하면 javascript 또는 css를 사용하는 각 라디오 버튼/체크 박스에 대해 다른 id에보다 쉽게 ​​작동합니다. 의심 스럽다면 name = "item1"과 id = "item2"를 모두 수행하고 다음 항목은 동일하지만 item2 등으로 변경하십시오. 몇 가지 체크 박스 중 하나를 선택하는 상황이 생길 때까지 두 세계에서 가장 좋습니다 (예 : 상자 1은 빨간색 상자 2 개, 파란색 2 개, 녹색 3 개)가 있습니다.

3)은 "onclick을 ="해봐요() "해봐요()가 같은 트리거 제출 폼 제출을 사용하거나 "버튼 유형 = "버튼을 사용하는 경우 경우 표시되지 않습니다.

다른 쪽 끝의 데이터를 확인하면 전달되는 것을 확인하거나 선택하지 않고 "켜기"라는 단어를 나타낼 수 있습니다. 양식을 제출할 때 POST를 사용하는 것보다 AJAX XMLHttpRequest 전송을 사용하여 이것을 발견했습니다. "on"확인/check 대신에 check 또는 uncheck 상태 표시 ... 아래의 dosomething() 스크립트를 시도하십시오. 스크립트는 전송할 localStorage 배열뿐만 아니라 lname이있는 기존 유형 및 입력 ID의 표준 유형도 표시합니다. localStorage 및 제출 양식은 자체적으로 작동하지 않으며 localStorage는 전송되지 않습니다. 대신 XMLHttpRequest를 사용하여 양식 필드와 localStorage를 모두 catchby.php의 코드로 서버에서 처리하도록 허용해야합니다.

이 예제에는 클라이언트가 버튼을 보내고 있습니다.같은 페이지에서, 해봐요()을 트리거

<button type="button" onclick="dosomething();" class="btn btn-primary btn-lg">Complete </button> 

은 예 또는 사용하기에 따라 없음 확인 또는 선택하지 않은 상태

<script> 
function dosomething() { 
    // Start XMLHttpRequest 
    var DaData = new XMLHttpRequest(); 
    // where to pass data to 
    var url = "catchby.php"; 
    // Data items to pass.. lname is a field in the form 
    var nm = document.getElementById("lname").value; 
    /* financial_type is the id of checkbox input converted to Yes or No avoiding the constant "on" being sent */ 
    if($('#financial_type').attr('checked')) { 
     var cb = "Yes"; 
     } else { 
     var cb = "No"; 
     } 

    /* svdStorage is the name of localStorage that is not part of the form that is other saved input user has entered saved on their client side computer that stays for next web visits... in this case an array that includes any outside the form data to send. */ 
    var ct = localStorage.getItem('svdStorage'); 

    // variables to be available in catchby.php via $_REQUEST in code 
    var vars = "nname="+nm+"&ftype="+cb+"&scdStuff="+ct; 

    DaData.open("POST", url, true); 
    DaData.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

    // Check onreadystatechange for XMLHttpRequest 
    DaData.onreadystatechange = function() { 
    if(DaData.readyState == 4 && DaData.status == 200) { 
     var return_data = DaData.responseText; 
     document.getElementById("status").innerHTML = return_data; 
    } 
    } 
    // OK Send data items and process data transfer 
    DaData.send(vars); 
    document.getElementById("status").innerHTML = "Processing..."; 
    } 
    </script>  


Then to receive data being sent here is catchby.php coding... 

<?php 
$customerInformation = ""; 
if(!empty($_REQUEST["name"])) 
    { 
    $customerInformation .= "Name: "; 
    $customerInformation .= $_REQUEST["name"]; 
    } 

    // this net line will show the Yes or No reflecting the checked status instead of just the work "on" 
    $customerInformation .= "<br/>The financial requested was: ".$_REQUEST["ftype"]; 

    // convert localStorage array to data items 
    $zscdStuff = json_decode($_REQUEST["scdStuff"]); 
    // determine # of array items 
    $length = count($zscdStiff); 
    for ($i = 0; $i < $length; $i++) { 
    // define items in each line of array 
    // then 
    // do what is needed with each line item 
} 
?> 

주 ... XMLHttpRequest의 기능이 작동 PHP

에 자바 스크립트에서 보내 양식 항목 및 전송하려는 다른 모든 변수 (비동기)는 제출하는 동안 양식 항목 만 전송합니다.

정확하게 똑같은 이름을 사용하는 경우 2) 제출 라인의 각 항목에 매개 변수 id와 매개 변수를 사용하고 3) 선택/선택 취소 상태를 "예"및 "아니오"와 같은 명시적인 텍스트 항목으로 변환하십시오. 당신의 결과는 당신이 기대하는 것입니다.

+0

2) '의심스러운 경우'name = "item1"및 id = "item2" '둘 다 수행하고 의심스러운 경우'name = "item1" id = "item1" – Arnold

관련 문제