1

I have an HTML table, linked to PHP $_SESSION data, to which I wish to add a Delete button to every row that deletes not only that row from the HTML table, but also from the $_SESSION variable.

This is the code that populates my table:

tableData.php

// echo the table headings
echo <<<HERE
<tr>
    <th>CityID</th>
    <th>State</th>
    <th>City</th>
    <th></th>
</tr>
HERE;

if (isset($_SESSION['cityData']))
{

    foreach($_SESSION['cityData'] as $city)
    {
        echo "<tr>";
        // print each row of data
        foreach($city as $data)
        {
            echo "<td>" . $data . "</td>";
        }
        //echo '<td><button action="<?php unset(' . $_SESSION['cityData'][key($_SESSION['cityData'])] . ')?>">Delete Row</button></td>';
        echo "</tr>";
    }

}

The line that I commented out, echo '<td><button action="<?php unset(' . $_SESSION['cityData'][key($_SESSION['cityData'])] . ')?>">Delete Row</button></td>'; is the line that creates the button that I am trying to create, to do what I am wanting it to do. I am trying to figure out the best way to name the array that I want gone.

P.S. I know, I should have it invoke some other function that does both tasks, it is just, if I pass the array in like I did, it will complain of " Array to string conversion ". Is there a way to do what I am trying to do, cleanly?

miken32
  • 42,008
  • 16
  • 111
  • 154
Mike Warren
  • 3,796
  • 5
  • 47
  • 99
  • Oh, and I know about the implode() function, and the fact that it asks for a token parameter: http://us1.php.net/manual/en/function.implode.php – Mike Warren Oct 19 '14 at 07:04
  • This is generating a static HTML file and sending it to a browser. If you want it to do something, you need to submit a form of some kind. You can't tell a web browser to execute PHP code on your server directly from the page. – miken32 Oct 19 '14 at 07:32

2 Answers2

1

It's just not that simple. You need to get your buttons to submit to a link, and then have the PHP unset the content.

foreach($_SESSION['cityData'] as $index => $city) //added $index =>
{
    echo "<tr>";
    // print each row of data
    foreach($city as $data)
    {
        echo "<td>" . $data . "</td>";
    }
    echo '<td><form method="post" action=""><input type="hidden" name="delete" value="' . $index . '"><input type="submit" value="Delete Row"></form></td>';
    echo "</tr>";
}

So I added a form that a button that will submit to data that indicates the row number, so when your client clicks on the button, it will submit them and the row number will be passed as a POST variable.

At the top of tableData.php, you can then have logic handling the delete. Simply check if the delete is set, and then attempt to unset from there.

if (isset($_POST['delete']))
    unset($_SESSION['cityData'][$_POST['delete']]);

You will want to have further validation that checks if POST delete within the bounds of $_SESSION['cityData'], but the basic idea is there.

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
  • Is there any way to make it POST instead? – Mike Warren Oct 19 '14 at 07:24
  • Yep no problem. You will just have many small forms with buttons, and the logic stays the same (just use $_POST instead of $_GET). I have updated the code above to reflect these changes. – Dave Chen Oct 19 '14 at 07:28
  • 1
    Try the updated code, having `` within an echo statement is just bad syntax. It should work now. – Dave Chen Oct 19 '14 at 07:40
1

You're mixing client-side and server-side code the wrong way here :(

The "client" is something like a user's browser. When a user clicks that button on their browser, it will run client-side code (i.e. JavaScript) - your PHP won't exist anymore at that stage so you don't have access to that array.

PHP is executed when a page has been requested from your server. That's when you can perform whatever computation you need and then deliver a textual response (via echo for example) back to the user's browser or whatever the client may be.

That button should make another request to your server so you can use PHP to delete the row. Then your PHP server should echo a response back to the requesting browser so users can know if it worked or not.

The link on the button will need to be provided some additional details, like the index of the row that the user wants to delete, so the PHP script doesn't delete the wrong one. See Dave Chen's answer below for some example code.

Bilal Akil
  • 4,716
  • 5
  • 32
  • 52