I can insert data into database but i cannot see my data i.e data fields are empty in database form is sending empty data to database can anybody solve this i am also getting this error-> [Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'preventDefault' of undefined". but still, the form is sending data to database (empty data)

here's my code

<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>

this is my script

<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 = /^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[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 Answers