2011-08-06 69 views
0

我有下面的代碼位:PHP裏面的Javascript?

function deletet(username){ 
if(confirm("Do you REALLY want to delete your account?")){ 
    if(confirm("Are you POSITIVE?")){ 
     var check = prompt("Enter your password",""); 
     if(check){ 
      <?php 
       require('functions.php'); 
       mysqlLogin(); 
       $password = 
       $username = $_COOKIE['sqlusername']; 
       $queyreg = ("SELECT * FROM `users` WHERE username='$username'"); 
       $row = mysql_fetch_array($queryreg,MYSQL_ASSOC); 
       $hash = hash('sha256', $row['salt'] . hash('sha256', $password)); 
       if($hash == $row['password']){ 
        $sql = mysql_query("DELETE FROM `users` WHERE username='$username' AND password='$hash'"); 
        if($sql){ 
         ?> alert("Account deleted, thank you"); window.location='login.php'; <?php 
        } else { 
         ?> alert("There was an error deleting your account"); return false; <?php 
        } 
       } else { 
       ?> alert("Passwords don't match!"); return false; <?php 
       } 
      ?> 
      return true; 
     } else { 
      alert("Please enter your password!"); 
      return false; 
     } 
    } else { 
     return false; 
    } 
} else { 
    return false; 
} 
} 

的幾個問題。

  1. 我該如何設置$password等於username變量傳入函數?
  2. 爲什麼我在調用函數時沒有得到confirm()對話框?
+1

我建議你使用AJAX來做到這一點。 –

+0

http://www.google.com/search?rlz=1C1CHKZ_enUS436US436&sourceid=chrome&ie=UTF-8&q=ajax – David

回答

2

在執行Javascript之前執行所有PHP。您的腳本將無法按預期工作。

如果你想從用戶通過Javascript接收輸入後執行PHP,你需要使用AJAX來啓動到服務器的新請求。

+0

有沒有辦法做我想做的事? – Jason

+0

是的。從用戶處獲得輸入,並向服務器發起一個AJAX請求以執行您的PHP腳本。 –

+0

@Jason,是的,但它需要你改變這種寫法。首先,PHP和JavaScript代碼必須是分開的,因爲PHP代碼在JavaScript代碼被讀取之前執行。 –

3

你需要AJAX來處理,這裏是一個使用jQuery的示例。
將這個代碼在一個PHP文件,ajax.php

 <?php 
      require('functions.php'); 
      mysqlLogin(); 
      $password = $_GET['password']; 
      $username = $_COOKIE['sqlusername']; 
      $queyreg = ("SELECT * FROM `users` WHERE username='$username'"); 
      $row = mysql_fetch_array($queryreg,MYSQL_ASSOC); 
      $hash = hash('sha256', $row['salt'] . hash('sha256', $password)); 
      if($hash == $row['password']){ 
       $sql = mysql_query("DELETE FROM `users` WHERE username='$username' AND password='$hash'"); 
       if($sql){ 
        echo 'deleted'; 
       } else { 
        echo 'error'; 
       } 
      } else { 
      echo 'false'; 
      } 
     ?> 

然後修改您的JS代碼,通過AJAX調用ajax.php。

function deletet(username){ 
    if(confirm("Do you REALLY want to delete your account?")){ 
     if(confirm("Are you POSITIVE?")){ 
      var check = prompt("Enter your password",""); 
      if(check){ 
       jQuery.ajax({ 
       url: 'ajax.php?password='+username, 
       type: 'GET', 
       data: '', 
       success: function(data) { 
        if (data == 'deleted') { 
        alert("Account deleted, thank you"); window.location='login.php'; 
        } 
        else if (data == 'error'){ 
        alert("There was an error deleting your account"); return false; 
        } 
        else if (data == 'false'){ 
        alert("Passwords don't match!"); return false; 
        } 
       } 
       }); 
       return true; 
      } else { 
       alert("Please enter your password!"); 
       return false; 
      } 
     } else { 
      return false; 
     } 
    } else { 
     return false; 
    } 
} 

希望這會有所幫助。