0

My site's homepage displays all the details of the movies i have in an xml file(performed with a foreach PHP loop,and DOM),i want to add a delete button now for each movie,so that should be done within the loop,the problem is i know how to delete the xml node,given the movie title,but,i can't find a way to transfer the data(movie title) to the page where i could confirm the deletion,i tried some solutions that didnt work:

-I tried a <form method="post" action="foobar"> within the PHP loop,with an <input type="hidden"name="deletion" value="php code giving the movie title"> but as you can see it gives the same name each time the loop runs again,so it obviously will not work.

-Thought about using buttons instead of forms within loop,with onClick event funstions,but i don't want to include js in this at all

-I looked for solutions on forums,i found cookies,sessions,array names...i don't want to use those either,because they aren't effective,since i can't determine which movie title was transfered(because i don't even know the name of the variable that contains it)

Is there any other solution for this problem?would be great PS:i am coding by hand,a friend already told me,that if i was using a framework,it would have been very easy,but i want to learn to code by hand first,then framework.

I can of course provide with code if necessary.

 foreach($Movies as $Movie) { ?> <div class="iodata"> <?php echo "Movie : {$Movie->Title}<br /><br />"; echo "Genre : {$Movie->Genre}<br /><br />"; echo "Year : {$Movie->Year}<br /><br />"; echo "Actors : {$Movie->Cast}<br /><br />"; echo "Director : {$Movie->Director}<br /><br>"; echo "Synopsis : {$Movie->Synopsis}<br /><br />";?> <form method="post" action="confirm.php"> <input type="hidden" name="deletion" value="<?php echo "$Movie->Title";?>"> <input type="submit" value="delete this"> </form> </div><br> <?php }}?> 
6
  • Can you add the code for your first attempt, with the for-loop and the <form method="post">? That should work just fine - it's probably something small. Commented Feb 9, 2016 at 18:28
  • @Kenney it can't be,by logic,because all the forms have the same input name,and in the other page where the action goes,i tried to echo the $_POST['deletion'] it says,deletion undefined index. Commented Feb 9, 2016 at 18:31
  • Thanks. You'll need <form method='post' action='confirm.php'> - the default is get, which puts deletion in $_GET. You could also use $_REQUEST, and then it doesn't matter if it's POST or GET. Commented Feb 9, 2016 at 18:37
  • @kenny that actually was a test page code,in the main one i have put the method="post" which is why i mentionned it didnt work,my bad i will edit the post,but still doesnt solve anything though. Commented Feb 9, 2016 at 18:40
  • Ok, now, if you put this in your confirm.php, you'll see that deletion has a different value depending on which submit button you click: echo "<pre>" . print_r( $_POST, 1 ) . "</pre>"; Commented Feb 9, 2016 at 18:42

1 Answer 1

1

Perhaps not an answer, but this doesn't fit in the comments.

Try these files, and see if they work for you:

movies.php

<?php $Movies = [ (object) [ 'Title' => 'Foo', 'Genre' => 'Action' ], (object) [ 'Title' => 'Bar', 'Genre' => 'Drama' ], ]; foreach($Movies as $Movie) { ?> <div class="iodata"> <?php echo "Movie : {$Movie->Title}<br /><br />"; echo "Genre : {$Movie->Genre}<br /><br />"; echo "Year : {$Movie->Year}<br /><br />"; echo "Actors : {$Movie->Cast}<br /><br />"; echo "Director : {$Movie->Director}<br /><br>"; echo "Synopsis : {$Movie->Synopsis}<br /><br />";?> <form method="post" action="confirm.php"> <input type="hidden" name="deletion" value="<?php echo "$Movie->Title";?>"> <input type="submit" value="delete this"> </form> </div><br> <?php } 

confirm.php

<?php echo "<pre>" . print_r( $_REQUEST, 1 ) . "</pre>"; 

If you click the first button, you should see

Array ( [deletion] => Foo ) 
Sign up to request clarification or add additional context in comments.

2 Comments

they actually doesn't work either,the data is not being sent to the confirm page,at all,that is the problem
So, you pasted the first snipped in a file called 'movies.php', the second snippet in a file called 'confirm.php', then requested the first file through your webserver, clicked a button, and nothing happens? I tested it: see here - these are the exact same files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.