php - 如何将此表单放入对话框?

标签 php javascript jquery html

背景:

这个页面是一个表单,有一个表格可以为不同的用户选择权限(只读,可编辑) 对于每个选择框,名称是用户的 ID。

问题:

1)如何使用jquery放入对话框? 我用谷歌搜索发现弹出框不是像往常一样的新页面,而只是<div>中的一个表单。 element ,在这种情况下我需要使用数据表,所以我不能简单地这样做。

2)弹出框关闭后如何取回值? 对于每个选择框,名称是用户的 ID,我想获取每个用户的用户 ID 和权限设置。

谢谢你的帮助

这是 add.php(上层表单)

<?
include("../connection/conn.php");
session_start();

if($_SERVER['REQUEST_METHOD'] == "POST"){

if (!isset($_POST['subscribe']))
$_POST['subscribe']=0;
if (!isset($_POST['unsubscribe']))
$_POST['unsubscribe']=0;

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

try {

    $listName = $_POST['lname'];
    $listFromName = $_POST['fname'];
    $listReplyTo = $_POST['remail'];
    $listSubject = $_POST['sub'];
    $listRemindSub = $_POST['subscribe'];
    $listRemindUnSub = $_POST['unsubscribe'];
    $listReminder = $_POST['creminder'];

    $query="INSERT INTO list (ListID,ListName,FromName,ReplyTo,Subject,IsRemindSub,IsRemindUnSub,CreateDate,Reminder) VALUES ('',?,?,?,?,?,?,CURDATE(),?)";
    $stmt = $conn->prepare($query);


    $stmt->bindParam(1, $listName , PDO::PARAM_STR);
    $stmt->bindParam(2, $listFromName, PDO::PARAM_STR);
    $stmt->bindParam(3, $listReplyTo, PDO::PARAM_STR);
    $stmt->bindParam(4, $listSubject, PDO::PARAM_STR);
    $stmt->bindParam(5, $listRemindSub, PDO::PARAM_INT);
    $stmt->bindParam(6, $listRemindUnSub, PDO::PARAM_INT);
    $stmt->bindParam(7, $listReminder, PDO::PARAM_STR);

    $stmt->execute();


}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="add.php"> Back</a>'); 
    $conn->rollBack();
    }


try {
    $lastID=$conn->lastInsertId();
    $query="INSERT INTO require_attributes (ReqID,ListID,Attribute,Tag) VALUES ('',$lastID,'Email','{email}')";
    $stmt = $conn->prepare($query);
    $stmt->execute();
    $query="INSERT INTO require_attributes (ReqID,ListID,Attribute,Tag) VALUES ('',$lastID,'FirstName','{fname}')";
    $stmt = $conn->prepare($query);
    $stmt->execute();
    $query="INSERT INTO require_attributes (ReqID,ListID,Attribute,Tag) VALUES ('',$lastID,'LastName','{lname}')";
    $stmt = $conn->prepare($query);
    $stmt->execute();
}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="add.php"> Back</a>'); 
    $conn->rollBack();
    } 

try {
    $userID = $_SESSION['username'];
    $query="INSERT INTO user_list (UserID,ListID,UserListRights) VALUES (?,$lastID,'Edit')";
    $stmt = $conn->prepare($query);
    $stmt->bindParam(1, $userID, PDO::PARAM_STR);

    $stmt->execute();

}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="add.php"> Back</a>'); 
    $conn->rollBack();
    }    


$conn = null;

?>
<div id="stylized" class="myform">
<div style="text-align:center;font-weight:bold;">You have created a list. By default Mail Address, First Name , Last Name is in your list. Add more field if you want. <a href='add.php'>Back</a></div>
<div class="spacer"></div>
</div>
<?
}else{?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script src="../plugin/jquery-1.6.1.min.js"></script>
<script src="../plugin/jquery.validate.min.js"></script>
<script src="../plugin/jquery.form.js"></script>
<link rel="stylesheet" type="text/css" href="../style/form.css" />
<script>
function loadDialogBox() {'
    $("#myEmptyDiv").load("popup.php", function() {

      $("#myEmptyDiv").dialog({
         title: "Permission Setting",
         autoOpen: false,
         buttons: { "Submit" : submitDialogForm() }
      });
      $("#myEmptyDiv").dialog("open");
    });
}

$(function() { $("#openDialog").on('click', loadDialogBox); });

$(document).ready(function(){
$("#addlist").validate();
});
</script>

</head>
<body>
<div id="stylized" class="myform">
<form id="addlist" method="post" action="add.php" >
<h1>Create your new subscriber list</h1> 
<p>Create a new list before adding subscriber <label class="right"><em class="dot">*</em> indicates required</label></p>

<label><em class="dot">*</em> List name:
<span class="small">Add your list name</span>
</label>
<input id="lname" name="lname" class="required" />

<div class="spacer"></div>

<label>Reminder:
<span class="small">Remind the details of your list</span>
</label>
<textarea id="creminder" name="creminder" cols="52" ></textarea>

<div class="spacer"></div>

<p>Permission Setting ...</p>

<a id="openDialog" href="#">Permission Settings</a>
<label>Or open to other users</label> <input type="checkbox" class="checkbox" name="public" value="1">


<div class="spacer"></div>

<p>Email me when ...</p> 
<label>People subscribe</label> <input type="checkbox" class="checkbox" name="subscribe" value="1">
<label>People unsubscribe</label> <input type="checkbox" class="checkbox" name="unsubscribe" value="1">


<div class="spacer"></div>
<input class="submit" type="submit" name="submit" value="Submit"/>
<div class="spacer"></div>

</form>
<div class="spacer"></div>
</div>
<br><br><br>
<div id="stylized" class="myform">
<?
try{
$sql = '
    SELECT   *
    FROM     list,user_list
    WHERE    user_list.UserID=?
    AND list.ListID=user_list.ListID
';
$stmt = $conn->prepare($sql);
$stmt->execute(array($_SESSION['username']));
$result= $stmt->fetchAll();
$num_rows= $stmt->rowCount();
}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="add.php"> Back</a>'); 
    }
$conn = null;
if ($num_rows == 0) {
    echo '<div style="text-align:center;font-weight:bold;">You have not created any list yet.</div>';}
else {
    echo '<h1>Your Subscriber List</h1> <p>You have created '.$num_rows.' list(s).</p>';
foreach ($result as $set) 
{
echo '<div style="font-weight:bold;">List Name : '.$set['FromName'].'</div><br>';
echo '<div style="font-weight:bold;">Subscriber : </div><br>';
echo '<div style="font-weight:bold;">Create Date : '.$set['CreateDate'].'</div><br>';
echo '<hr>';
}}
    ?>

<div class="spacer"></div>
</div>
</body>
</html>

<?
}
?>

这是popup.php

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
      @import "../plugin/datatable/media/css/demo_page.css";
      @import "../plugin/datatable/media/css/demo_table.css";
    </style>
    <script type="text/javascript" src="../plugin/datatable/media/js/jquery.js"></script>
    <script type="text/javascript" src="../plugin/datatable/media/js/jquery.dataTables.js"></script>
    <script>

    function submitDialogForm() {
  $.post("add.php", $("#myEmptyDiv form").serialize(), function(data) {
     $("#yourUserIdDisplay").val(data.userId);
  });
}
    $(document).ready(function() {
    $('#viewSub').dataTable();
    } );
    </script>
    </head>
<body>
<?
include("../connection/conn.php");
session_start();
$sql =
    "SELECT  UserID,Name,Rights,Position
    FROM     user
    WHERE UserID != ?
    AND Rights != 'Admin'
    ";
$stmt = $conn->prepare($sql);
$stmt->execute(array($_SESSION['username']));
$num_rows= $stmt->rowCount();
if ($num_rows == 0)
echo "There is no another user in this system";
else{
$result = $stmt->fetchAll();
?>
<form>
<table width="100%" class="display" id="viewSub">
<thead>
<tr>
<th field="col1" width="40%">Name:</th>
<th field="col2" width="40%">Position:</th>
<th field="col2" width="20%">Permission:</th>
</tr>
</thead>
<tbody>
<?
foreach ($result as $set) 
{
    echo "<tr><td>".$set['Name']."</td><td>".$set['Position']."</td><td><select name=".$set['UserID']."><option value='default'>Not Share</option><option value='Read'>Read Only</option><option value='Edit'>Editable</option></select></td></tr>";
}
?>
</tbody> 
</table>
<div style='text-align:center'> <button id='save'>Confirm</button></div>
</form>
<?
}
?>
</body>
</html>

最佳答案

首先,要从 jQuery 将页面加载到对话框中,请尝试如下操作:

function loadDialogBox() {
    $("#myEmptyDiv").load("url-to-this-form.php", function() {

      $("#myEmptyDiv").dialog({
         title: "your dialog title",
         autoOpen: false,
         buttons: { "Submit" : submitDialogForm() }
      });
      $("#myEmptyDiv").dialog("open");
    });
}

现在您可以通过调用 loadDialogBox 函数来加载和打开表单。也许是这样的:

<a id="openDialog" href="#">Open Dialog</a>

$(function() { $("#openDialog").on('click', loadDialogBox); });

然后,像这样创建一个提交函数:

function submitDialogForm() {
  $.post("submit-page.php", $("#myEmptyDiv form").serialize(), function(data) {
    // here you can parse your return data for userid or permission settings that come back from server
     $("#yourUserIdDisplay").val(data.userId);
  });
}

这将序列化对话框表单数据并将其发布到您的服务器。

希望这对您有所帮助!

关于php - 如何将此表单放入对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9543096/

相关文章:

php - Laravel:如何根据用户权限返回验证消息?

phpMyAdmin fatal error : Call to undefined function __()

javascript - magento 中的嵌套类别下拉列表

php - 为什么我的ajax无法获取输入表单中当前输入的字符串?

javascript - 在鼠标单击事件上停止音轨

javascript - 如何在 d3 条形图中的单个条形中添加值

javascript - Bootstrap Datepicker - 月份和年份没有间距

jquery - Bootstrap 3 导航栏菜单项 - 淡入和淡出

jquery - 如何使悬停时菜单点击消失

php - 使用 get_the_terms() 不会返回应用于 WooCommerce 产品的特定父类别的子条款