简体   繁体   中英

Trouble deleting row of data from array. PHP + Javascript

I have a screen like this: notifications example

And it deletes the row, but only from the screen. Because if I refresh then it appears back again. I am not sure how to delete it from the actual array. The array is taken out of a csv file- and I know how to add it back in etc. But what I don't know is deleting rows from the array.

Heres what I have:

// Grabs the csv file (and its existing data)  and makes it into an array so the new data can be added to it.
$Notifications = array();
$lines = file('data/AdminNotifications.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
    $Notifications[$key] = str_getcsv($value);
}

echo array2table(array_reverse($Notifications));


// FUNCTION ---------------------------------------------------------------------

//This converts an array to a table
function array2table($array, $recursive = false, $null = ' ')
{
    // Sanity check
    if (empty($array) || !is_array($array)) {
        return false;
    }

    if (!isset($array[0]) || !is_array($array[0])) {
        $array = array($array);
    }

    // Start the table
    $table = "<table>\n";

    // The header
    $table .= "\t<tr>";

    // Take the keys from the first row as the headings
    foreach (array_keys($array[0]) as $heading) {

    }

    $table .= "</tr>\n";

    // The body
    foreach ($array as $row) {
        $table .= "\t<tr>" ;

        foreach ($row as $cell) {
            $table .= '<td>';

            /*
            if($cell ==0 && $heading==1){
            $cell = $cell.":  ";
        }
            */

            $details = $cell;


            // Cast objects
            if (is_object($cell)) { $cell = (array) $cell; }

            if ($recursive === true && is_array($cell) && !empty($cell)) {
                // Recursive mode
                $table .= "\n" . array2table($cell, true, true) . "\n";
            } else {
                $table .= (strlen($cell) > 0) ?
                    htmlspecialchars((string) $cell) :
                    $null;
            }

 $table .= '</td>';


        }
            $table .= '<td>';


            $table .= '<input type="submit" value="Delete" onclick="deleteRow(this)" name="delete"/>';


            $table .= '</td>';



        $table .= "</tr>\n";
    }

    $table .= '</table>';
    return $table;
}


//If the delete button is pressed, then it does this.
if (isset($_POST['delete'])) {

}

?>

//What happens when it is pressed. (This is javascript)
<script>
function deleteRow(btn) {
  var row = btn.parentNode.parentNode;
  row.parentNode.removeChild(row);

}

</script>

Any help would be greatly appreciated. I am not too sure whether I can delete a row using javascript? Or in php and java...

Thanks

Php being a server side language will execute only on server. When you are pressing delete button here a javascript function is being called which is actually deleting your row just from client side as it's a client side language that means it actually exist yet on server in that file that's why when you refresh it shows again.

Now you can make an ajax call or place that delete button inside a form to post that request to server to actually delete that.

  //If the delete button is pressed, then it does this.
if (isset($_POST['delete'])) {
    $arr_index = $_POST['delete']; //you need to pass the row number
    unset($array[$arr_index]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM