未捕获的TypeError:$ .post不是addRecord的函数(hrinfo.php:21)

时间:2017-10-11 07:32:48

标签: javascript jquery ajax

我有一个带有按钮的模态,它将在我的数据库中插入记录我使用了ajax但是它给了我一个错误Uncaught TypeError:$ .post不是一个函数

继承我的代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link rel="stylesheet" href="css/w3.css">
<!-- Scripts -->
<script src="js/jquery-3.2.1.slim.min.js" crossorigin="anonymous"></script>
<script src="js/popper.min.js"  crossorigin="anonymous"></script>
<script src="js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<!-- Scripts -->
<script type="text/javascript">  
// Add Record 
function addRecord() {
    // get values
    var firstname = $("#firstname").val();
    var lastname = $("#lastname").val();
    var  basicsalary = $("#basicsalary").val();

    // Add record
    $.post("ajax/hrinfo.php", {
        firstname: firstname,
        lastname: lastname,
        basicsalary:  basicsalary
    }, function (data, status) {
        // close the popup
        $("#addnewrecordmodal").modal("hide");

        // read records again
        readRecords();

        // clear fields from the popup
        $("#firstname").val("");
        $("#lastname").val("");
        $("# basicsalary").val("");
    });
}

// READ records
function readRecords() {
    $.get("ajax/readRecords.php", {}, function (data, status) {
        $(".records_content").html(data);
    });
}
</script>
<style>
.topd{
    background-color: #3498DB;
    padding: 14px 16px;
   font-family: 'Pacifico', cursive;
   color : white;
   /* color: rgba(255, 255, 255, 0.6); */
}
.zbody{
  width: 90%;
  margin-left: 5%;
}
</style>
</head>
<body>
<div class="topd">
<h1>Welcome to HopesV2.0</h1>
</div>
<nav class="navbar navbar-expand-lg navbar-light w3-teal" data-spy="affix" data-offset-top="197">
  <a class="navbar-brand" href="#"></a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
    <div class="navbar-nav">
    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        HR
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <a class="nav-item nav-link" href="#">Features</a>
      <a class="nav-item nav-link" href="#">Pricing</a>
            <a class="nav-item nav-link" href="first.php" onclick="return confirm('Are you want to exit?')">Logout</a>
    </div>
  </div>
</nav>
<BR>
<!-- Start Body -->
<div class="jumbotron zbody">
  <h1>Basic Information</h1>
  <hr class="my-4">
<p class="lead">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<!-- <input class="btn btn-primary" type="submit" value="NEW" name="new" /> -->

<button type="button" class="btn btn-success" data-toggle="modal" data-target="#addnewrecordmodal">New Record</button>

<!-- Modal -->
  <div class="modal fade mymodal1" id="addnewrecordmodal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Employees Basic Information</h4>
</div>
<div class="modal-body">

<div class="form-group">
<label for="first_name">First Name</label>
<input type="text" id="firstname" placeholder="First Name" class="form-control" />
</div>

<div class="form-group">
<label for="last_name">Last Name</label>
<input type="text" id="lastname" placeholder="Last Name" class="form-control" />
</div>

<div class="form-group">
<label for="salary">Basic Salary</label>
<input type="text" id="basicsalary" placeholder="Basic Salary" class="form-control" />
</div>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" onclick="addRecord()">Add Record</button>
</div>
</div>
</div>
  </div>
<!--End Modal Content-->

</form>
  </p>
</div>
<!-- End Body -->  
</body>
</html>

我很确定我正确引用了jquery文件,因为我的前端函数使用jquery工作。但是这个给我带来了一些麻烦。

2 个答案:

答案 0 :(得分:0)

使用
    的jquery-3.2.1.min.js

代替

jquery-3.2.1.slim.min.js

答案 1 :(得分:0)

检查此链接以了解jquery的普通和精简包之间的差异。

What are the differences between normal and slim package of jquery?