无法查看数据库中的数据,但是我可以使用vue.js插入数据

时间:2019-04-29 08:30:31

标签: vue.js vuejs2 axios vue-component

我可以将数据插入数据库,但是我看不到我的数据,即数据库字段中的数据字段为空,正在向数据库发送空数据,任何人都可以解决这个问题,我也遇到此错误-> [Vue警告]:v-中的错误处理程序上的错误:“ TypeError:无法读取未定义的属性'preventDefault'”。  但表格仍在向数据库发送数据(空数据)

这是我的代码

<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.prevent="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"
          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>
import axios from 'axios'

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

  methods: {
    getAllUsers: function(){
      axios.get('http://localhost:8888/vue-and-php/public/api/update-info-form.php?action=read', { crossdomain: true })
      .then((response) => {
        if(response.data.error){
          this.errorMessage = response.data.message;
        }else{
          this.newUser = response.data.newUser;
        }
        });
    },
    saveUser(event){
    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_no: ""};
          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>

0 个答案:

没有答案