2014-01-30 4 views
0

사용자 이름과 암호를 가져와 데이터베이스에 등록하는 간단한 html 양식이 있습니다. 그러나 폼이 제출되기 전에 페이지가로드되는 즉시 한 번 다음 php if 문을 실행합니다. 다음은 그 코드입니다.양식 제출 전에 실행중인 자바 스크립트

if (($_POST['reg_username']=="") || ($_POST['reg_password']=="")) 
{ ?> 
<script> alert("please enter both email and password"); </script> 

<? } 

전체 PHP 코드가 아래에 있습니다. 이것은 최종 코드가 아니기 때문에 여전히 암호와 모든 것을 암호화하지 않았지만 그냥 테스트하고 있습니다. 선배 들께 감사드립니다.

if (($_POST['reg_username']=="") || ($_POST['reg_password']=="")) 
{ ?> 
<script> alert("please enter both email and password"); </script> 

<? } 


$reg_username=mysql_real_escape_string($_POST['reg_username']); 
$reg_username=htmlspecialchars($reg_username); 
$reg_password=mysql_real_escape_string($_POST['reg_password']); 
$reg_password=htmlspecialchars($reg_password); 



if(!empty($reg_username) && !empty($reg_password)) 
{ 

if (isset($reg_username) && isset($reg_password)) 
{ 
mysql_select_db($db, $dbconnect); 
mysql_query("INSERT into students(username,password) VALUES  
('$reg_username','$reg_password')"); 
} 
} 

?> 
+0

가능한 중복 [다시 변경하기 ference : 왜 내 자바 스크립트에서 PHP (또는 다른 서버 측) 코드가 작동하지 않습니까?] (http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side -code-in-my-javascript-not-wor) – Quentin

답변

0

변경

if(isset($_POST['reg_username']) || isset($_POST['reg_password'])) 
{ 
if (($_POST['reg_username']=="") || ($_POST['reg_password']=="")) 
{ ?> 
<script> alert("please enter both email and password"); </script> 

<?php } 
} 
+0

고마워 로버트, 지금 일하고 있습니다. 또한 초기 코드로 새 PHP 파일을 만들고 양식 값을 해당 파일로 리디렉션 할 때도 작동했습니다. 다시 한번 감사합니다. – user3150373

+0

기꺼이 도와 드리겠습니다.이 답변을 정확하다고 표시해 주시기 바랍니다;) – Hackerman

0

왜 이러한 태그를 PHP 안에 포함해야합니까? 왜 당신은 단지 그것을 별도로하지 마십시오. 다음은 Javascript (jQuery)의 예입니다.

분 안에 스 니펫을 작성한 이후로 일부 오타 및 구문 오류가있을 수 있습니다. 이 속으로

if (($_POST['reg_username']=="") || ($_POST['reg_password']=="")) 
{ ?> 
<script> alert("please enter both email and password"); </script> 

<? } 

:이

<script type="text/javascript"> 

$(document).ready(function() { 

("#login_form").submit(function() { 

var username = ("#selector").val(); 
var password = ("#password").val(); 

if(username == "" && password == "") { 

alert("please enter both email and password"); 

} 
} 

)}; 
</script> 
0
if(isset($_POST['SubmitButtonName'])){ //check if form was submitted 
     unset($_POST['SubmitButtonName']); 
     if (($_POST['reg_username']=="") || ($_POST['reg_password']=="")) 
     { ?> 
      <script> alert("please enter both email and password"); </script> 
     <? } 
} 

기억은 SubmitButtonName에게의

0
 <script> 
    $(document).ready(function(){ 
    $("#submit").click(function(){ 
    var register_name=$('#reg_username').val(); 
var register_password=$('#reg_password').val(); 

if(register_name =="") 
{ 
alert("please enter your name"); 
return false; 
} else if(register_password =="") 
{ 
alert("please enter your password"); 
return false; 
} 
else 
{ 
return true; 
} 
     }); 
     }); 
      </script> 
     </head> 
     <body> 
      <form action="" method="post"> 
      Register name:<input type="text" name="reg_username" id="reg_username"><br> 
     Password: <input type="password" name="reg_password" id="reg_password"><br> 

      <input type="submit" id="submit"> 
     </form> 
    </html> 
+0

PHP는 서버 측이고 jquery는 클라이언트 측이므로이 목적으로 PHP 대신 Jquery 유효성 검사를 사용하십시오. –

관련 문제