If statement not fully functional?

I am working on a function in my preferences page where logged in users can choose to completely remove their account from the database. I have a few else / if’ statement that checks if they click on the delete my account button.

if they do, it will bring up another form that asks if they’re sure they’d like to delete the account. For some reason when hitting the buttons that say yes / no there is no action after hitting the buttons (nothing is being echo’d as it should.

<form method="post" id="preferences-form">
  <h1 class="register-heading" style="margin-top:-5px;">Delete account</h1>
  <p class="register-heading" style="margin-top:-15px;border-bottom:1px solid #d6d6d6;"><span>Completing this form will permanently remove your account from database!</span></p>

  <p class="register-heading" style="margin-top:5px;margin-bottom:0;">confirm (delete account):</p>
  <input class="preferences half" style="margin-top:0;" type="password" name="confirm_delete_account" placeholder="current password" required/>

  <button type="submit" name="delete-account" class="delete-account half">Delete account entirely</button>
</form>

<?php
// if user hits delete account button...
if (isset($_POST['delete-account']))  {

  // check to make sure current password was entered correctly!
  if (isset($_POST['confirm_delete_account'])) {

    $posted_confirm_delete_account_password = $_POST['confirm_delete_account'];

    // if posted password matches stored password
    if (password_verify($posted_confirm_delete_account_password, $storedHash)) {
      echo '
      <form method="post">
        <p class="input-error" style="margin-bottom:0;">Are you sure you want to permanently delete your account?</p>

        <button type="submit" class="delete-account half" name="yes">yes, delete my account</button>
        <button type="submit" class="delete-account half" name="no">no, nevermind</button>
      </form>
      ';


      if (isset($_POST['yes'])) {
        echo "yes";
      }

      if (isset($_POST['no'])) {
        echo "no";
      }

    // if posted password doesn't match stored password, give user error.
    } else {
      echo '
      <style type="text/css">
        form#preferences-form>input[name=confirm_delete_account] {border:1px solid red;}
      </style>

      <p class="input-error">Current password entered incorrectly.</p>
      ';
    }
  }
}

The result:
https://puu.sh/Ab2Sw/4fbc4f559f.gif

Try adding the following to see the $_POST variables.

echo '<pre>'; // adds linefeeds
print_r($_POST);
echo '</pre>';

Like this?

if (isset($_POST['yes'])) {
  echo '<pre>'; // adds linefeeds
  print_r($_POST);
  echo '</pre>';
  echo "yes";
}

if (isset($_POST['no'])) {
  echo '<pre>'; // adds linefeeds
  print_r($_POST);
  echo '</pre>';
  echo "no";
}

Still nothing being echo’d with that. :neutral_face:

No, post the script before the first if statement.

1 Like

That responds with this.
https://puu.sh/Ab3Fx/4f1ab59a89.png

You need to do your form processes at the top of your page. If you turned on error logs, you’ll be getting a ton of errors. If this was on a live site, you’d be getting a lot of errors like crazy.

Ok so now you know what has been passed so move the script below the relevant confirmed if statement.

Beware of leading and trailing spaces.

I just turned on error_reporting(E_ALL) at the top of the page as well as moved my forms. You can view the entire page source code on the github project here.

Sorry but I am not exactly sure what you mean by the relevant confirmed if statement. I tried to put that code into the first if statement that checks if the user hit the yes, delete my account <button> but still doesn’t echo anything.

if (isset($_POST['yes'])) {
  echo "yes";
  echo '<pre>'; // adds linefeeds
  print_r($_POST);
  echo '</pre>';
}

The confirm_delete_account has been passed successfully so move the script below the relevant test if statement.

I assume that I try to put it now under the if (isset($_POST['delete-account'])) { since it doesn’t seem that was passed successfully but when doing that it still only shows me what was posted in the confirm-delete-account input.

// if user hits delete account button...
if (isset($_POST['delete-account']))  {
    
  echo '<pre>'; // adds linefeeds
  print_r($_POST);
  echo '</pre>';

It’s not supposed to because PHP doesn’t process indexes with dashes.

1 Like

So all my variables and <input name='s that have a - or _, they shouldn’t be there?!

Only dashes. Underscores works. It’s just the same thing as variables.

$var-iable

Will not work, but

$var_iable

Will work.

1 Like

Alright I fixed those variables I had that used the dashes. Changing those still did not make anything change as far as getting the if statement(s) to echo = "yes"; correctly. Not sure what it is causing the issue.

Again, you have to place all your data processes at the top of your page. This is probably why it isn’t doing anything.

As for data processes are those my forms? I have all my forms on top now (including the one that was inside the if (password_verify($posted_confirm_delete_account_password, $storedHash)) { statement.

I only want this form to display when the user submits their current password to delete their account so by default I have the form styled to display: none; and tried to display: block; it when the user submits their current password. When doing so though nothing is displayed.

I still am not getting any error(s) on my page(s) although I do have error_reporting(E_ALL); set at the top of my page. Sorry if I am making things difficult but not very good at understanding the terms of what each thing is other then what they’re actually called :confounded:

All of this needs to be at the top of your page.

I moved all of my php if / else statements above my <form>'s but now when I go to submit the delete_account button, it gives me a blank page as shown below:
https://puu.sh/Ab7jN/dcf5360b25.gif

Did you place it underneath all of the other PHP code? Because I am testing out your stuff and I don’t get that blank page. However, there are tons and tons of errors. One error pertains to using a deprecated Recaptcha library.