簡體   English   中英

我的Vue.js代碼有問題,我在將數據插入數據庫時​​遇到錯誤

[英]I have problem in my Vue.js code, i am getting errors while inserting data into database

從表單提交數據時,我收到3個不同的錯誤

第一個錯誤是:[Vue警告]:掛鈎錯誤:“TypeError:this.getAllUsers不是函數”

然后,如果我測試表單驗證沒有輸入任何驗證工作,但我得到此錯誤:[Vue警告]:v-on處理程序錯誤:“TypeError:無法讀取屬性'preventDefault'未定義”

然后我填寫所有字段並單擊提交按鈕然后我收到此錯誤:[Vue警告]:v-on處理程序中的錯誤:“ReferenceError:axios未定義”

注意:我的API工作得很好

這是我的代碼

這是表格

<template>
  <b-container>
    <div class="update-info">
      <div class="feature-text myinv-title">
        <h5 class="title title-sm">Update your information</h5>
      </div>
      <form id="app" @submit="saveUser" method="post" novalidate="true">

    <p v-if="errors.length">
      <b>Please fill in all the fields</b>
      <ul>
        <li v-for="error in errors" class="alert alert-danger">{{ error }}</li>
      </ul>
    </p>

    <div class="form-row">
      <div class="form-group col-md-3">
        <label for="trx">TRX Address No.</label>
        <input
          id="trx"
          class="form-control trx-address-nooverflow"
          v-model="myAddress"
          type="text"
          name="TRX Address"
          readonly
        >
      </div>
      <div class="form-group col-md-3">
        <label for="name">Name</label>
        <input
          id="name"
          class="form-control"
          v-model="name"
          type="text"
          name="name"
        >
      </div>
      <div class="form-group col-md-3">
        <label for="name">Country</label>
        <country-select
         id="Country"
         class="form-control"
         v-model="country"
         :country="country"
         topCountry="US" />
      </div>
      <div class="form-group col-md-3">
        <label for="email">Email ID</label>
        <input
          id="email"
          class="form-control"
          v-model="email"
          type="email"
          name="email"
        >
      </div>
    </div>
    <div class="form-row">
      <div class="form-group col-md-3">
        <label for="email">Mobile No</label>
        <input
          id="mobile"
          class="form-control"
          v-model="mobile_no"
          type="text"
          name="mobile"
        >
      </div>
      <div class="form-group col-md-3">
        <div class="top-30">
          <input type="submit" class="btn btn-btn btn-grad btn-submit" />
        </div>
      </div>
       <div class="clearfix"></div>
    </div>
    </form>
    </div>
  </b-container>
</template>

這是劇本

<script>
export default{
  data(){
    return{
    errorMessage: "",
    successMessage: "",
    errors: [],
    trx_no: "",
    name: "",
    country: "",
    email: "",
    mobile_no: "",
    myAddress: "",
    newUser: {trx_no: "", name: "", country: "", email: "", mobile: ""}
    }
  },
  mounted: function(){
    this.getAllUsers();
  },

  methods: {

    saveUser(event){
    event.preventDefault()

    this.checkForm()

    if(!this.errors.length) {
       var formData = this.toFormData(this.newUser);

        axios.post('http://localhost:8888/vue-and-php/public/api/update-info-form.php?action=update', formData, { crossdomain: true })
        .then((response) => {

          this.newUser = {trx_no: "", name: "", country: "", email: "", mobile: ""};

          if(response.data.error){
            this.errorMessage = response.data.message;
          }else{
            this.getAllUsers();
          }
          });
      }
    },
    toFormData: function(obj){
      var form_data = new FormData();
      for(var key in obj){
        form_data.append(key, obj[key]);
      }
      return form_data;
    },
    clearMessage: function(){
      this.errorMessage = "";
      this.successMessage = "";
    },
    //validation
    checkForm: function (e) {
      this.errors = [];

      if (!this.name) {
        this.errors.push("Name Required.");
      }
      if (!this.country) {
        this.errors.push("Country Required.");
      }
      if (!this.email) {
        this.errors.push('Email Required.');
      } else if (!this.validEmail(this.email)) {
        this.errors.push('Valid Email Address Required.');
      }
      if (!this.mobile_no) {
        this.errors.push("Phone Number Required.");
      }

      if (!this.errors.length) {
        return true;
      }

      e.preventDefault();
    },

    validEmail: function (email) {
      var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
      return re.test(email);
    }
  }
}
</script>

我也發布我的PHP代碼僅供參考

<?php

//Import Database

require 'base.php';


if(isset($_GET['action'])){
  $action = $_GET['action'];
}

//create
if($action == 'update'){

  $trx_number = $_POST['trx'];
  $name = $_POST['name'];
  $country = $_POST['country'];
  $email = $_POST['email'];
  $mobile = $_POST['mobile_no'];

  $result = $con->query(" INSERT INTO `update_information` (`id`, `trx_no`, `name`, `country`, `email`, `mobile_no`) VALUES (null,'$trx_number','$name','$country','$email','$mobile') ");
  if($result){
    $res['message'] = "Success";
  }else {
    $res['error'] = true;
    $res['message'] = "Error";
  }
  $res['update_information'] = $update_information;
  }
  $con->close();
 //encoding into json
  echo json_encode($res);
  die();


以下是您的錯誤的解決方案

  1. 沒有名為getAllUsers方法您需要在代碼中定義一個或停止調用它。
  2. 從您的代碼中刪除event.preventDefault()並在表單中使用@submit.prevent="saveUser" @submit="saveUser"替換@submit="saveUser"
  3. 使用import axios from 'axios'導入到您的組件中。 注意:確保在項目中安裝了axios ins

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM