-1

I have the following bit of code:

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;
}
}

A few questions.

  1. How do I set $password equal to the username variable passed into the function?
  2. Why don't I get the confirm() dialog when I call the function?
2
  • 1
    I recommend you do this with AJAX. Commented Aug 6, 2011 at 3:56
  • google.com/… Commented Aug 6, 2011 at 4:03

2 Answers 2

3

You need AJAX to handle that, here's a sample using jQuery.
Put this code in one PHP file, 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';
            }
        ?>

Then modify your JS code to call ajax.php through AJAX.

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;
  }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

2

All PHP is executed before your Javascript is executed. Your script will not work as expected.

If you want to execute PHP after receiving input via Javascript from the user, you will need to use AJAX to initiate a new request to the server.

3 Comments

Is there a way to do what I'm trying to do?
Yes. Get the input from the user, and initiate an AJAX request to the server to execute your PHP script.
@Jason, yes, but it requires that you change the way this is written. For one thing, the PHP and JavaScript code have to be separate, because the PHP code is executed before the JavaScript code is even read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.