简体   繁体   English

使用jQuery模式弹出窗口提交表单

[英]submit form using jquery modal popup

i have button called no of users when i click on the button it opens one modal popup.. 当我单击该按钮时,它会打开一个模式弹出窗口。

so what i am trying to do is in the modal popup i am trying t insert form with one field enter number..and when they click submit i want to store those values in database 所以我想做的是在模式弹出窗口中,我想用一个字段输入数字来插入表格..当他们单击提交时,我想将这些值存储在数据库中

here is my button code: 这是我的按钮代码:

<button type="button" class="btn btn-success"style="float:right; margin:3px 0px 3px 3px" id="btnShow">No Of Users</button>

here is my modal dailog code: 这是我的模态模拟代码:

<script type="text/javascript">
$(function () {
$("#btnShow").click(function(){
$('#demoModal').modal('show');
});
});

<div style="text-align:center; margin-top:10%"></div>

 <div class="modal fade" id="demoModal" 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">&times;</button>
            <h4 class="modal-title" id="myModalLabel">Bootstrap Modal Popup</h4>
        </div>
        <div class="modal-body">Hi, Welcome to Aspdotnet-Suresh.com</div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
        </div>
    </div>
</div>

can anyone help me how to do 谁能帮我怎么做

Thanks in advance.. 提前致谢..

For, an example first you have to create your modal popup form and ajax script in the index.php file 例如,首先,您必须在index.php文件中创建模式弹出窗口和ajax脚本。

--Modal PopUp Form code-- -模式弹出式表单代码-

 <div style="text-align:center; margin-top:10%"></div>

 <div class="modal fade" id="demoModal" 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">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Bootstrap Modal Popup</h4>
    </div>
    <div class="modal-body">

     <form method="post">
       <input type="text" name="name1" id="name1" placeholder="name">
       <br>
       <input type="text" name="age1" id="age1" placeholder="age">
       <br>
       <div id="myCont"></div>
       <br>
       <input type="submit" id="btn" class="btn" value="SUBMIT">
     </form>

    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
    </div>
</div>

--Script Code-- -脚本代码-

 <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function (){
            $("#btn").click(function (e){
                e.preventDefault();
                $.ajax({
                    type: "POST",
                    async: false,
                    url:"page1.php",
                    dataType: 'json',
                    cache: false,
                    success: function (result) {
                            $("#myCont").html(result);
                    },
                    error: function () {
                        alert("server error");
                    }
                });
            });
        });
    </script>

======================================================================== ================================================== ======================

Now create another page named Page1.php which will contain your database insert code. 现在,创建另一个名为Page1.php的页面,其中将包含您的数据库插入代码。

<?php
if(isset($_POST))
{
  $name = $_POST['name1'];
  $age = $_POST['age1'];

  $servername = "localhost";
  $username = "username";
  $password = "password";
  $dbname = "myDB";

  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  // Check connection
  if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
  }

  $sql = "INSERT INTO user_details (name, age)
  VALUES ($name, $age)";

  if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
  } else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  }

  mysqli_close($conn);
}
?>

I have been tried this previously and working very nice. 我以前已经尝试过了,并且工作非常好。

Hope this will work for you. 希望这对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM