How to use delete query (delete user if html class with name="submit" attribute is selected)

User has logged in, has filled out a survey, then goes to another screen that asks them to tick a box if it is their last survey. The button text changes based on whether the tickbox if ticked or empty.

If the user ticks the box, meaning this is the last survey, I want it to delete that user so they can’t log back in using their user and password data - it also logs them out as they click the button to save and logout. The opposite behaviour is just to logout as normal.

public function last()

    $usersData = $this->loadModel('Users');
    $auth = $this->Authentication->getResult()->getData();
    $deleteUserLoggedIn = $this->Users->find()->where(['Users.code' => $auth->get('code')]);

    if(isset($_POST['submit'])) {
        if(!empty('name')) {

            // delete query here
            $deleteUser = $this->Users->delete($deleteUserLoggedIn, ['Users' => 'code']);
            return $this->redirect(['controller' => 'Users', 'action' => 'logout']);

        } else {

            // if not clicked - logout as normal
            $this->Flash->success(__('All questions answered, thank you for your participation.'));
            return $this->redirect(['controller' => 'Users', 'action' => 'logout']);



First, you should never directly access $_POST or other superglobals. Look into $this->getRequest()->getData() (may not be the exact right name; you haven’t specified which version of CakePHP you’re using, and it has changed over time).

Second, you didn’t show what your form looks like; the way your buttons are set up will have an impact on how to check the data.

Third, the string “name” will never be empty, so it’s always going to take the first path in your if, never the else.

Four, you haven’t said what system you’re using for authentication. There’s probably a more direct option than redirecting them to the logout page, but it will again depend on some specifics.

And finally, you haven’t ever actually said what’s not working about your system.

  1. Using CakePHP latest version, to my knowledge (strawberry)

  2. Template file for the form this page is on:

<div class="col-md-6 offset-md-3">
    <p><?=__('You have successfully completed week '.$week.'\'s questions.')?></p>
    <p><?=__('If this is your last session, tick the box and press "SAVE FINAL SESSION AND END SURVEY" to end your participation. Otherwise, just press the "SAVE LATEST RESULTS AND LOG OUT" button.')?></p>
    <?= $this->Form->create(null); ?>
        <div class="theend">
            <div class="tick">
				<?= $this->Form->control('active',['label'=>__(''),'class'=>'bigtick','id'=>'toggletick','checked'=>false,'value'=>0,'hiddenField'=>1, 'name'=>'lastsession[]']); ?>
            <div class="copy">
            	<p><?=__('Is this your last session? Tick this box to end your participation in the programme.')?></p>
            	<p><span><?=__('You won\'t be able to log in again, so do not tick if you are still due to report more results!')?></span></p>
    <div class="formend toggle-off" id="button1">
    	<p><?= $this->Form->button(__('Save Final Session and End Survey'),['class'=>'continue large', 'name'=>'submit']); ?></p>
    <div class="formend toggle-on" id="button2">
    	<p><?= $this->Html->link(__('Save Latest Results and Log Out'),['controller'=>'Users','action'=>'logout'],['class'=>'linkbutton large']); ?></p>
    <?= $this->Form->end(); ?>


document.addEventListener('DOMContentLoaded',() => {
	var trigger=document.getElementById('toggletick');
	var formendon=document.getElementById('button1');
	var formendoff=document.getElementById('button2');
	trigger.addEventListener('click',() => {

/*$('toggletick').click(function() {
  1. Would a better option be to refer to a class or id or something else instead rather than if(!empty) being there?

  2. As I’ve been asked to look at this at work, they have asked this stays the same. Essentially, log them out or log them out then delete their id/username etc. - its a patient and survey type system where they’ll only get left on the system if they still have more weekly surveys left

Not sure as to the system for authentication, specifically

  1. What it should do is log you out, but then deem the username and password is incorrect; meaning the username/id has been removed from the database table in MySQL
  1. “Strawberry” is just anything in the Cake 4 line, I believe. You can always find your current version from cat vendor/cakephp/cakephp/VERSION.txt or bin/cake version.

  2. Your PHP code (which you posted initially) can’t refer to classes or IDs, those are parts of the DOM, which exists in the browser, not in PHP. What you want is to check things like the request type and data posted to it.

  3. I understand that they want the user to be logged out after doing this. My question about that is whether they demand, for some obscure reason, that it happens via a redirect to the logout function, or if it’s okay for some other action to have a side effect of logging the user out?

4b. You still haven’t said what you are using for authentication. The old Auth component? The new Authentication plugin? Some third party plugin?