Ajax请求期间遇到未知错误

时间:2019-09-02 23:01:52

标签: javascript php jquery mysql ajax

我正在尝试使用ajax在数据库中插入一条记录,但这给了我一个未知的错误。

我正在用ajax和php将记录插入用户发送的数据库中。用户还可以插入标签,这些标签首先通过jQuery函数进行序列化,然后通过ajax发送到php请求页面。我正在使用jQuery UI和jQuery TokenField文件让用户以标签格式提供输入,一旦他/她单击按钮,数据就会被序列化,并且可以轻松传递。

这是我的表单和完整的JScript文件,具有允许用户在标签中输入内容的功能:

1.End()     at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()     at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult)     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult

这是提供数据库连接的连接文件:

<!DOCTYPE html>
<html>
<head>

</head>
<body>
      <form class="col-md-12">
            <div class="form-group">
                <label> Project Title </label>
                <input type="text" name="txtName" id="txtName" class="form-control" placeholder="Enter Project's Title">
            </div>
            <div class="form-group">
                <label>Project Description</label>
                <textarea name="txtDescpt" id="txtDescpt" class="form-control" placeholder="Enter Project's Description" rows="5"></textarea>
            </div>
            <p id="exception"> </p>
            <div class="form-group">
                <label>Project Category</label>
                <select class="form-control" id="category" name="category">
                  <option value="0" disabled selected>Choose Project Category</option>
                  <option value="active">Active Project</option>
                  <option value="drafted">Drafted Project</option>
                </select>
            </div>
            <!-- Skill input (in tags) -->
            <div class="form-group create-project-select">
                <label>Skill Tags (for e.g. carpenter, mason, blacksmith, etc.)</label>
                <input type="text" name="skill" id="skill" class="form-control">
            </div>
            <div class="create-project-button">
            <input type="button" class="btn btn-primary" value="Create Project" id="btnCreate" name="btnCreate">
            </div>
      </form>

      <!-- Tags data is coming from database (successfully checked) -->
      <?php
        $selectSkills = $connect->prepare("SELECT * FROM skills");
        $selectSkills->execute();
        $result = $selectSkills->fetchAll();
      ?>

<script>
        $(document).ready(function () {
          $('#skill').tokenfield({
            autocomplete: {
              source:<?php echo "[";
                $str = "'";
                foreach ($result as $row): echo htmlspecialchars($str).$row['name'].htmlspecialchars($str).","; endforeach;
                echo "],
              delay:100";
              ?>
            },
            showAutocompleteOnFocus: true
          });
          $("#btnCreate").click(function () {
            var NameReglExp = /^([A-Za-z_\ _\.]{1,255})$/;
            var name = $("#txtName").val();
            var descpt = $("#txtDescpt").val();
            //var category = document.getElementById('category').value;
            var skills = $.trim($('#skill').val());

            var category = $("#category :selected").attr('value');

            if (name.length > 0)
            {
              if (descpt.length > 0)
              {
                if (category != "0")
                {
                  if (skills.length > 0)
                  {
                    if (NameReglExp.test(name) == true)
                    {
                      $.ajax({
                        type: "POST",
                        url: "working.php",
                        data: {"Name":name, "Descpt":descpt, "Category":category, "Skills":skills},
                        dataType: 'JSON',
                        success: function(feedback) {
                          swal("", feedback.message, feedback.status);
                        },
                        error: function(feedback) {
                          swal("", feedback.message, feedback.status);
                        }                        
                      });
                    }
                    else
                    {
                      swal("", "You are not allowed to enter any special character as a Project Name.", "error", {buttons : false});
                    }
                  }
                  else
                  {
                    swal("", "Please enter atleast one skill to proceed.", "error", {buttons : false})
                  }
                }
                else
                {
                  swal("", "Please select a category.", "error", {buttons : false});
                }
              }
              else
              {
                swal("", "Please write a description about your project", "error", {buttons : false});
              }
            }
            else
            {
              swal("", "Please enter a project name.", "error", {buttons : false});
            }
          });
</script>
</body>
</html>

这是我的mysql db结构图:

This is my mysql db structure image

这是我发送请求的php页面:

//database_connection.php

session_start();

$connect = new PDO("mysql:host=localhost;dbname=***;charset=utf8mb4", "username", "password");

执行php请求页面时出现错误。记录未插入。为了解决该错误,我尝试将<?php //By providing this file I also execute the session_start() function include($_SERVER['DOCUMENT_ROOT']."/database_connection.php"); if (isset($_POST['Name']) && isset($_POST['Descpt'])) { try { $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $name = $_POST['Name']; $description = $_POST['Descpt']; $status = $_POST['Category']; $skills = $_POST['Skills']; $date = date("d-m-Y"); //Converting the value into integer to met the requirments of the database field which takes a value for foreign key as an integer $custId = intval($_SESSION['Reg_Id']); $query = "INSERT INTO projects (cust_id, name, description, skills_required, date_added, status) VALUES ($custId, '$name', '$description', '$skills', '$date', '$status')"; $command = $connect->prepare($query); if ($command->execute()) { echo json_encode(['status' => 'success', 'message' => 'Project Created Successfully.']); } else { echo json_encode(['status' => 'error', 'message' => 'Error Occurred.']); } } catch (PDOException $e) { $exception = $e->getMessage(); echo json_encode(['status' => 'error', 'message' => $exception]); } } ?> 函数回送给ajax请求,但它均无济于事。我个人认为该错误可能是由于标签所致。此外,没有字母大小写或拼写错误。

0 个答案:

没有答案