2

I am trying to delete a value in a comma seperated string in a table. The script that does this (delete_url.php) works fine on it's own if I set the post values manually, but I'm having a problem passing those values to the script with ajax. In the page that I want to pass the values from I have:

<script type="text/javascript">
            $(document).ready(function() {

                 $('.delete').click(function() {
        $.ajax({
            type: 'POST',
            url: '../scripts/delete_link.php', 
            data: 'link=' + $(this).attr('data_link') + '&topic_pk=' + $(this).attr('data_topic'),
            success: function() {

            }
        });        
    });
            });
        </script>

And:

<a class='delete_link' href='#' data_link='<?php echo urlencode($link); ?>' data_topic='<?php echo $topic_pk; ?>' onclick="return confirm('Are you certain you want to DELETE this link?')";><img src="../images/delete.png" width="16" height="16" alt="delete" title="delete this link" border='0' /></a>

I probably should be using json, but not sure of the correct way to do this. Thanks for any help.

3
  • 1
    If you are storing multiple values in one database column, you are doing it wrong! Commented Feb 18, 2013 at 2:15
  • 1
    Sorry, but that is not the problem, and the only way that I can do this. Commented Feb 18, 2013 at 2:17
  • I just saw the error, should be $('.delete_link').click(function() { Commented Feb 18, 2013 at 2:18

1 Answer 1

1

I have updated my code with the folowing and now works deleting the record and removing the entry on the page:

<script type="text/javascript">
$(document).ready(function(){

    $('table#delTable td a.delete_link').click(function()
    {
        if (confirm("Are you sure you want to delete this row?"))
        {
            var id = $(this).parent().parent().attr('id');
            var data = 'id=' + id ;
            var parent = $(this).parent().parent();

            $.ajax(
            {
                   type: "POST",
                   url: '../scripts/delete_link.php',
                   data: 'link=' + $(this).attr('data_link') + '&topic_pk=' + $(this).attr('data_topic') + '&topic_introduction=' + $(this).attr('data_introduction'),
                   cache: false,

                   success: function()
                   {
                    parent.fadeOut('fast', function() {$(this).remove();});
                   }
             });
        }
    });
});

 </script>

And the table:

<table id='delTable' width="100%" border="0" cellpadding="5">
<?php 
if(!empty($retrieved_links)){
    foreach($retrieved_links as $link){
?>
  <tr>
    <td><?php echo $link; ?></td>
    <td width='16' align='center' valign='middle'><a class='delete_link' href='#' data_link='<?php echo urlencode($link); ?>' data_topic='<?php echo $topic_pk; ?>' data_introduction='<?php echo $topic_introduction; ?>'><img src="../images/delete.png" width="16" height="16" alt="delete" title="delete this link" border='0' /></a></td>
  </tr>
<?php }
}
?>
</table>

And delete_link.php:

<?php
require_once('../connection/connect.php');
mysql_select_db($database, $connection);

$link = urldecode($_POST['link']);
$topic_pk = $_POST['topic_pk'];
$topic_introduction = $_POST['topic_introduction'];


if(!empty($link)){

$query_get_topic = "SELECT * FROM topic WHERE topic_pk = '$topic_pk'";
$result_get_topic = mysql_query($query_get_topic, $connection) or die(mysql_error());
$row_get_topic = mysql_fetch_assoc($result_get_topic);

$retrieved_links = explode(",", $row_get_topic['links']);   

$delete_link = array_search($link,$retrieved_links);

unset($retrieved_links[$delete_link]);
$updated_links = mysql_real_escape_string(implode(',',$retrieved_links));

$links_body = str_replace(',', '<p>', $updated_links);
$topic = $topic_introduction . '<p>' . $links_body;

$query = "UPDATE topic SET links = '$updated_links', topic = '$topic' WHERE topic_pk = '$topic_pk'";
$result = mysql_query($query, $connection) or die(mysql_error());
}
mysql_close();
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.