加载对话框内容并传递变量 [英] Load dialog contents and pass variables

查看:120
本文介绍了加载对话框内容并传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天来,我一直无法弄清楚如何为以下问题设计解决方案:我在数据库中存储了很多项目(大约1300个),每个项目都有自己的"id",一些"name"和第三个属性已启用".

For several days, I cannot figure out how to design a solution for the following issue: I have a lot of items (around 1300) stored in database, each has its own "id", some "name" and a third property "enabled".

我想在同一页面上显示指向(所有)对话框的用户链接.然后,对话框将显示名称",并允许用户选择确定"/取消"(即启用/不执行操作). (通过文件some_file.php进行启用"的更改,该文件已经可以正常工作,并且不受此问题的影响.)

I would like to show on the same page to the user links to (all) the dialogs. Dialogs then shall show the "name" and allow the user to select OK/Cancel (i.e. enable/no action). (Changing of "enable" is made through a file some_file.php, which is already working properly and is not subject of this question.)

我发现了类似的问题,例如,但是其中任何一个都不需要在php之间传递变量和javascript之类的对话框.

I have found similar questions like this or this but any of them so not need to pass variables between php and javascript like my dialogs.

我无法解决以下注释中所述的问题:

I am not able to solve the problems stated below in comments:

javascript:

javascript:

$(function(){
  $('#dialog').dialog({
    autoOpen: false,
    width: 600,
    modal: true,
    buttons: {
        'Cancel': function() {
            $(this).dialog('close');
        },
        'OK': function() {
                $.ajax({
                    url: 'some_file.php',
                    type: 'POST',
                    data: 'item_id=' + id,// here I need to pass variable, i.e. $line["id"] from the php loop
                });
                $(this).dialog('close');
        }
    }
});
$('.link_dialog').click(function(){
    $('#dialog').dialog('open');
    return false;
  });
});`

html + php:

html + php:

<?
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
// not sure here how to pass the "text" to some javascript function
    if ($line["name"]=="") {
      text = "Number ".$line["id"]." does not have any name.";
    } else {
      text = "The name of number ".$line["id"]." is ".$line["name"];
    }
}
?>
<a href='#' class='link_dialog'>Dialog 1</a>
<a href='#' class='link_dialog'>Dialog 2</a>
<a href='#' class='link_dialog'>Dialog 3</a>

<div id='dialog' title='Name' style='display: none;'>
    // not sure here how to extract the "text" from javascript function created above
</div>

jsfiddle演示 (当然,不起作用)

jsfiddle demo (of course, not working)

如果有人明白这一点,我将非常感谢您的帮助.您可以更新我的jsfiddle.

If somebody sees the point, I would really appreciate your help. You can update my jsfiddle.

推荐答案

在PHP中:

<?
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if ($line["name"]=="") {
      $text[$line["id"]] = "Number ".$line["id"]." does not have any name.";
    } else {
      $text[$line["id"]] = "The name of number ".$line["id"]." is ".$line["name"];
    }
}

/***
 * Give each link unique ID (I've used 'dialog-n')
 * Advantage to creating link dynamically: 
 * (what if the number of dialogs changes in the future?)
 * Also suggest that you wrap these in a div
 */
$num_links = count($text);
for($i = 1; $i <= $num_links; $i++) {
    echo "<a href='#' id='dialog-$i' class='link_dialog'>Dialog $i</a>";
}

HTML:

<div id='dialog' title='Name' style='display: none;'>
</div>

在Java语言中:

    var DIALOG_TEXT = <?php echo json_encode($text); ?>; //Pass text via JSON

    $('.link_dialog').click(function() { 
      var link = this;

      //Get link ID
      var link_id = link.attr('id').split('-'); //Split string into array separated by the dash
      link_id = link_id[2]; //Second array element should be the ID number
      var msg_text = DIALOG_TEXT[link_id]; //Retrieve associated text

      //Insert text into dialog div
      $('#dialog').text(msg_text); //Use .html() if you need to insert html

      $('#dialog').dialog({
        buttons: {
          "Cancel": function() {
             $(this).dialog('close');
          },
          "OK": function() {
             $.ajax({
                      url: 'some_file.php',
                      type: 'POST',
                      data: 'item_id=' + link_id, //Use link id number extracted above
                    });
                    $(this).dialog('close');
          }
        }
      }); 
      return false;
    });

我尚未测试以上内容,您可能需要根据需要进行修改.

I have not tested the above, you will probably have to modify for your needs.

选项2:

如果您打算动态生成对话框内容(例如,仅当用户单击链接时),则可以执行以下操作

If you intend to have the dialog content generated dynamically (e.g. only when the user clicks the link), you can do the below

jQuery('#dialog').load('content_generator.php?item_id=**[your id]**').dialog('open'); 

其中"content_generator.php"采用给定的ID并输出适当的文本,然后将".load()"插入到对话框中.

where 'content_generator.php' takes the given id and outputs the appropriate text, which ".load()" inserts into the dialog.

选择2基于Sam 此处

这篇关于加载对话框内容并传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆