简体   繁体   中英

refresh page after submit on bootstrap modal

I am using bootstrap admin theme in one of my project. I have a popup modal in one of the php page and i have a registration form in that popup, now when i enter the details and click on submit, the data is actually being inserted into the database and modal closes by default, but the page doesn't refreshes, as i have a table of registered members in the page, i only can see that entry when i refresh the page, and again when i refresh the page, it asks me to reload the page or close the alert box, when i click on reload, the entry goes into the database for two times, i am attaching my code, please help, i am new to stack overflow and bootstrap as well.

Modal code

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Add New Member </button>

<!-- 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"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Add New Member</h4>
      </div>
      <div class="modal-body">
        <form method="post">
          <fieldset>
            <div class="form-group">
              <label>First Name</label>
              <input name="fname" class="form-control" placeholder="First Name" type="text" required>
            </div>
            <div class="form-group">
              <label>Last Name</label>
              <input name="lname" class="form-control" placeholder="Last Name" type="text" required>
            </div>
            <div class="form-group">
              <label>Email Address</label>
              <input name="email" class="form-control" placeholder="Email Address" type="email" required>
            </div>
            <div class="form-group">
              <label for="h-input">Cell Number</label>
              <input name="cell" type="text" class="form-control" placeholder="XXX XXX XXXX (Enter number without space)">
            </div>
            <div class="form-group">
              <label>Member Type</label>
              <select class="form-control" name="membertype">
                <option value="Member">Member</option>
                <option value="Guest">Guest</option>
              </select>
            </div>
            <div class="form-group">
              <label>Member Status</label>
              <select class="form-control" name="memberstatus">
                <option value="Active">Active</option>
                <option value="Inactive">Inactive</option>
              </select>
            </div>
          </fieldset>
          <div>
            <input name="addmember" class="btn btn-primary" value="Add New Member" type="submit"/>
          </div>
        </form>
        <?php
        if(isset($_POST['addmember'])) {
            require('config.php');
            $fname = $_POST['fname'];
            $lname = $_POST['lname'];
            $email = $_POST['email'];
            $cell = $_POST['cell'];
            $mtype = $_POST['membertype'];
            $mstatus = $_POST['memberstatus'];  

            $addmember = "INSERT INTO members (fname, lname, email,cellnumber,type,status,regdate) VALUES (:fname,:lname,:email,:cell,:mtype,:mstatus,CURDATE())";

            $q = $conn->prepare($addmember);
            $q->execute(array(
            ':fname'=>$fname,
            ':lname'=>$lname,
            ':email'=>$email,
            ':cell'=>$cell,
            ':mtype'=>$mtype,
            ':mstatus'=>$mstatus,
            )); 
            if(!$q) {
                echo "Error: Member not added.";
            }
            else {
                header("Location: addMembers.php");
            }
        }
        ?>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

i have created this modal file as a separate file and included in my main php file where table of registered members is printed.

Location requires an absolute path. Check the second to last note in the Notes section.

http://php.net/manual/en/function.header.php

When you manually refresh the page you are just resending the form data that was submitted which is why you get two entries in the DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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