2011-07-03 6 views
0

누군가이 코드를 수정하도록 도와 줄 수 있습니까? 수정하고 싶은 부분은 다음과 같습니다.양식 값 변수 및 제출 문제

사람이 사용자 이름이나 암호를 입력하지 않은 경우 맨 위의 '죄송합니다, 액세스 할 수 없음'메시지없이 오류 텍스트 (옆에 빨간색 메시지가 표시됨) 만 입력하면됩니다.

또한 사용자가 액세스 권한을 얻거나 얻지 못하면 텍스트 필드와 제출 버튼을 사라지게하고 싶습니다.

이것은 실제 양식이 아니므로 사용자 이름과 암호를 어떻게 사용하고 있는지 걱정하지 마십시오. 가능하다면 대부분의 코드가 동일하게 유지 될 수 있다고 생각하십니까? ?

감사합니다.

<?php 
    echo '<style type="text/css"> 
     .error 
     { 
      color: red; 
     } 
    </style>'; 

    $error = false; 

    if (isset($_POST['submitted'])) 
    { 

    if (empty($_POST['username']) || empty($_POST['password'])) 
    { 
    $error = TRUE; 
    } 

    if (!$error && $_POST['username']=='test' && $_POST['password']=='abc123') { 

    echo '<p>Correct. Thank you for entering.<p/>'; 
    } 

    else 
    { 
    echo '<p>Sorry, no access.</p> 
    '; 
    } 

    } 
    ?> 
    <form action="" method="post"> 
    Username: <input type="text" name="username" size="20" value="<?php 
    if (isset($_POST['submitted']) && !empty($_POST['username'])) 
    { 
     echo $_POST['username']; 
    } ?>" /> 
    <?php 
    if (isset($_POST['submitted']) && empty($_POST['username'])) 
    { 
     echo '<span class="error">Please enter a username.</span>'; 
    } 
    ?> 
    <br />Password: <input type="password" name="password" size="20" value="<?php 
    if (isset($_POST['submitted']) && !empty($_POST['password'])) 
    { 
     echo $_POST['password']; 
    } ?>" /> 
    <?php 
    if (isset($_POST['submitted']) && empty($_POST['password'])) 
    { 
     echo '<span class="error">Please enter a password.</span>'; 
    } 
    ?> 
    <br /><input type="submit" value="Log in" /> 
    <br /><input type="hidden" name="submitted" value="true" /> 
    </form> 

답변

0

이 시도 :

<style type="text/css"> 
.error { 
color: red; 
} 
</style> 
<?php 

$submitted = isset($_POST['submitted']); 
$userName = isset($_POST['username']) ? $_POST['username'] : null; 
$password = isset($_POST['password']) ? $_POST['password'] : null; 

if($submitted) { 
    if (!$userName || !$password) { 
     echo '<p class="error">Please go back and fill the inputs.</p>'; 
    } elseif($userName == 'test' && $password == 'abc123') { 
     echo '<p>Correct. Thank you for entering.<p/>'; 
    } else { 
     echo '<p class="error">Sorry, no access.</p>'; 
    } 
} else { 
?> 
<form action="" method="post"> 
Username: <input type="text" name="username" size="20" value="<?php echo $userName; ?>" /> 
<br /> 
Password: <input type="password" name="password" size="20" value="" /> 
<br /><input type="submit" value="Log in" /> 
<br /><input type="hidden" name="submitted" value="true" /> 
</form> 
<?php } ?> 
+0

작동하는 동안 (그리고 그런데 감사합니다) 어쨌든 입력 필드 옆에 개별 오류 메시지가 나타납니다. 궁극적으로 내가 원하는 것은 물론, 내가해야 할 가장 어려운 일입니다! LOL – Reki

+0

@Reki 죄송합니다. 그게 당신 일입니다. 이것은 프로그래밍 팜이 아닌 Q & A 사이트입니다. –

+0

나는 내 문제가 q와 q의 'q'라고 생각했다! 나는 누군가가 나를 위해 무엇인가를 처음부터 쓰기를 요구하지 않고 있습니다. 나는 가능한 한 최선을 다해 문제를 해결했습니다. 나는 정직하게 올바른 방향으로 나아갈 것을 기대하고 있습니다 ... – Reki

0

는 양식에 태그의 onsubmit 이벤트를 사용하는 것이 좋습니다.

제출하지 못하도록 php와 javascript를 결합해야합니다. onSubmit에 의해 호출되는 Jscript 함수를 만듭니다. 값이 채워지지 않은 경우 false를 반환합니다. 제출 버튼을 죽이고 화면에 직접 인쇄합니다. 양식에 다음

$submitted = isset($_POST['submitted']); 
$userName = isset($_POST['username']) ? $_POST['username'] : null; 
$password = isset($_POST['password']) ? $_POST['password'] : null; 

if($submitted) 
{ 
    if (!$userName || !$password) { 
     echo '<p class="error">Please go back and fill the inputs.</p>'; 
    } 
} 

을 그리고 :

그래서 그냥이 걸릴 <form action="" method="post" onSubmit="checkForm()">

그럼 당신은 자바 스크립트와 양식을 확인하는거야 방법을 알아낼. 나머지는 함께 모을 수 있습니다.