Why this ajax jquery php to do list delete action doesn't work? -
i tested source code tutorial http://query7.com/php-jquery-todo-list-part-1 , deploy source taken here http://query7.com/wp-content/uploads/php-jquery-todolist.zip
to surprise after delete action, refreshed screen item still not deleted
i can't see bug in code can ?
process.php
<?php //connect database $connection = mysql_connect('localhost:3316', 'root' , 'root'); $selection = mysql_select_db('notes', $connection); //was form submitted? if($_post['submit']){ //map content sent form variable. not necessary keeps things tidy. $content = $_post['content']; //insert content database $ins = mysql_query("insert `notes` (content) values ('$content')"); //redirect user index page header("location:index.php"); } /*doesn't matter if form has been posted or not, show latest posts*/ //find notes in database , order them in descending order (latest post first). $find = mysql_query("select * `notes` order id desc"); //setup un-ordered list echo '<table border="0" cellpadding="5" cellspacing="0" class="list" width="100%">'; //continue looping through of them while($row = mysql_fetch_array($find)){ //for each one, echo list item giving link delete page it's id. echo '<tr><td valign="middle" width="90%">' . $row['content'] . ' </td> <td valign="middle" width="10%"><form id="form" action="delete.php?id=' . $row['id'] . '" method="post"> <input class="todo_id" name="todo_id" type="hidden" value="' . $row['id'] . '" /> <input class="todo_content" name="todo_content" type="hidden" value="' . $row['content'] . '" /> <input type="image" src="images/delete.png" class="delete" name="delete" width="20px" /> </form> </td></tr>'; } //end un-ordered list echo '</table>'; ?> <script type="text/javascript"> $(".delete").click(function(){ //retrieve contents of textarea (the content) var todo_id = $(this).parent().find(".todo_id").val(); var todo_content = $(this).parent().find(".todo_content").val(); //build url send var url = 'submit=1&id=' + todo_id; //use jquery's ajax function send $.ajax({ type: "post", url: "delete.php", data: url, success: function(){ //if successful , notify user added $("#msg").html("<p class='remove'>you deleted: <i>" + todo_content + "</i></p>"); $("#content").val(''); todolist(); } }); //we return false when button clicked, doesn't follow action return false; }); </script>
delete.php
<?php //connect database $connection = mysql_connect('localhost', 'root' , ''); $selection = mysql_select_db('notes', $connection); //delete.php?id=idofpost if($_post['submit']){ //if($_get['id']){ echo $id = $_post['id']; //$id = $_get['id']; //delete record of post $delete = mysql_query("delete `notes` `id` = '$id'"); //redirect user header("location:index.php"); } ?>
process.php connects mysql database instance running on port 3316 user root , password root, while delete.php connects mysql instance running on default port 3306 user root , empty password: start there
Comments
Post a Comment