0

Here is my code:

for($i = 0; $i < $printcoll->getlineCount(); $i++){
    $li   = $printcoll->getLineItem($i);
    $item = $li->getItem();
    if($item instanceof Product){
        print "Bike ID - ";
    }
    print $item->getId();


    if($item instanceof Product){
        print "&nbsp Price &pound";
    }
    print $item->getPrice();

    if($item instanceof Product){
        print "&nbsp Quantity - ";
    }

    print $li->getQuantity();





    print "<a href='myOO.php?delete=" . $i . "'>Delete</a>";
    echo "</br>";
}

if(isset($_GET['delete'])){
    $id = $_GET['delete'];
    $li = $printcoll->getLineItem($id);
    $printcoll->delLineItem($li);
}

This code deletes the object the user specifies (by clicking the a href). However, after clicking delete, the page does not go back to "myOO.php". It stays as "myOO.php?delete=".$i." and so i have to manually delete the "?delete=".$i." bit out of the address bar for the update to appear on the page. Any ideas?

One solution is putting ob_start(); at the start of the page, and then putting header('location:myOO.php') in the if statement. However im not sure this is the correct way of doing this. Any ideas? Thanks

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • yes, PHP is a server-sided language, which means that if you want something to happen on the front-end (browser), you will need to use redirects, or javascript to update what the user sees. – kennypu Jan 01 '15 at 23:53
  • Put the if-block from the bottom to the top, then when a user deletes a item, it will be deleted before the page gets rendered and sent to the user. Also putting the delete button in a form element and using POST instead of GET would solve the url issue... AND is the prefered way when making a *change*. Check this for a little more on that issue http://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them :) – Richard87 Jan 02 '15 at 00:01

1 Answers1

0

Ask kennypu already pointed out, PHP code is executed on the webserver.

This means that everytime you change the document on the server, the page must be reloaded on the client manually or automatically ( e.g. by using AJAX ) in order to re-execute the PHP code on the server.

Tacticus
  • 561
  • 11
  • 24
  • Can you point me in the right direction as to the ajax code used please? –  Jan 02 '15 at 00:04
  • Sorry, I misunderstood the question. It would be easiest to redirect to the send-page using a meta refresh, or the PHP header() function. AJAX is too "overpowered" and complicated for this. It was intended for dynamically adding content to the page. Fore more information about the header() approach, see [link](http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) – Tacticus Jan 02 '15 at 00:23