2015-01-26 2 views
1

나는이 작은 문제를 파악하려고 노력해 왔으며 내 머리카락을 꺼낼 준비가되었습니다. 나는 프로그래머보다 디자이너가 더 많지만, 필자는 학교에서 PHP로 제출 양식을 만들었습니다. 여기에 내가 일하고 시험 형태 :PHP 라디오 버튼을 선택한 경우 텍스트 입력란

http://www.inrf.uci.edu/php-form-test/

사람이 외부 사용자를 선택했을 때 내가 뭘하려고 오전, 그들은 그들의 회사 명을 기입해야한다. 누군가 내부 사용자를 선택하면 그룹/PI 이름을 입력해야합니다. 이것은 잘 작동하지 않습니다. 내부를 선택하면 회사 이름이 아닌 그룹/PI 이름 만 필요로합니다. 어떤 도움을 주시면 감사하겠습니다. 고맙습니다! 여기

// internal selected (group pi) 

if ($userType == 'internal') { 
    $internalChecked = ' checked="checked" '; 
} else { 
    if(trim($_POST['grpPI']) === '') { 
     $grpPIError = '<span class="error">Please enter your group or PI name.</span>'; 
     $hasError = true; 
    } else { 
     $grpPI = trim($_POST['grpPI']); 
    } 
} 

    } 

// external selected (company name) 

if ($userType == 'external') { 
    $externalChecked = ' checked="checked" '; 
} else { 
    if(trim($_POST['companyName']) === '') { 
     $companyNameError = '<span class="error">Please enter your company name.</span>'; 
     $hasError = true; 
    } else { 
     $companyName = trim($_POST['companyName']); 
    } 
} 

및 HTML :

<table class="usertype" id="usertype"> 

         <tr> 
          <td colspan="2"> 
          <input type="radio" name="userType" value="external" <?php echo $externalChecked; ?>>External 
          <select name="externalSelect" size="1"> 
           <option selected="selected">Select</option> 
           <option>Academic</option> 
           <option>Non-Profit</option> 
           <option>Commercial</option> 
          </select><?php if($externalError != '') { ?><span class="error"><?=$externalError;?></span><?php } ?></td> 
      </tr> 
         <tr> 
          <td colspan="2"><label for="companyName">Company Name:</label> 
          <input type="text" size="40" name="companyName" id="companyName" value="<?php if(isset($_POST['companyName'])) echo $_POST['companyName'];?>" /> 
       <?php if($companyNameError != '') { ?> 
       <span class="error"><?=$companyNameError;?></span><?php } ?></td> 
         </tr> 
         <tr> 
          <td colspan="3"><input type="radio" name="userType" value="internal" <?php echo $internalChecked; ?>>Internal &nbsp;&nbsp;&nbsp;&nbsp;<input title="Enter Account String. Required for Internal Users." type="text" size="30" name="ucfund" value="<?php if(isset($_POST['ucfund'])) echo $_POST['ucfund'];?>" placeholder="KFS or Account (eg. xxxxxxx)" /><?php if($ucfundError != '') { ?><span class="error"><?=$ucfundError;?></span><?php } ?>&nbsp;-&nbsp; <input title="Enter Fund String. Required for Internal Users." type="text" size="25" name="ucfund2" value="<?php if(isset($_POST['ucfund2'])) echo $_POST['ucfund2'];?>" placeholder="Fund (eg. xxxxx)" /><?php if($ucfund2Error != '') { ?><span class="error"><?=$ucfund2Error;?></span><?php } ?><br /><span class="small">(Please enter KFS number or Accound Fund)</span> 
</td> 
         </tr> 

         <tr> 
          <td colspan="3">Optional&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" size="30" name="sub" value="<?php if(isset($_POST['sub'])) echo $_POST['sub'];?>" placeholder="Sub" /><?php if($subError != '') { ?><span class="error"><?=$subError;?></span><?php } ?>&nbsp;-&nbsp; <input type="text" size="25" name="proj" value="<?php if(isset($_POST['proj'])) echo $_POST['proj'];?>" placeholder="Project" /><?php if($projError != '') { ?><span class="error"><?=$projError;?></span><?php } ?> 
</td> 
         </tr> 
         <tr> 
          <td colspan="3"><label for="grpPI">Group/PI:</label> 
          <input type="text" size="40" name="grpPI" id="grpPI" value="<?php if(isset($_POST['grpPI'])) echo $_POST['grpPI'];?>" /> 
       <?php if($grpPIError != '') { ?> 
       <span class="error"><?=$grpPIError;?></span><?php } ?></td> 
       </tr> 

     </table> 
+0

어디'$ userType'가 결정됩니다 두 개의 코드 경로 중 하나만 실행되도록

당신은 함께 두 if() 문을 묶어하는 else 절을해야합니까? 코드가 예상 한 것과 다르게 동작하기 시작한 곳을 확인하기 위해 디버깅을 했습니까? –

+0

'($ userType == 'internal') ELSE IF ($ userType == 'external') ... ' –

+1

'var_dump ($ userType);'사용자 유형이 서버에 분명히 존재하지 않게하십시오. 그것을 보내고'$ _POST'를 사용하여 가져와야합니다. – Abel

답변

1

당신의 논리를 참고 : 여기에

내 PHP는 여기에 코드입니다

if ($user == 'internal') { 
    ... 
} else { 
    ... check for empty field ... 
} 

그래서 사용자가 선택하면 '외부', 다음 "internal-only"필드는 비어있을 것이므로 오류가 발생하게됩니다. 사용자가 "외부"를 선택했기 때문에 내부 전용 필드를 설정해야하는 이유는 무엇입니까?

마찬가지로 external 검사의 경우 내부 전용 필드가 비어 있고 다른 오류가 발생합니다.

if ($user == 'internal') { 
    ... do internal-only checks ... 
} else if ($user == 'external') { 
    ... do external-only checks ... 
} else { 
    ... $user is undefined type, "zomg how'd you get here" error handling. 
} 
+0

고맙습니다. 마침내 제대로 작동했습니다! Wahoo !! :) – user2292856

관련 문제