2014-01-30 77 views
0

我有一個簡單的html表單,它接受用戶名和密碼並將其註冊到數據庫中。但是,即使在提交表單之前,頁面加載後,它也會立即執行以下php if語句。以下是這段代碼。javascript在表單提交前執行

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:爲什麼我的Javascript中的PHP(或其他服務器端)代碼無法工作?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side -code-in-my-javascript-not-wor) – Quentin

回答

0

更改此:

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

<? } 

進入這個:

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

感謝Robert,它正在工作。當我用我的初始代碼創建一個新的php文件並將表單值重定向到該文件時,它也起作用。再次感謝。 – user3150373

+0

我很高興幫助,隨時標記這個答案是正確的;) – Hackerman

0

爲什麼你必須在PHP中包含這些標籤?爲什麼你只是不單獨做。下面是一個使用Javascript(jQuery)的示例:

請注意,自從我在一分鐘內寫入該片段後,可能會出現一些拼寫錯誤和語法錯誤。

<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

請使用jquery驗證,而不是使用php爲此目的,因爲php是服務器端和jquery是客戶端 –