Home>
I want to delete

id to php, delete the data that is equal to that id and display it on the js side, but the data is always displayed for some reason despite being deleted from the txt file. Don't know why?

<? php
$json = file_get_contents ("php: // input");
$json_data = json_decode ($json, true);
$ID = $json_data ['userid'];
$filearr = file ('product.txt');
 $newLines = array ();
 foreach ($filearr as $no =>$val) {
     $cols = explode (",", $val);
     $keyval = explode ("=", $cols [0]);
     if ($keyval [1] == $ID) {
         continue;
     } else {
         $newLines [] = $val;
     }
 }
 $fp = fopen ("product.txt", "w");
 fwrite ($fp, implode ("\ r", $newLines));
 fclose ($fp);
$fp = fopen ("product.txt", "r");
while (($data = fgetcsv ($fp, 1024))! == false) {
  $tmp =&$datas [];
  foreach ($data as $x) {
    $y = explode ("=", $x);
    $tmp [trim ($y [0])] = trim ($y [1]);
  }
}
unset ($tmp);
fclose ($fp);
$data = json_encode ($datas);
echo $data;
  • Answer # 1

    The question code was edited before it was added
    You may be looking at the cache with ajax requests. Check the web server access log to see if a request is coming.
    Also, if you are looking at the cache, try to disable the cache when making a request with ajax.
    cache: false

    By the way, the php side worked without problems with the code described in the question.
    * To nikuniku12. Please check because the answer to the question (https://StackOverflow.com/questions/225750) of the previous questioner (https://StackOverflow.com/users/dog50) has been corrected.

    $cat -n product.txt
         1 id = 1, Product = Pen
         2 id = 2, product = chair
         3 id = 3, Product = Eraser
    $# Delete first run ID = 2
    $php
    $val) {
        $cols = explode (",", $val);
        $keyval = explode ("=", $cols [0]);
        if ($keyval [1] == $ID) {
            continue;
        } else {
            $newLines [] = $val;
        }
    }
    $fp = fopen ("product.txt", "w");
    fwrite ($fp, implode ("\ r", $newLines));
    fclose ($fp);
    $fp = fopen ("product.txt", "r");
    while (($data = fgetcsv ($fp, 1024))! == false) {
      $tmp =&$datas [];
      foreach ($data as $x) {
        $y = explode ("=", $x);
        $tmp [trim ($y [0])] = trim ($y [1]);
      }
    }
    unset ($tmp);
    fclose ($fp);
    $data = json_encode ($datas);
    echo $data;
    [{"id": "1", "\ u5546 \ u54c1": "\ u30da \ u30f3"}, {"id": "3", "\ u5546 \ u54c1": "\ u6d88 \ u3057 \ u30b4 \ u30e0 "}]
    $# Delete the second execution ID = 1
    $php
    $val) {
        $cols = explode (",", $val);
        $keyval = explode ("=", $cols [0]);
        if ($keyval [1] == $ID) {
            continue;
        } else {
            $newLines [] = $val;
        }
    }
    $fp = fopen ("product.txt", "w");
    fwrite ($fp, implode ("\ r", $newLines));
    fclose ($fp);
    $fp = fopen ("product.txt", "r");
    while (($data = fgetcsv ($fp, 1024))! == false) {
      $tmp =&$datas [];
      foreach ($data as $x) {
        $y = explode ("=", $x);
        $tmp [trim ($y [0])] = trim ($y [1]);
      }
    }
    unset ($tmp);
    fclose ($fp);
    $data = json_encode ($datas);
    echo $data;
    [{"id": "3", "\ u5546 \ u54c1": "\ u6d88 \ u3057 \ u30b4 \ u30e0"}]
    $# Result of deleting ID = 2 and ID = 1
    $cat -n product.txt
         1 id = 3, Product = Eraser

    Because you are reading fromproduct.txtfirst?
    product.txtis not rewritten in the source of the question.

    $filearr = file ('product.txt');
    // abbreviated
    $fp = fopen ("json12.txt", "w");
    // abbreviated
    $fp = fopen ("json12.txt", "r");
    // abbreviated