Home>
Premise, what I want to achieve

I'm creating a bulletin board in PHP practice, but before deleting a comment,'Can I really delete it? I want to display an alert.
As the current function
・ Enter the password when entering comments
・ To delete or edit a comment, enter the password you decided when you posted the comment.
・ Click "Delete comment" to open a form for entering the comment number I want to delete and its password.
・ Click "Edit comment" to open the form for entering the comment number I want to edit, and enter the number to reflect the contents of the selected number in the name and comment input form.
-Display the number, name, comment, comment date and time below the input form
-Save the entered contents in a text file (<>saves the delimiter and password together)
there is.

problem

I tried to make a code so that an alert will appear when I press the delete button, but the alert screen does not appear.
No error message is coming out.

Also, in the code before adding the password function, the alert at the time of deletion was displayed, but after adding the password function, the alert no longer appears.
I would appreciate it if anyone could tell me the cause.

Corresponding source code
<? php
// Save the commented content to a text file
date_default_timezone_set ('Japan');
$name ='';
$comment ='';
$password ='';
$array = [];
$filepath ='keijiban.txt';
if (file_exists ($filepath)) {
  $filedata = file ($filepath);
  // Convert text data to array
  foreach ($filedata as $row) {
    $rowdata = explode ('<>', $row);
    $array [] = $rowdata;
  }
}
// When "Post" or "Edit" is pressed
if (isset ($_ POST ['send'])) {
  $name = htmlspecialchars ($_POST ['name']);
  $comment = htmlspecialchars ($_POST ['comment']);
  $password = $_POST ['password'];
  $date = date ('Y/m/d H: i: s');// Define variables
  if (($name =='') or ($comment =='')) {
    echo'There are unfilled items';
  } elseif (preg_match ('/ (? =. * [Az]) (? =. * [0-9]) [a-zA-Z0-9] {6,} /', $password)) {// Check password conditions
    if (file_exists ($filepath)) {
      $num = count ($filedata) +1;
    } else {
      $num = 1;
    }
    $writedata = ($_POST ['edit_post']?: $num).'<>'. $Name.'<>'. $Comment.'<>'. $Date.'<>'. $Password. " \ n ";
    if ($_ POST ['edit_post']) {// When "Edit" is pressed
      if ($password! = trim ($array [$_ POST ['edit_post']-1] [4])) {// Match password
        echo "The password is incorrect";
      } else {
        foreach ($filedata as&$row) {
          $rowdata = explode ('<>', $row);
          if ($rowdata [0] == $_POST ['edit_post']) {
            $row = $writedata;
          }
        }
      }
    } else {// When "Post" is pressed
      $filedata [] = $writedata;
    }
    file_put_contents ($filepath, $filedata);
  } else {
    echo'Not a proper password';
  }
}
?><script type = "text/javascript">// Function to issue an alert
  function delete_check () {
    if (! window.confirm ('Are you sure I want to delete it?')) {
      return false;
    }
    document.submit ();
  }</script><? php
// Delete comment
$del_num ='';
if (isset ($_POST ['delsend_2'])) {
  $del_num = htmlspecialchars (mb_convert_kana ($_POST ['delete'],

'n'));
  $password = $_POST ['password'];
  if ($del_num =='') {
      echo "No number specified";
  } else {
    if (($del_num>count ($filedata)) or ($del_num<= 0)) {echo "The comment number does not exist";
    } elseif ($password! = trim ($array [$del_num-1] [4])) {
      echo "The password is incorrect";
    } else {
      $fp = fopen ($filepath,'w');
      for ($i = 0;$i<count ($filedata);$i ++) {
        $delcom = explode ('<>', $filedata [$i]);
        if ($delcom [0]! = $del_num) {
          fwrite ($fp, $filedata [$i]);
        } else {
          fwrite ($fp, "deleted<>N \ n");
        }
      }
      fclose ($fp);
    }
  }
}
?><? php
// Edit comment
$edit_num ='';
$edit_name ='';
$edit_comment ='';
if (isset ($_ POST ['editsend_2'])) {
  $edit_num = htmlspecialchars (mb_convert_kana ($_POST ['edit'],

'n'));
  if ($edit_num =='') {
      echo'No number specified';
  } else {
    if (($edit_num>count ($filedata)) or ($edit_num<= 0)) {
      echo'The comment number does not exist';
    } else {
      foreach ($filedata as $row) {
        $edit_data = explode ('<>', $row);
        if ($edit_data [0] == $edit_num) {
          $edit_name = $edit_data [1];
          $edit_comment = $edit_data [2];
          break;
        }
      }
    }
  }
}
?><! DOCTYPE html><html><head><meta charset ='utf-8'><title>Bulletin board</title></head><body><form action ='' method ='post'><input type ='hidden' name ='edit_post' value ='<? php echo $edit_num;?>'>  Name<br><input type ='text' name ='name' value ='<? php echo $edit_name;?>'><br>  Comment<br><textarea name ='comment' rows = '5' cols = '35'><? php echo $edit_comment;?></textarea><br><? php
      if ($edit_num =='') {
        echo "Please decide and enter the password<br />";
        echo "(6 or more characters, including 1 or more half-width alphabets and 1 or more half-width numbers)<br />";
        echo "<input type ='password' name ='password'><br />";
        echo "<input type ='submit' name ='send' value ='post'><br /><br />";
      } else {
        echo "Enter your password<br />";
        echo "<input type ='password' name ='password'><br />";
        echo "<input type ='submit' name ='send' value ='edit'><br /><br />";
      }
    ?></from><? php // Input form for deletion
      if (isset ($_ POST ['delsend_1'])) {// Display when "Delete comment" is pressed
        echo "<form action ='' method ='post' on submit ='return delete_check ()'>";
        echo "Delete target number<br />";
        echo "<input type ='text' name ='delete' size = '5'><br />";
        echo "Enter your password<br />";
        echo "<input type ='password' name ='password'><br />";
        echo "<input type ='submit' name ='delsend_2' value ='delete'></form>";
      } else {
        echo "<form action ='' method ='post'>";
        echo "<input type ='submit' name ='delsend_1' value ='delete comment'></form>";
      }
    ?><form action ='' method ='post'><? php // Input form for editing
      if (isset ($_ POST ['editsend_1'])) {// Display when "Edit comment" is pressedecho "Edit target number<br />";
        echo "<input type ='text' name ='edit' size = '5'><br />";
        echo "<input type ='submit' name ='editsend_2' value ='edit'>";
      } else {
        echo "<input type ='submit' name ='editsend_1' value ='edit comment'><br />";
      }
    ?></form> Comment section   <? php
    if (file_exists ($filepath)) {
      $filedata = file ($filepath);
      foreach ($filedata as $value) {
        $com = explode ('<>', $value);
        for ($i = 0;$i<count ($com) -1;$i ++) {// -1 to hide the password
          echo $com [$i],

''';
        }
        echo "<br />";
      }
    }
    ?></body></html>


Code before adding password function

<? php
// Save the commented content to a text file
date_default_timezone_set ('Japan');
$name ='';
$comment ='';
$filepath ='keijiban.txt';
if (file_exists ($filepath)) {
  $filedata = file ($filepath);
}
if (isset ($_ POST ['send'])) {
  $name = htmlspecialchars ($_POST ['name']);
  $comment = htmlspecialchars ($_POST ['comment']);
  $date = date ('Y/m/d H: i: s');
  if (($name =='') or ($comment =='')) {
      echo'There are unfilled items';
  } else {
    if (file_exists ($filepath)) {
      $num = count ($filedata) +1;
    } else {
      $num = 1;
    }
    $writedata = ($_POST ['edit_post']?: $num).'<>'. $Name.'<>'. $Comment.'<>'. $Date. "\ n";
    if ($_ POST ['edit_post']) {
      foreach ($filedata as&$row) {
        $rowdata = explode ('<>', $row);
        if ($rowdata [0] == $_POST ['edit_post']) {
          $row = $writedata;
        }
      }
    } else {
      $filedata [] = $writedata;
    }
    file_put_contents ($filepath, $filedata);
  }
}
?><script type = "text/javascript">function delete_check () {
    if (! window.confirm ('Delete?')) {
      return false;
    }
    document.submit ();
  }</script><? php
// Delete comment
$del_num ='';
if (isset ($_ POST ['delsend'])) {
  $del_num = htmlspecialchars (mb_convert_kana ($_POST ['delete'],

'n'));
  if ($del_num =='') {
      echo'No number specified';
  } else {
    if (($del_num<= count ($filedata)) and ($del_num>0)) {
      $fp = fopen ($filepath,'w');
      for ($i = 0;$i<count ($filedata);$i ++) {
        $delcom = explode ('<>', $filedata [$i]);
        if ($delcom [0]! = $del_num) {
          fwrite ($fp, $filedata [$i]);} else {
          fwrite ($fp, "deleted \ n");
        }
      }
      fclose ($fp);
    } else {
      echo'The comment number does not exist';
    }
  }
}
?><? php
// Edit comment
$edit_num ='';
$edit_name ='';
$edit_comment ='';
if (isset ($_ POST ['editsend'])) {
  $edit_num = htmlspecialchars (mb_convert_kana ($_POST ['edit'],

'n'));
  if ($edit_num =='') {
      echo'No number specified';
  } else {
    if (($edit_num<= count ($filedata)) and ($edit_num>0)) {
      foreach ($filedata as $row) {
        $edit_data = explode ('<>', $row);
        if ($edit_data [0] == $edit_num) {
          $edit_name = $edit_data [1];
          $edit_comment = $edit_data [2];
          break;
        }
      }
    } else {
      echo'The comment number does not exist';
    }
  }
}
?><! DOCTYPE html><html><head><meta charset ='utf-8'><title>Bulletin board</title></head><body><form action ='' method ='post'><input type ='hidden' name ='edit_post' value ='<? php echo $edit_num;?>'>  Name<br><input type ='text' name ='name' value ='<? php echo $edit_name;?>'><br>  Comment<br><textarea name ='comment'><? php echo $edit_comment;?></textarea><br><? php
      if ($edit_num =='') {
        echo "<input type ='submit' name ='send' value ='post'><br /><br />";
      } else {
        echo "<input type ='submit' name ='send' value ='edit'><br /><br />";
      }
    ?></form><form action ='' method ='post' onsubmit = "return delete_check ()">  Deletion target number<br><input type ='text' name ='delete' size = '5'><br><input type ='submit' name ='delsend' value ='delete'></form><form action ='' method ='post'>  Edit target number<br><input type ='text' name ='edit' size = '5'><br><input type ='submit' name ='editsend' value ='edit'></form> Comment section   <? php
    if (file_exists ($filepath)) {
      $filedata = file ($filepath);
      foreach ($filedata as $value) {
        $com = explode ('<>', $value);
        foreach ($com as $c) {
          echo $c,'';
        }
        echo "<br>";
      }
    }
    ?></body></html>