aveeva Posted July 16, 2019 Author Share Posted July 16, 2019 (edited) @Barand For my clarification, this line -> "<td><button class='delbtn' data-id='"+ k +"'>Delete</button></td>" Edited July 16, 2019 by aveeva Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568421 Share on other sites More sharing options...
Barand Posted July 16, 2019 Share Posted July 16, 2019 20 hours ago, Barand said: list = list + "<td><button class='delbtn' data-id='"+ k +"'>Delete</button></td></tr>\n" // add "Delete" button to each playlist item Was the comment on that line not a sufficient clue? Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568422 Share on other sites More sharing options...
aveeva Posted July 16, 2019 Author Share Posted July 16, 2019 @Barand That line okay, i got it, no confusion. My confusion about how can i use in below code, <?php session_start(); foreach ($_SESSION['playlist'] as $key => $value) { echo "<tr>"; echo "<td>" . $key . "</td>\n<td>" . $value . "</td>"; // code.... echo "</tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568423 Share on other sites More sharing options...
Barand Posted July 16, 2019 Share Posted July 16, 2019 Just echo another <td>..</td> with the delete button html inside it. Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568424 Share on other sites More sharing options...
aveeva Posted July 16, 2019 Author Share Posted July 16, 2019 @Barand If i ask any stupid question, make me a pardon, i am learning in php. <?php session_start(); foreach ($_SESSION['playlist'] as $key => $value) { echo "<tr>"; echo "<td>" . $key . "</td>\n<td>" . $value . "</td>"; echo "<td>" ."<button class='delbtn' data-id='"+ k +"'>Delete</button>" . "<td>"; echo "</tr>"; } ?> Pls correct me? Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568425 Share on other sites More sharing options...
Barand Posted July 16, 2019 Share Posted July 16, 2019 Right idea, but you can't just paste javascript code into php and expect it to work. The variable names and syntax are different. Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568426 Share on other sites More sharing options...
aveeva Posted July 16, 2019 Author Share Posted July 16, 2019 @Barand How can i assign in php? Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568427 Share on other sites More sharing options...
Barand Posted July 16, 2019 Share Posted July 16, 2019 On 7/2/2019 at 12:25 PM, aveeva said: $statement = $connect->prepare($query); $statement->execute(); $result = $statement->fetchAll(); $total_row = $statement->rowCount(); $output = ''; In this excerpt from your code, all lines except the execute() are assigning values - you know how to assign in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568428 Share on other sites More sharing options...
ginerjm Posted July 16, 2019 Share Posted July 16, 2019 How is this button going to work? What is it calling? How is the current row of data being passed to whatever script is going to do the delete? I suggest a form around each row and a proper Submit button (<input type='submit'>) that will do what you need. Using the <button> tag is just not that useful even though people love to use it. I don't get it. Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568437 Share on other sites More sharing options...
Barand Posted July 16, 2019 Share Posted July 16, 2019 27 minutes ago, ginerjm said: How is this button going to work? What is it calling? How is the current row of data being passed to whatever script is going to do the delete? If you read the previous posts and code in this topic before jumping in, all those questions will be answered. Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568439 Share on other sites More sharing options...
aveeva Posted July 17, 2019 Author Share Posted July 17, 2019 @Barand If i click delete button whole list will be deleted, how to delete by single, <html> <head> <style type="text/css"> table { border-collapse: collapse; border: 1px solid black; } table td,th { border: 1px solid black; } td { text-align: center; } </style> </head> <body> <h2>Play Lists</h2> <table id="table"> <th>Voice SKU</th> <th>Voice Name</th> <th>Action</th> <?php session_start(); foreach ($_SESSION['playlist'] as $key => $value) { echo "<tr>"; echo "<td>" . $key . "</td>\n<td>" . $value . "</td>"; echo "<td>". "<button id='btn'>Delete</button>"."</td>"; echo "</tr>"; } ?> </table> <script> var btn = document.getElementById('btn'); btn.onclick = function () { document.getElementById('table').remove(); this.remove(); }; </script> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568456 Share on other sites More sharing options...
Barand Posted July 17, 2019 Share Posted July 17, 2019 18 minutes ago, aveeva said: how to delete by single, By passing the key of the item you want to delete from the SESSION and not removing the entire table. Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568457 Share on other sites More sharing options...
aveeva Posted July 17, 2019 Author Share Posted July 17, 2019 (edited) @Barand Did you mean -> document.getElementById('$key').remove(); ? Edited July 17, 2019 by aveeva Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568459 Share on other sites More sharing options...
Barand Posted July 17, 2019 Share Posted July 17, 2019 You need to remove them from the SESSION, not just from the page display, then redisplay the list from the session data. Otherwise, when you eventually save the session data the deleted items will still be there. 1 Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568461 Share on other sites More sharing options...
aveeva Posted July 19, 2019 Author Share Posted July 19, 2019 (edited) help me with my workout : my_cart.php: <?php header('Content-Type: text/html; charset=utf-8'); session_start(); session_regenerate_id(); echo ' <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <metaname="viewport"content="width=device-width,height=device-height,initial-scale=1"> <title>Playlist</title> </head> <body> <form id="playlist" action="playlist_action.php" method="post"> <table id="playlist"> <caption>Play List</caption> <thead> <tr> <th scope="col">Voice SKU</th> <th scope="col">Voice Name</th> <th scope="col">Action</th> </tr> </thead> <tbody>'; foreach ($_SESSION['playlist'] as $key => $value) { $key = htmlspecialchars($key); echo ' <tr> <th scope="row">', $key, '</th> <td>', htmlspecialchars($value), '</td> <td><button name="delete" value="', $key, '">Delete</button></td> </tr>'; } echo ' </tbody> </table> </form> </body> </html> playlist_action.php <?php header('Content-Type: text/html; charset=utf-8'); session_start(); session_regenerate_id(); if (array_key_exists('playlist_action', $_POST)) { switch ($_POST['playlist_action']) { case 'delete': if (array_key_exists($_POST['delete'], $_SESSION['playlist']) { unset($_SESSION['playlist'][$_POST['delete']]); // notify user of successful delete } else { // notify user of non-existent key in the playlist } default: // handle invalid/unknown action here. } } ?> my output: |Parse error: syntax error, unexpected 'unset' (T_UNSET) in C:\wamp\www\voice_bank\playlist_action.php on line *10*| Edited July 19, 2019 by aveeva Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568508 Share on other sites More sharing options...
Barand Posted July 19, 2019 Share Posted July 19, 2019 On the line before the "unset" ... if (array_key_exists($_POST['delete'], $_SESSION['playlist']) { ... count the number of opening parentheses "(" and count the number of closing ones. (That should show you why the "unset" is unexpected) Quote Link to comment https://forums.phpfreaks.com/topic/308914-php-ajax-passing-value-to-another-php-file/page/2/#findComment-1568509 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.