Skip to content Skip to sidebar Skip to footer

Form Hidden Value Not Working?

I have a simple table that prints our the list of videos and plays them when you click on the row. I have a delete button to delete the corresponding video in the row. But for so

Solution 1:

Can you post the HTML? I suspect you've got multiple hidden input fields on the page, all with the same name. If that's the case, only the last one that loads on the page will register as a valid hidden input. A better way to do it would be to set a data attribute on the delete button, and then add a JS click event to that delete button. Something like this:

<button class="delete" data-video_url="youtube.com/abcd">Delete ABCD</button>
<button class="delete" data-video_url="vimeo.com/xyz">Delete XYZ</button>

and then when someone clicks on a .delete button, you send the data-video_url value to the PHP script via POST. A jQuery example might look like this:

$('.delete').click(function() {
    var url = $(this).data('video_url');
    $.post('/delete.php', { video_url: video_url }, function() {
        // Do something on success
    });
});

Another way to do it is to simply make each button its own form:

<form method="post" action="delete.php">
   <input type="hidden" name="video_url" value="url_goes_here">
   <button>Delete</button>
</form>

Hope that helps. Good luck.


Solution 2:

The problem is that the name of the hidden field is not unique (for every iteration in the loop a new hidden field with the same name is created). How should the script know which row you want if all of them has the same name?


Solution 3:

Your code does not seem to name the hidden values properly.

You should use $i to name the variable.

input type="hidden" name="video_url<?php echo $i; ?>"

then in handler you can do

<?php
if ($_POST) {
  $kv = array();
  foreach ($_POST as $key => $value) {
    // check here which one begins with video_url
  }
}
?>

Post a Comment for "Form Hidden Value Not Working?"