views:

583

answers:

2

I have a form that adds links to a database, deletes them, and -- soon -- allows the user to edit details. I am using jQuery and Ajax heavily on this project and would like to keep all control in the same page. In the past, to handle editing something like details about another website (link entry), I would have sent the user to another PHP page with form fields populated with PHP from a MySQL database table. How do I accomplish this using a jQuery UI modal form and calling the details individually for that particular entry?

Here is what I have so far-

<?php while ($linkDetails = mysql_fetch_assoc($getLinks)) {?>
<div class="linkBox ui-corner-all" id="linkID<?php echo $linkDetails['id'];?>">
<div class="linkHeader"><?php echo $linkDetails['title'];?></div>
<div class="linkDescription"><p><?php echo $linkDetails['description'];?></p>
<p><strong>Link:</strong><br/>
<span class="link"><a href="<?php echo $linkDetails['url'];?>" target="_blank"><?php echo $linkDetails['url'];?></a></span></p></div>
<p align="right">
<span class="control">
<span class="delete addButton ui-state-default">Delete</span> 
<span class="edit addButton ui-state-default">Edit</span>
</span>
</p>
</div>
<?php }?>

And here is the jQuery that I am using to delete entries-

$(".delete").click(function() {
      var parent = $(this).closest('div');
      var id = parent.attr('id');
      $("#delete-confirm").dialog({
                     resizable: false,
                     modal: true,
                     title: 'Delete Link?',
                     buttons: {
                         'Delete': function() {
      var dataString = 'id='+ id ;
         $.ajax({
         type: "POST",
         url: "../includes/forms/delete_link.php",
         data: dataString,
         cache: false,
         success: function()
         {
          parent.fadeOut('slow');
          $("#delete-confirm").dialog('close');    
         }
        });                                
                         },
                         Cancel: function() {
                            $(this).dialog('close');
                         }
                     }
                 });
       return false;
});

Everything is working just fine, just need to find a solution to edit. Thanks!

A: 

*Updated to include all of the fields you are editing

It sounds like you have the right idea. You would probably want to create a new div on your page for the edit modal dialog.

<div id="dialog-edit" style="background-color:#CCC;display:none;">
    <input type="hidden" id="editLinkId" value="" />
    Link Name: <input type="text" id="txtLinkTitle" class="text" />
    Link Description <input type="text" id="txtLinkDescription" class="text" />
    Link URL <input type="text" id="txtLinkURL" class="text" />
</div>

When the user clicks your edit button you'll want to populate the hidden field and the text box with the values of the link they clicked on and then turn the dialog on.

$('.edit').click(function () {
            //populate the fields in the edit dialog. 
            var parent = $(this).closest('div');
            var id = parent.attr('id');

            $("#editLinkId").val(id);

            //get the title field
            var title = $(parent).find('.linkHeader').html();
            var description = $(parent).find('.linkDescription p').html();
            var url = $(parent).find('.linkDescription span a').attr("href");
            $("#txtLinkTitle").val(title);
            $("#txtLinkDescription").val(description);
            $("#txtLinkURL").val(url);

            $("#dialog-edit").dialog({
                bgiframe: true,
                autoOpen: false,
                width: 400,
                height: 400,
                modal: true,
                title: 'Update Link',
                buttons: {
                    'Update link': function () {
                        //code to update link goes here...most likely an ajax call.

                        var linkID = $("#linkID").val();
                        var linkTitle = $("#txtLinkTitle").val()
                        var linkDescription = $("#txtLinkDescription").val()
                        var linkURL = $("#txtLinkURL").val()
                        $.ajax({
                            type: "GET",
                            url: "ajax_calls.php?function=updateLink&linkID=" + linkID + "&linkTitle=" + linkTitle + "&linkDescription=" + linkDescription + "&linkURL=" + linkURL,
                            dataType: "text",
                            error: function (request, status, error) {
                                alert("An error occured while trying to complete your request: " + error);
                            },
                            success: function (msg) {
                                //success, do something 
                            },
                            complete: function () {
                                //do whatever you want 
                            }
                        }); //end ajax
                        //close dialog
                        $(this).dialog('close');
                    },
                    Cancel: function () {
                        $(this).dialog('close');
                    }
                },
                close: function () {
                    $(this).dialog("destroy");
                }
            }); //end .dialog()

            $("#dialog-edit").show();
            $("#dialog-edit").dialog("open");

        }) //end edit click
Erikk Ross
Looks like this is just what I am looking for, too late to test it tonight. Would you please explain what .find(.linkHeader').html() does? A previous poster mentioned this and I was unable to find any information in jQuery's API or on the web.. thanks! :)
Benjamin
I lied, had to try this before I finished working for the day - worked PERFECTLY. Thank you so much for your help! Still wondering about .find('.linkHeader').html() though. Thanks again :)
Benjamin
Okay, perhaps I spoke too soon. I saw that it pulled the title using the example you provided but I can't get it to pull the description and URL in the same fashion. I need to be able to edit every part. Any ideas?
Benjamin
In your HTML code you have a div named "linkHeader" and within it you are displaying the title of the link. The find method is searching within the parent div (the linkID div) and finding the "linkHeader" div. Then the html() function finds the html code that is within that div...in your case the link title. http://api.jquery.com/find/
Erikk Ross
I updated my answer above to show how you could do all of the fields. Personally I'd probably change your HTML markup to make the selectors a bit easier, but the above will work.
Erikk Ross
Thank you so much. I completely forgot about that in my HTML markup.. this is what happens when one works too long without taking breaks. Thank you :)
Benjamin
A: 

Solved by simply wrapping each line of text from the PHP in <span class="theseDetails">blahblah</span> and using $(".theseDetails").text();.... very simple solution. :)

Benjamin
Yeah so apparently this doesn't work, either.. did not factor in the fact that there are several records and it would just pull from each span with that ID and mash them all together, for example: Site1somewhereSite2somewhere, etc
Benjamin
Right, that's why you need to base your selector on the parent div of the edit button you clicked on.
Erikk Ross