0

I am new when it comes to PHP, Javascript and AJAX, so sorry if this is a simple question. I am working on plugin; in this plugin I am developing a table in PHP. Users are able to add links to a specific widget to display on their home page. I am trying to make a function with Javascript and AJAX that deletes a row from their list and database. I am trying to assign each TR a unique ID but I am unsure how to do this.. here is my code:

<table id="links" width="350px" border="1">
    <tr>
        <td></td>
        <td><b>Display</b></td>
        <td><b>Link *</b></td>
        <?php       

        if ( isset($_POST['links']) ) {

            $displays = $_POST['displays'];
            $links = $_POST['links'];
            $domain = $_POST['domain'];

            foreach($links as $i => $link) {
                if ( empty($displays[$i]) )
                    $displays[$i] = "";
                if( empty($links[$i]))
                    unset($links[$i]);

            }


            $autodomain = array('displays' => $displays, 'links' => $links, 'domain' => $domain);

            $options = get_option("autodomain_links") or array();
            $options[] = $autodomain;
            //$dump = array();
            update_option("autodomain_links",  $options);

        }
        $options = get_option("autodomain_links");

        if ($options) {

            foreach($options as $option) if ($tmp++ < 15) {

                $displays = $option['displays'];
                $links = $option['links'];
                $domain = $option['domain'];
                foreach($links as $i => $link) 
                {
                    echo '<tr>';
                    echo '<td><a href="#" class="delete"/><img alt="" border="0" src="https://jarkin.its-express-tst.syr.edu/wp-content/uploads/2011/06/delete.png" /></a></td>';
                    echo '<td>' . $displays[$i] . '</td>';
                    echo '<td>' . $link . ' ' . $domain . '</td>';
                    echo '</tr>';
                ;

                }

            }

        }


        ?>

</table>

Here is the Javascript/AJAX:

<script type="text/javascript" >
$(document).ready(function()
        {
            $('table#links td a.delete'').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: "delete_row.php",
                           data: data,
                           cache: false,

                           success: function()
                           {
                                parent.fadeOut('slow', function() {$(this).remove();});
                           }
                     });                
                }
            });
        });
</script>

The Javascript/ajax is using the Table id and the TR id as the parent to identify the row - but since I am using PHP I don't know how to generate unique IDs for each of the TR's.

0

2 Answers 2

2

You can define it by $i:

       foreach($links as $i => $link) 
        {
            echo '<tr id="id_'.$i.'">'; //<--here
            echo '<td><a href="#" class="delete"/><img alt="" border="0" src="https://jarkin.its-express-tst.syr.edu/wp-content/uploads/2011/06/delete.png" /></a></td>';
            echo '<td>' . $displays[$i] . '</td>';
            echo '<td>' . $link . ' ' . $domain . '</td>';
            echo '</tr>';
        ;

        }

Then each row's id is id_{rowNumber}

Sign up to request clarification or add additional context in comments.

7 Comments

when I tried this each TR did not receive a unique value, there were several 1s + 0s
@Jamie, how many times do you go thru this loop? if you only go once you should have unique ids
my plugin is kind of strange - its a widget to allow the user to add links to a list without having to type in the domain name, so each time they enter a new link it is making a new array. I am able to delete each row, but its not deleting from the wordpress database
you have to come up with some way to merge the arrays instead of going thru them over and over (then ull have the same ids).try array_merge : php.net/manual/en/function.array-merge.php
thank you so much I was looking for something exactly like that, however where would I put this in my code?
|
0

You can use a function like mt_rand() to generate a unique string for each row. Or just use a counter to generate a series of numbers for the rows.

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.