【问题标题】:Populating fields in modal form using PHP, jQuery使用 PHP、jQuery 以模态形式填充字段
【发布时间】:2011-02-08 15:29:24
【问题描述】:

我有一个表单,它可以添加指向数据库的链接、删除它们,并且——很快——允许用户编辑详细信息。我在这个项目中大量使用 jQuery 和 Ajax,并希望将所有控件都保留在同一页面中。过去,为了处理编辑关于另一个网站的详细信息(链接条目)之类的内容,我会将用户发送到另一个 PHP 页面,其中的表单字段由 MySQL 数据库表中的 PHP 填充。如何使用 jQuery UI 模态表单并单独调用该特定条目的详细信息来完成此操作?

这是我目前所拥有的-

<?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 }?>

这是我用来删除条目的 jQuery-

$(".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;
});

一切正常,只需要找到一个解决方案进行编辑。谢谢!

【问题讨论】:

  • 如果您设法进行删除,您将设法进行编辑。您可以使用 $('.linkHeader').html() 获取值,将它们传递给模态表单上的控件。

标签: php jquery mysql forms jquery-ui-dialog


【解决方案1】:

*已更新以包含您正在编辑的所有字段

听起来你的想法是对的。您可能希望在页面上为编辑模式对话框创建一个新 div。

<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>

当用户单击您的编辑按钮时,您需要使用他们单击的链接的值填充隐藏字段和文本框,然后打开对话框。

$('.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

【讨论】:

  • 看起来这正是我正在寻找的东西,今晚来不及测试它。您能解释一下 .find(.linkHeader').html() 的作用吗?以前的海报提到了这一点,我无法在 jQuery 的 API 或网络上找到任何信息。谢谢! :)
  • 我撒了谎,在我完成一天的工作之前必须尝试这个 - 完美地工作。非常感谢你的帮助!尽管如此,仍然想知道 .find('.linkHeader').html() 。再次感谢:)
  • 好吧,也许我说得太早了。我看到它使用您提供的示例提取了标题,但我无法让它以相同的方式提取描述和 URL。我需要能够编辑每个部分。有什么想法吗?
  • 在您的 HTML 代码中,您有一个名为“linkHeader”的 div,在其中显示链接的标题。 find 方法是在父 div(linkID div)中搜索并找到“linkHeader”div。然后 html() 函数找到该 div 中的 html 代码...在您的情况下是链接标题。 api.jquery.com/find
  • 我更新了上面的答案,以展示您如何完成所有领域。就个人而言,我可能会更改您的 HTML 标记以使选择器更容易一些,但上述方法将起作用。
【解决方案2】:

通过简单地将PHP中的每一行文本包装在&lt;span class="theseDetails"&gt;blahblah&lt;/span&gt;中并使用$(".theseDetails").text();....非常简单的解决方案。 :)

【讨论】:

  • 是的,所以显然这也不起作用..没有考虑到有几条记录的事实,它只会从具有该 ID 的每个跨度中提取并将它们混合在一起,例如: Site1somewhereSite2somewhere, etc
  • 对,这就是为什么您需要将选择器基于您单击的编辑按钮的父 div。
猜你喜欢
  • 2016-02-19
  • 2023-03-16
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 2021-05-20
相关资源
最近更新 更多