phreak3r Posted August 24, 2020 Share Posted August 24, 2020 I am trying to delete some videos. The videos are echo'd out individually with their own 'delete' input. The input value takes the id of the video, I didn't know of any other work around for that. I'd like to delete the video that corresponds with the button pressed, if that makes sense. Anyways, I would like to delete the video where the video id is equal to the video id of the input or the value of the input pressed. I am positive the way I am carrying this out is not correct, as I am thrown an 'index undefined error'. Here is the disgusting code at hand. Recommendations are appreciated, but please try and keep answers in line with the relevant information given. Thank You. <?php error_reporting(-1); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); require('dbcon/dbcon.php'); include('header.php'); include('user.php'); $channel_id = $_SESSION['channel_id']; $query = $pdo->prepare("SELECT * FROM videos001 WHERE uploader = :channel_id"); $query->bindValue('channel_id', $_SESSION['channel_id']); $query->execute(); while ($row = $query->fetch(PDO::FETCH_ASSOC)) { $title = $row['video_title']; $video_path = $row['video_path']; $video_id = $row['video_id']; /* - get videos of user - display videos out onto page */ ?> <html> <body> <div class="content"> <form method="post"> <h3><?php echo $title; ?></h3> <input type="button" name="delete" id="delete" value="<?php echo $video_id;?>" onclick="deleteVideo()"> </form> </div> </body> </html> <?php } function removeVideoFromFilesystem(PDO $pdo, $video_path, $video_id) { //chdir($video_path); //unlink($video_id . ".mp3"); $query = $pdo->prepare("DELETE from videos001 where video_id = :video_id"); $query->bindValue(':video_id', $_POST['delete']); $query->execute(); } if (isset($_POST['call_func'])) { removeVideoFromFileSystem($pdo, $video_path, $_POST['delete']); } ?> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> function deleteVideo() { $.ajax({ url: 'dashboard.php', type: 'post', data: {"call_func":"1"}, success: function(response) { console.log(response); } }); } </script> </head> <body><?php print_r($_POST['delete']); ?></body> </html> Link to comment https://forums.phpfreaks.com/topic/311374-undefined-index-error/ Share on other sites More sharing options...
requinix Posted August 24, 2020 Share Posted August 24, 2020 Sorry but I'm going to have to start with the non-relevant stuff first. 1. Exactly how many <html> tags do you think are appropriate for a webpage? Spoiler: the answer is exactly one. Refactor your code so you have less stuff in the loop. 2. Don't use IDs for HTML elements unless you actually need it. Form fields do not need them in order to work. Just names. 3. If you need a PHP function (you don't but it's not a particularly heinous crime) then don't mix it into the HTML output. Keep it somewhere easily accessible and far out of the way. Such as at the beginning of the script. Slightly more relevant, 4. Nobody uses inline event handlers (eg, onclick) anymore. That's so 1990s. If you're using jQuery then you might as well use it for its strengths - such as event handling. Find a way to set up a single event handler that will automatically apply to all of the buttons. 5. If you're using AJAX then don't bother with the <form>. It doesn't mean anything. And actually relevant, 6a. AJAX only sends the data you tell it to send. And right now, all you're telling it to send is that "call_func" thing. 6b. Your click event handler (the one you're going to write as you address problem #4) will know which button was pressed. All you have to do is get the button's value and add it to the AJAX request data. Link to comment https://forums.phpfreaks.com/topic/311374-undefined-index-error/#findComment-1580914 Share on other sites More sharing options...
phreak3r Posted August 24, 2020 Author Share Posted August 24, 2020 @requinix I'm going to use as many tags as I need to. When I figure out a way to not have to use more than one I will eliminate one of the two. I didn't know inline event handlers were already outdated, I don't follow the corporation or industry standards. I guess I'll just figure it out. Thanks for the response though. Link to comment https://forums.phpfreaks.com/topic/311374-undefined-index-error/#findComment-1580915 Share on other sites More sharing options...
requinix Posted August 24, 2020 Share Posted August 24, 2020 Ah, there's no feeling in the world quite the same as receiving a reply that says "thank you for the advice on what I should be doing right but I really don't give a damn". 1 Link to comment https://forums.phpfreaks.com/topic/311374-undefined-index-error/#findComment-1580919 Share on other sites More sharing options...
Recommended Posts