0

I have a page that shows gigs from database. First load only shows the title of each entry, and a button to show the full details, and a button to delete (not yet utilized)

I am having trouble passing on the ID value for each entry of my foreach loop. It always passes on the ID number of the last entry, no matter which one I click. So I always see the details of second gig, even if I click the first one. (I just have two gigs in the database for now)

Any suggestions how to pass on the ID of the one I click the SHOW button on?

This is my code

require_once "gigPDO.php";


//Show button is pressed

if (isset ( $_POST ["show"])) {
    $id = $_POST ["id"];
    //$id = 5;

    try {

    $dbase = new gigPDO(); 
    $result = $dbase->searchGig($id); 

    //print_r($result);

    foreach($result as $gig){
        print("<p>Title: " . $gig->getTitle());
        print ("<br>Venue: " . $gig->getVenue());
        print ("<br>Date: " . $gig->getDate());
        print ("<br>Time: " . $gig->getTime());
        print ("<br>Country: " . $gig->getCountry());
        print ("<br>Address: " . $gig->getAddress());
        print ("<br>Postcode: " . $gig->getPostcode());
        print ("<br>City: " . $gig->getCity());
        print("<br>Description: " . $gig->getDescription() . "</p>");
        echo "<br />";

        unset($_POST ["show"]);
        echo "<a href='searchGigs.php'>Back to list</a>";
        exit();                


    }
    exit ();


} catch ( Exception $error ) {
    print("<p>Error: " . $error->getMessage ());
    exit ();
}
    }

//First load, shows all gigs

try {

    $dbase = new gigPDO(); 
    $result = $dbase->allGigs();


    foreach($result as $gig){
        print("<p>". $gig->getTitle(). "<p>");
        print ("<input type='hidden' name='id' value='".$gig->getId() ."'>");
        print('<input type="submit" name="show" class="next show-button" value="Show" />');
        print(' <input type="submit" name="delete" class="next show-button" value="Delete" />');
        echo "<br />";
        echo "<br />";                
    }


    if ($dbase->getRows() == 0){
        print("There are no gigs in the database yet");

     }else{       
        print("<p>Total " . $dbase->getRows() . " gigs</p>");
       }
} catch ( Exception $error ) {
    print("<p>Error: " . $error->getMessage ());

}
  • I forgot to mention this in my answer, but you'll run into the same problem when you get to the delete button. Hopefully I explained it properly. Please let me know if I need to clarify anything. – Don't Panic May 05 '16 at 15:54
  • This Q&A discusses the effect of having multiple inputs with the same name: http://stackoverflow.com/questions/452066/html-form-with-multiple-hidden-control-elements-of-the-same-name – Don't Panic May 05 '16 at 15:58
  • I'm not sure how your foreach loop is supposed to work, since you are exiting it after the first iteration already - you have `exit()` without condition inside it! – CherryDT May 05 '16 at 17:23

1 Answers1

0

If you use a button instead of an input type="submit", then you can imbed the id into the button instead of using the hidden input.

echo '<button type="submit" name="id" class="next show-button" value="'.$gig->getId().'" />
         Show
      </button>';

The way you're doing it currently could work, but you would need a separate form tag around each set of inputs. Currently, when you submit your form, all of those hidden inputs are submitted, and since they have the same name, you get the value of the last one. Doing it the way I suggested, only the value of the button you clicked will be submitted.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • Thanks for the reply. I am trying to get it working like this, but something is not quite right. Can't quite see what it wrong, but putting the line as it is in my PHPdesigner, it marks it red, and I can't quite find out where the problem is. I tried editing it a bit, it seemed to work better, but I could not seem to get the id transfered to the top part where I want it to show only this one gig. I guess the info transferred from "button" works differently? – Boogiewoogie May 05 '16 at 16:05
  • Sorry about that! I just fixed a typo. The info transferred from a button should work the same. You should be able to get it from `$_POST['id']`. But if you're using my button suggestion, you'll no longer have the input named "show", so you'll need to change the `if (isset ( $_POST ["show"])) {` part to `if (isset ( $_POST ["id"])) {`. – Don't Panic May 05 '16 at 16:18
  • Wonderful! That fixed it. Just getting to know PHP a bit better and PDO and all that. It can be a bit tricky to get a hang of it. But cheers to you!!! – Boogiewoogie May 05 '16 at 16:28
  • I'm glad you got it working! I'm happy to help. Please feel free to [accept the answer](http://stackoverflow.com/help/someone-answers) if it solved your problem. – Don't Panic May 05 '16 at 16:36