Home > Software design >  Laravel form post data from placeholder value
Laravel form post data from placeholder value

Time:05-15

I want to pass the data on a form placeholder without user input, want to know if that is possible...

Below is my form in view

@extends('layouts.app')

@section('content')
<div >
    <div >
        <div >
        <form action="kind" method="POST">
         @csrf
         <div >
    <label for="exampleFormControlInput1"><i style="color:#000"  style="font-size:24px"></i> Name</label>
    <input type="text"  id="exampleFormControlInput1" name="name" placeholder="{{ auth()->user()->name }}" >
    <small>Type your name as given on the placeholder</small>
  </div>
  <div >
    <label for="exampleFormControlInput1"><i style="color:#000"  style="font-size:24px"></i> Organazation</label>
    <input type="text"  id="exampleFormControlInput1" name="organazation">
    <small>Type your organazation</small>
  </div>
  <div >
    <label for="exampleFormControlInput1"><i style="color:#000"  style="font-size:24px"></i> Country</label>
    <input type="text"  id="exampleFormControlInput1" name="country">
    <small>Enter country</small>
  </div>
  <div >
    <label for="exampleFormControlInput1"><i style="color:#000"  style="font-size:24px"></i> Project name</label>
    <input type="text"  id="exampleFormControlInput1" name="title">
    <small>Enter the name for the project</small>
  </div>
  <div >
    <label for="exampleFormControlTextarea1"><i style="color:#000"  style="font-size:24px"></i> Describe project</label>
    <textarea type="text"  id="exampleFormControlTextarea1" rows="3" name="message"></textarea>
    <small>Describe the project in details</small>
  </div>
  <div >
    <label for="exampleFormControlInput1"><i style="color:#000"  style="font-size:24px"></i> Contacts</label>
    <input type="text"  id="exampleFormControlInput1" name="contact">
    <small>Enter office contacts here</small>
  </div>
  <form action="{{ route('file.upload.post') }}" method="POST" enctype="multipart/form-data">
            @csrf
            <div >
  
                <div >
                    <input type="file"  name="imogi">
                </div>
            </div>
            <small>Select business logo if any or implicating image</small>
            <br/>
            <br/>
            <button type="submit" >Send</button>
        </form>
        <br/>
</form>
        </div>
    </div>
</div>
@endsection

...below is my controller

<?php

namespace App\Http\Controllers;

use App\Kind;
use Illuminate\Http\Request;

class KindController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function display(Request $request)
    {
        $request->validate([
            'name'=>'required',
            'organazation'=>'required',
            'country'=>'required',
            'title'=>'required',
            'message'=>'required',
            'contact'=>'required',
            'imogi'=>'required'
           
        ]);
        $Kind = new Kind;
        
        $Kind->name = $request->input('name');
        $Kind->organazation = $request->input('organazation');
        $Kind->country = $request->input('country');
        $Kind->title = $request->input('title');
        $Kind->message = $request->input('message');
        $Kind->contact = $request->input('contact');
        $Kind->imogi = $request->input('imogi');
        $Kind->save();

        return redirect()->back();

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Kind  $kind
     * @return \Illuminate\Http\Response
     */
    public function show(Kind $kind)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Kind  $kind
     * @return \Illuminate\Http\Response
     */
    public function edit(Kind $kind)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Kind  $kind
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Kind $kind)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Kind  $kind
     * @return \Illuminate\Http\Response
     */
    public function destroy(Kind $kind)
    {
        //
    }

And this is my route

Route::POST('/kind', 'KindController@display')->name('kind');

Data is being submitted in the database from user input, but that`s not what i want...i want to capture authenticated user name without user manually inserting it

CodePudding user response:

You need to pass the username through the value attribute, but make the input element read-only like this:

<input type="text"  id="exampleFormControlInput1" name="name" value="{{ auth()->user()->name }}" readonly>

CodePudding user response:

It will automatically exclude the placeholder from being sent when the form is submitted. So you need to send it through the hidden form field. Just replace the following line with your existed line and fetch name at the controller side.

<input type="hidden"  id="exampleFormControlInput1" name="name" value="{{ auth()->user()->name }}" >
  • Related