Bootstrap uses modal box modal to implement form submission pop-up box

A Modal box is a subform that is overlaid on the parent form. In general, the purpose is to display content from a single source, with some interaction without leaving the parent form. Subform can provide information, interaction, etc.
If you want to refer to the plug-in's functions separately, you need to refer to modal.js. Or, as mentioned in the Bootstrap plug-in overview chapter, you can refer to bootstrap.js or the compressed version of bootstrap.min.js.

usage

You can toggle the hidden content of the Modal plug-in:
Through the data attribute: set the attribute data toggle = "module" on the controller element (such as button or link), and set the data target = "
JavaScript: with this technology, you can call the modal box with id="identifier" through a simple line of JavaScript:
$('#identifier').modal(options)

Example 1. Simple bullet box

A static modal window instance, as shown in the following example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"> 
    <title>Bootstrap Example - Modal box( Modal)Plug-in unit</title>
    <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<script>

    // update Form
    function update_info(id)
    {
        var id = id;
        //Another form of more complex json  
        var value2 = {"user_id":"123456","username":"coolcooldool"};  
 
 
        // $('input[name=username]').removeAttr("readonly"); / / remove the readonly attribute of the input element
         var obj2 = eval(value2);  
         // alert(obj2);

        // assignment
        $("#user_id").val(obj2.user_id);
        $("#user_name").val(obj2.username);
        $("#act").val("edit");

        // Set the input element to readonly
        $('#user_id').attr("readonly","readonly")

    }

    

    // Add stock in operation
    function add_info()
    {
        var form_data = $("#form_data").serialize();
        alert(form_data);

    }
</script
<body>

<h2>Create a modal box( Modal)</h2>
<!-- Button trigger mode box -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
    //Add to
</button>
<button class="btn btn-primary btn-lg" onclick="update_info(8)" data-toggle="modal" data-target="#myModal">
    //edit
</button>
<!-- Modal box( Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    ×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    //Modal title
                </h4>
            </div>
            <form id="form_data">
            <div class="modal-body">
                user_id: <input type="text" id="user_id" name="user_id"/>
                name: <input type="text" id="user_name" name="user_name"/>
                <input type="hidden" id="act" value="add" name="act"/>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close
                </button>
                <button type="button" onclick="add_info()" class="btn btn-primary">
                    //Commit changes
                </button>
            </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
</body>
</html>


Example 2: add, delete and modify the form pop-up window


Click Add to open the add form box:


Front page
user_list.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="gb2312">
    <title>User list</title>
    <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<script>
    // Submit Form 
    function delete_info(id)
    {
        if(!id)
        {
            alert('Error!');
            return false;
        }
        // var form_data = new Array();

        $.ajax(
                {
                    url: "action/user_action.php",
                    data:{"id":id, "act":"del"},
                    type: "post",
                    beforeSend:function()
                    {
                        $("#Tip "). HTML (" < span style ='color: Blue '> processing... < / span > ");
                        return true;
                    },
                    success:function(data)
                    {
                        if(data > 0)
                        {
                            alert('Operation successful');
                            $("#Tip "). HTML (" < span style ='color: blueviolet '> congratulations, deletion succeeded! </span>");

                            // document.location.href='world_system_notice.php'
                             location.reload();
                        }
                        else
                        {
                            $("#Tip "). HTML (" < span style ='color: Red '> failed, please try again < / span > ");
                            alert('operation failed');
                        }
                    },
                    error:function()
                    {
                        alert('Request error');
                    },
                    complete:function()
                    {
                        // $('#tips').hide();
                    }
                });

        return false;
    }

    // edit form 
    function get_edit_info(user_id)
    {
        if(!user_id)
        {
            alert('Error!');
            return false;
        }
        // var form_data = new Array();

        $.ajax(
                {
                    url: "action/user_action.php",
                    data:{"user_id":user_id, "act":"get"},
                    type: "post",
                    beforeSend:function()
                    {
                        // $("tip"). HTML ("< span style ='color: Blue '> processing... < / span >");
                        return true;
                    },
                    success:function(data)
                    {
                        if(data)
                        {

                            // Parsing json data
                            var data = data;

                            var data_obj = eval("("+data+")");

                            // assignment
                            $("#user_id").val(data_obj.user_id);
                           
                            $("#name").val(data_obj.name);
                            $("#address").val(data_obj.address);
                            $("#remark").val(data_obj.remark);
                            $("#act").val("edit");

                            // Set the input element to readonly
                            $('#user_id').attr("readonly","readonly")
                           // location.reload();
                        }
                        else
                        {
                            $("#Tip "). HTML (" < span style ='color: Red '> failed, please try again < / span > ");
                          //  alert('operation failed ');
                        }
                    },
                    error:function()
                    {
                        alert('Request error');
                    },
                    complete:function()
                    {
                        // $('#tips').hide();
                    }
                });

        return false;
    }

    // Submit Form 
    function check_form()
    {
        var user_id = $.trim($('#user_id').val());
        var act     = $.trim($('#act').val());

        if(!user_id)
        {
            alert('user ID Cannot be empty!');
            return false;
        }
           var form_data = $('#form_data').serialize();

        // Submit data asynchronously to the action/add_action.php page
        $.ajax(
                {
                    url: "action/user_action.php",
                    data:{"form_data":form_data,"act":act},
                    type: "post",
                    beforeSend:function()
                    {
                        $("#Tip "). HTML (" < span style ='color: Blue '> processing... < / span > ");
                        return true;
                    },
                    success:function(data)
                    {
                        if(data > 0)
                        {

                            var msg = "Add to";
                            if(act == "edit") msg = "edit";
                            $("#Tip "). HTML (" < span style ='color: blueviolet '> congratulations, "+ msg +" succeeded! </span>");
                            // document.location.href='system_notice.php'
                            alert(msg + "OK!");
                            location.reload();
                        }
                        else
                        {
                            $("#Tip "). HTML (" < span style ='color: Red '> failed, please try again < / span > ");
                            alert('operation failed');
                        }
                    },
                    error:function()
                    {
                        alert('Request error');
                    },
                    complete:function()
                    {
                        $('#acting_tips').hide();
                    }
                });

        return false;
    }

    $(function () { $('#addUserModal').on('hide.bs.modal', function () {
        // Clear the edit status to add when closing
        $("#act").val("add");
        location.reload();
    })
    });
</script>
<body>
<div class="container" style="min-width: 1200px;">

<h1>
//User list
</h1>
    <form action="extreme_award_user_list.php" method="post" class="form">
    <table class="table table-bordered">
        <tbody>
        <tr>
            <td>user ID: <input type="text" name="search_user_id" value="{search_user_id}"></td>
            <td>Total condition user: <input type="text" name="search_total" value="{search_total}"></td>
            <td>   <!-- Button trigger mode box -->
                <button class="btn btn-primary btn-sm" data-toggle="modal"  data-target="#addUserModal">
                    //Add user
                </button>
            </td>
        </tr>
        <tr>
            <td colspan="10" style=" text-align: center; padding: 10px; border: none">
                <input type="submit" class="btn btn-default" value="search" />  <a href="extreme_award_user_list.php">default</a>
            </td>
        </tr>
        </tbody>
    </table>
    </form>

    //Total (< b > {total "count} < / b >)
<table class="table table-hover table-bordered" >
    <thead>
    <tr>
        <th>user id</th>
        <th>user name</th>
        <th>address</th>
        <th>Remarks</th>
        <th>operation</th>
    </tr>
    </thead>
    <tbody>
    <!-- BEGIN list -->
    <tr>
        <td>{user_id}</td>
        <td>{name}</td>
        <td>{address}</td>
        <td>{remark}</td>
        <td>
            <button type="button" class="btn btn-info" data-toggle="modal" onclick="return get_edit_info({user_id})" data-target="#Addusermodal "> Edit < / button >
              
        <button type="button" class="btn btn-danger" onclick="return delete_info({id})">delete</button>
        </td>

    </tr>

    <!-- END list -->


    </tbody>
</table>
    {page_str}       

    <!-- Modal box example( Modal) -->
    <form method="post" action="" class="form-horizontal" role="form" id="form_data" onsubmit="return check_form()" style="margin: 20px;">
    <div class="modal fade" id="addUserModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        ×
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        //User information
                    </h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label for="user_id" class="col-sm-3 control-label">user ID</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="user_id" name="user_id" value="{user_id}"
                                       placeholder="Please enter user ID">
                            </div>
                        </div>
                       
                        <div class="form-group">
                            <label for="lastname" class="col-sm-3 control-label">user name</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="user_name" value="" id="user_name"
                                       placeholder="user name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="lastname" class="col-sm-3 control-label">address</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="address" value="" id="address"
                                       placeholder="address">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="remark" class="col-sm-3 control-label">Remarks</label>
                            <div class="col-sm-9">
                                <textarea  class="form-control"  name="remark" value="{remark}" id="remark"
                                       placeholder="Remarks">

                                </textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close
                    </button>
                    <button type="submit" class="btn btn-primary">
                        //Submission
                    </button><span id="tip"> </span>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
    </form>
</div>
</body>
</html>


Background PHP page action/user_action.php

<?php

/**
 * Get submitted data
 *
 */

$act       = $_POST['act'];
$id        = $_POST['id'];
$user_id   = (int)$_POST['user_id'];
$form_data = $_POST['form_data'];
$param_arr = array();


// The format of the data obtained is "foo = Bar & Baz = Boom & cow = Milk & PHP = hypertext + processor"
// The data form of HTTP build query is parsed into array format by parse str
parse_str($form_data, $param_arr);

// Remarks Chinese processing
$param_arr['remark']  = iconv("utf-8", "gbk", trim($param_arr['remark']));


switch($act)
{
    case "add":

        // Add stock in operation
        // ...
        // ...
        break;

    case "edit":

        // Edit operation
        $user_id = $param_arr['user_id'];
        
        // ...
        break;

    case "get":

        // Return detailed user information
        // get($user_id);
        echo $ret;
        exit();
        break;
    case "del":
        // delete
        // delete();
        break;
}

echo $ret > 0 ? 1 : 0;


Original link: https://segmentfault.com/a/1190000000007651357



Tags: PHP JQuery Attribute Javascript

Posted on Mon, 04 May 2020 23:32:21 -0400 by AnAmericanGunner