简体   繁体   English

使用 laravel 和 vue.js 提交数据后显示消息

[英]show message after submit data using laravel and vue.js

good day;再会; i new in laravel and vue.js when i submit form if i have problem in validate one of input it show correctly like The Brand Name field is required. but my real problem is when submitted form successfully when i show response message that i get from controller **i get only one capital** **litter of message appear** only like ***Y*** like我在提交表单时在 laravel 和 vue.js 中是新的,如果我在验证输入之一时遇到问题,它会正确显示,就像Brand Name field is required. but my real problem is when submitted form successfully when i show response message that i get from controller **i get only one capital** **litter of message appear** only like ***Y*** like一样Brand Name field is required. but my real problem is when submitted form successfully when i show response message that i get from controller **i get only one capital** **litter of message appear** only like ***Y*** like Brand Name field is required. but my real problem is when submitted form successfully when i show response message that i get from controller **i get only one capital** **litter of message appear** only like ***Y*** like "Your Data Added Successfully" but i get the first litter is Y` like attached photo that appear how can i solve this problem thanks here is my controller Brand Name field is required. but my real problem is when submitted form successfully when i show response message that i get from controller **i get only one capital** **litter of message appear** only like ***Y*** like “添加了你的数据成功”, but i get the first litter is Y`,就像出现的附加照片一样,我该如何解决这个问题,谢谢这里是我的控制器

<?php

namespace App\Http\Controllers;

use App\PhoneBook;
use Illuminate\Http\Request;
use Validator;

class PhoneBookController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('phonebook');
    }
   }

    public function add(Request $request)
    {
        //prepare data for validation
        $validationarray=Validator::make($request->all(),
            [
                'name' => 'required',
                'phone' => 'required|min:2|required',
                'email' => 'required',
                ]
            ,[],[
                "name"=>"Brand Name",
                'phone' => 'phone',
                'email' => 'Email',
            ]);

        if($validationarray->fails())
        {
            foreach ($validationarray ->messages()->getMessages() as $field_name =>$message):
                $response['message']=$message;
            endforeach;
            $response['status']="failed";
            return response()->json($response);
        }
        // start pass data to model
        $branddata=array(
            'name'          =>$request->name,
            'phone'     =>$request->phone,
            'email'     =>$request->email,
        );

        // start add data
        $add=PhoneBook::create($branddata)->id;
        if(false !=$add)
        {

            return response(['status'=>'success',"message"=>"Your Data Added Successfully"]);

        }else{
            return response(['status'=>'failed',"message"=>"Error Adding data please try again !!"]);


        }



    }

then my route is那么我的路线是

Route::post('phonebook/add',"PhoneBookController@add")->name("cart.deleteProductFromCart");

my view code template我的视图代码模板

<template>
<div>
    <div class="modal fade" id="addData" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">New message</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <span v-if="list.status=='success'" >Record submitted successfully!</span>

                    <div   v-model="list.message" >{{list.message?list.message[0]:''}}</div>
                    <div class="form-group">

                        <label for="exampleInputEmail1">Brand Name</label>
                            <input type="text" name="name" v-model="list.name" class="form-control" id="exampleInputEmail1" placeholder="Example : Kia - Renault">
                            <p class="help-block">Example : Kia - Renault</p>

                    </div>
                        <div class="form-group">
                            <label  class="col-form-label">phone:</label>
                            <input type="text" name="phone" v-model="list.phone" class="form-control" id="reckipient-name">
                            <p class="help-block">Example : Kia - Renault</p>

                        </div>
                        <div class="form-group">
                            <label  class="col-form-label">email:</label>
                            <input type="text" class="form-control"  v-model="list.email" name="email" id="recikpient-name">
                            <p class="help-block">Example : Kia - Renault</p>

                        </div>

                </div>
                <div class="modal-footer">

                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" @click="save">Send message</button>
                </div>
            </div>
        </div>
    </div>
</div>
</template>


<script>
    export default {

        name: "addData",
        data: function () {
            return {
                list: {
                    name: '',
                    phone: '',
                    email: '',
                    message:'',

                },



            }
        },
        methods: {
            save: function () {
              axios.post('/phonebook/add',this.$data.list)
                    .then((response) => this.list.message=response.data.message)
                  .catch((error) => this.list.message=error.response.data.message)

            }
        }
    }

</script>
<style scoped>

</style>

[![enter image description here][1]][1] [![在此处输入图像描述][1]][1]
[1]: https://i.stack.imgur.com/sZpDS.png [1]:https://i.stack.imgur.com/sZpDS.png

It's good to pass response as JSON Format将响应作为JSON格式传递是很好的

YourXYZController.php
....
$data = new stdClass;
$data->status = "success";
$data->message = "your message";

return response(json_encode($data));

in Vuejs在Vuejs中

axios.post('YourXYZController URL', params)
        .then(response => {
          console.log(response)
        })
        .catch(error => {
          console.log(response)
        })
    })

And also it's best practice to check request and response in network tab of browser.并且最好在浏览器的网络选项卡中检查请求和响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM