在提交表单 Vue.js 时显示 flash 消息

Posted

技术标签:

【中文标题】在提交表单 Vue.js 时显示 flash 消息【英文标题】:Show flash message onSubmit form Vue.js 【发布时间】:2020-07-16 13:51:39 【问题描述】:

我正在尝试创建一个联系表单,我已经完成了,但现在我正在尝试在提交时创建一个提交 Flash 消息。提交后,表单会转到 php 文件,但我想显示一条消息而不是重定向。

这就是我所拥有的名为 FlashMessage 的消息组件

<template>
  <div
    class="flex items-center bg-green-500 text-white text-sm font-bold px-4 py-3 mb-6"
    role="alert"
    v-show="show"
  >
    <svg class="fill-current w-4 h-4 mr-2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
      <path
        d="M12.432 0c1.34 0 2.01.912 2.01 1.957 0 1.305-1.164 2.512-2.679 2.512-1.269 0-2.009-.75-1.974-1.99C9.789 1.436 10.67 0 12.432 0zM8.309 20c-1.058 0-1.833-.652-1.093-3.524l1.214-5.092c.211-.814.246-1.141 0-1.141-.317 0-1.689.562-2.502 1.117l-.528-.88c2.572-2.186 5.531-3.467 6.801-3.467 1.057 0 1.233 1.273.705 3.23l-1.391 5.352c-.246.945-.141 1.271.106 1.271.317 0 1.357-.392 2.379-1.207l.6.814C12.098 19.02 9.365 20 8.309 20z"
      />
    </svg>
    <p> body </p>
  </div>
</template>

<script>
export default 
  name: "FlashMessage",
  props: 
    message: 
      type: String,
      required: true
    
  ,
  data() 
    return 
      show: false,
      body: ""
    ;
  ,
  created() 
    if (this.message) 
      this.flash(this.message);
    
    window.events.$on("flash", message => this.flash(message));
  ,
  methods: 
    flash(message) 
      this.show = true;
      this.body = message;

      setTimeout(() => 
        this.hide();
      , 3000);
    ,
    hide() 
      this.show = false;
    
  
;
</script>

这是表格

<FlashMessage message="Message sent!" />

<form @submit.prevent="onSubmit" action="./backend/contact.php" method="POST">
<div class="mb-4">
  <label class="block" for="name">
    <span class="text-gray-900">Full name</span>
    <input
      class="form-input mt-1 block w-full outline-none"
      placeholder="Sherlock Holmes"
      id="name"
      name="name"
      type="text"
    />
  </label>
</div>

<div class="mb-4">
  <label class="block" for="email">
    <span class="text-gray-900">Email address</span>
    <input
      class="form-input mt-1 block w-full outline-none"
      placeholder="sherlock@bakerstreet.com"
      id="email"
      name="email"
      type="email"
    />
  </label>
</div>

<div class="mb-4">
  <label class="block" for="department">
    <span class="text-gray-700">Department</span>
    <select
      class="form-select mt-1 block w-full outline-none"
      id="department"
      name="department"
    >
      <option value="1">General Support</option>
      <option value="2">Hosting Support</option>
      <option value="3">Domain Support</option>
      <option value="4">Game Support</option>
    </select>
  </label>
</div>

<div class="mb-4">
  <label class="block" for="message">
    <span class="text-gray-900">Message</span>
    <textarea
      class="form-input mt-1 block w-full outline-none h-56 resize-none"
      placeholder="Enter your message here"
      id="message"
      name="message"
      type="text"
    />
  </label>
</div>

<div class="flex mt-6">
  <label class="flex items-center">
    <input type="checkbox" class="form-checkbox" />
    <span class="ml-2">
      I agree to the
      <a href="#" class="underline">privacy policy</a>
    </span>
  </label>
</div>

<div class="flex mt-6">
  <label class="flex items-center">
    <button
      type="submit"
      class="flex items-center py-3 px-6 border rounded bg-gray-800 hover:bg-gray-900 font-medium text-white transition duration-300 ease-in-out"
    >
      <i class="fas fa-paper-plane mr-4"></i>
      Send
    </button>
  </label>
</div>
</form>

<script>
import FlashMessage from "../components/FlashMessage";
import FeaturesWGrid from "../components/FeaturesWGrid";
import Grid from "../components/Grid";
import GridItem from "../components/GridItem";

export default 
  name: "Contact",
  components: 
    FlashMessage,
    FeaturesWGrid,
    Grid,
    GridItem
  ,
  methods: 
    onSubmit() 
      show: true;
    
  
;
</script>

contact.php

<?php
include("/home/hostedca/redacted/includes/api.php");

$api_identifier = "";
$api_secret = "";

$postfields = array(
    "identifier" => $api_identifier,
    "secret" => $api_secret,
    "action" => "OpenTicket",
    "responsetype" => "json",
);

$name = $_POST["name"];
$email = $_POST["email"];
$department = $_POST["department"];
$message = $_POST["message"];

$command = "OpenTicket";
$postData = array(
    "deptid" => "$department",
    "name" => "$name",
    "email" => "$email",
    "subject" => "Contact form Inquiry from $name for $department",
    "message" => "$message",
    "priority" => "Medium",
    "markdown" => false,
);

$adminUsername = "admin";

// Call the localAPI function
$results = localAPI($command, $postData, $adminUsername);
if ($results['result'] == 'success') 
    echo 'Message sent successfully!';
 else 
    echo "An Error Occurred: " . $results['result'];

?>

不知道哪里出错了。

【问题讨论】:

当您在表单中设置操作时,您的数据会转到后端,因此页面会因此而刷新。您必须使用 ajax 调用来防止页面刷新。 我不认为我可以使用 ajax,因为表单是使用 WHMCS API 提交的,我将添加 PHP 代码,但如果你能提供一个很好的例子。 【参考方案1】:

show 数据属性应该是类似的道具:

export default 
  name: "FlashMessage",
  props: 
    message: 
      type: String,
      required: true
    ,
   show:
   type:Boolean,
   default:false
   
  ,
  data() 
    return 

      body: ""
    ;
  ,

在联系人组件中:

<FlashMessage message="Message sent!" :show="showMsg"/>

....
export default 
  name: "Contact",
  components: 
    FlashMessage,
    FeaturesWGrid,
    Grid,
    GridItem
  ,
 data()
 return
  showMsg:false,

,
  methods: 
    onSubmit() 
      this.showMsg= true;
    
  
;

【讨论】:

【参考方案2】:

您可以从子组件FlashMessage.vue 中删除“v-show”条件。直接使用parent中的值,

data 部分将showfalse 设置为默认值。更改onSubmit() 方法如下

export default 
  name: "Contact",
  data:()=>
    return 
      show:false
    
  ,
  methods: 
    onSubmit() 
      this.show = true;
      setTimeout(()=>
        this.show = false;
      ,3000);
      // rest of your logic
    
  
;

在调用子组件时添加v-ifv-show 条件,如follow

<FlashMessage message="Message sent!" v-if = "show"/>

【讨论】:

【参考方案3】:

您没有将show 作为道具传递,并且它没有在父级中声明,因此,与其在子级的data 中声明它,不如让父级控制并将变量传递为prop如下:

在父级中:
    <FlashMessage message="Message sent!" show="show"/>
    data()
         return
              show:false
         
    ,
    methods:
         onSubmit()
             this.show = true;
             setTimeout(() => 
                 this.show = false;
             , 3000);
         
    
在孩子身上:
    <template>
          <div
               class="flex items-center bg-green-500 text-white text-sm font-bold px-4 py-3 mb-6"
               role="alert"
               v-show="show"
           >
                <svg class="fill-current w-4 h-4 mr-2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
                <path
                 d="M12.432 0c1.34 0 2.01.912 2.01 1.957 0 1.305-1.164 2.512-2.679 2.512-1.269 0-2.009-.75-1.974-1.99C9.789 1.436 10.67 0 12.432 0zM8.309 20c-1.058 0-1.833-.652-1.093-3.524l1.214-5.092c.211-.814.246-1.141 0-1.141-.317 0-1.689.562-2.502 1.117l-.528-.88c2.572-2.186 5.531-3.467 6.801-3.467 1.057 0 1.233 1.273.705 3.23l-1.391 5.352c-.246.945-.141 1.271.106 1.271.317 0 1.357-.392 2.379-1.207l.6.814C12.098 19.02 9.365 20 8.309 20z"
               />
            </svg>
            <p> message </p>
         </div>
    </template>

    <script>
    export default 
         name: "FlashMessage",
         props: 
             message: 
                  type: String,
                  required: true
             ,
             show: 
                  type: Boolean,
                  required: true
             
         ,
    ;
    </script>
    ```

【讨论】:

以上是关于在提交表单 Vue.js 时显示 flash 消息的主要内容,如果未能解决你的问题,请参考以下文章

表单验证错误消息以角度提交时显示

React JS:Yup & Formik 错误消息在提交时显示多次

在 laravel 集体中提交表单时显示加载器

如何通过 Javascript / jQuery 在按钮单击时显示引导警报?

单击提交重力表单时显示加载动画

高级卡支付 SDK 在提交表单时显示错误