Dynamic Form with Upload - php

Straight to the point.
I have dynamic field form. Per row, I need to insert multiple file attachments.
For example i have 3 field row.
Row 1 = 3 files
Row 2 = 2 Files
Row 3 = 3 Files
When I dd($request->all()) the files (upload[]) are not group per row, it became 8, how do i group the files?
Thanks.
Form Code
<div class="row">
<div class="col-md-2">
<div class="form-group">
<input type="text" class="form-control datepicker" name="date[]" />
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" name="description[]" />
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<select class="form-control" name="category[]">
<option selected disabled value="">Please Select</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-1">
<div class="form-group">
<input type="text" class="form-control numeric" name="cost[]" />
</div>
</div>
<div class="col-md-1">
<div class="form-group">
<input type="file" name="upload[]" multiple />
</div>
</div>
<div class="col-md-1">
<div class="form-group">
<button type="button" class="btn btn-success add"><i class="fas fa-plus"></i></button>
</div>
</div>

I will add new class to you input like this:
<div class="col-md-1">
<div class="form-group">
<input type="file" name="upload[]" class="uploads" multiple />
</div>
</div>
then i create function changeUploadName() and call it when you click on add new or delete row.
function changeUploadName() {
$(".uploads").each(function(index) {
$(this).attr('name', 'upload' + index + '[]');
});
}

I changed the name attribute to an unique name.
To capture the request at in the laravel controller, I come out with unique variable to capture the variable.
Example:
Blade View
<div class="col-md-1">
<div class="form-group">
<input type="file" name="upload_{{ $id }}[]" class="uploads" multiple />
</div>
</div>
Controller
$store->file = $request->{'upload_'.$id}

Related

Submitting contact form data

The contact form has several text fields and several dropdowns with the ability to select multiple values. The question is how to send these values by e-mail? Text values of type string (one word at a time) are sent fine. But how to send arrays containing multiple values? How to collect them using the $_POST method and put them in a letter?
Form:
<section class="post-content-area pt-20">
<div class="container">
<div class="row">
<div class="col-lg-8 posts-list">
<div class="card card-signin my-5">
<div class="card-body">
<h5 class="card-title text-center" style="font-size: 26px">Test contact form</h5>
<form method="post" action="email-script.php" enctype="multipart/form-data" id="emailForm">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control" placeholder="Name" >
<div id="nameError" style="color: red;font-size: 14px;display: none">nameError</div>
</div>
<div class="form-group">
<input type="text" name="surname" id="surname" class="form-control" placeholder="Surame" >
<div id="nameError" style="color: red;font-size: 14px;display: none">nameError</div>
</div>
<div class="form-group">
<input type="text" name="phone" id="phone" class="form-control" placeholder="Phone" >
<div id="subjectError" style="color: red;font-size: 14px;display: none">subjectError</div>
</div>
<div class="form-group">
<label>First Level Category</label><br />
<select id="first_level" name="first_level[]" multiple class="form-control">
<?php
foreach($result as $row)
{
echo '<option value="'.$row["first_level_category_id"].'">'.$row["first_level_category_name"].'</option>';
}
?>
</select>
</div>
<div class="form-group">
<label>Second Level Category</label><br />
<select id="second_level" name="second_level[]" multiple class="form-control">
</select>
</div>
<div class="form-group">
<label>Third Level Category</label><br />
<select id="third_level" name="third_level[]" multiple class="form-control">
</select>
</div>
<div class="form-group">
<input type="file" name="attachment" id="attachment" class="form-control">
<div id="attachmentError" style="color: red;font-size: 14px;display: none">attachmentError</div>
</div>
<div class="submit">
<center><input type="submit" name="submit" onclick="return validateEmailSendForm();" class="btn btn-success" value="SUBMIT"></center>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
Emailing script:
https://pastebin.com/3SR67MUP
There are multiple ways on how to send them via email.
The easiest way is getting them on your form action page and sending them with the php mail() function.
To make the email look better you can create an HTML email template file.
Then get the file data with file_get_contents() and resetting strings with your collected data with the str_replace function and putting the data into your mail funtion.
Hope it helps!

Select element dose not display selected value in it's input field - Laravel

I am making a crud application in Laravel. I make edit option, when I click on edit option it displays data from database according to corresponding id. It displays all values in input fields that I have entered while inserting data, but select element dose not display any data in its input field.
Page displaying all data from database
When I click on edit, it displays edit form as shown in the below picture
You can see all input fields displaying correct data from database but select element dose not displaying any value.
Code for this form is
#foreach($result as $list)
<div class="container">
<!------------ Row 1 ------------>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>NAME</label>
<input type="text" name="name" class="form-control" value="{{ $list->cust_name }}">
</div>
#error('name')
<span id="field_error">{{$message}}</span>
#enderror
</div>
<div class="col-md-3">
<div class="form-group">
<label>PHONE</label>
<input type="number" name="phone" class="form-control" value="{{ $list->cust_phone }}">
</div>
#error('phone')
<span id="field_error">{{$message}}</span>
#enderror
</div>
<div class="col-md-3">
<div class="form-group">
<label>CITY</label>
<input type="text" name="city" class="form-control" value="{{ $list->cust_city }}">
</div>
#error('city')
<span id="field_error">{{$message}}</span>
#enderror
</div>
<div class="col-md-3">
<div class="form-group">
<label>INSTALLER NAME</label>
<input type="text" name="instaname" class="form-control" value="{{ $list->inst_name }}" >
</div>
</div>
</div>
<!------------ Row 2 ------------>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>STORE NAME</label>
<select type="text" name="storename" class="form-control" value="{{ $list->storename }}">
<option> ~ SELECT STORE NAME ~</option>
<option>PACKAGES</option>
<option>EMPORIUM</option>
<option>FORTRESS</option>
</select>
</div>
#error('storename')
<span id="field_error">{{$message}}</span>
#enderror
</div>
<div class="col-md-3">
<div class="form-group">
<label>ORDER DATE</label>
<input type="date" name="odate" class="form-control" value="{{ $list->odate }}">
</div>
#error('odate')
<span id="field_error">{{$message}}</span>
#enderror
</div>
<div class="col-md-3">
<div class="form-group">
<label>INSTALLATION DATE</label>
<input type="date" name="instadate" class="form-control" value="{{ $list->inst_date }}">
</div>
#error('instadate')
<span id="field_error">{{$message}}</span>
#enderror
</div>
<div class="col-md-3">
<div class="form-group">
<label>BRAND NAME</label>
<input type="text" name="bname" class="form-control" value="{{ $list->brandname }}">
</div>
#error('bname')
<span id="field_error">{{$message}}</span>
#enderror
</div>
</div>
<!------------ Row 3 ------------>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>PRODUCT NAME</label>
<input type="text" id="add" name="pname" class="form-control" value="{{ $list->cust_order }}">
</div>
#error('pname')
<span id="field_error">{{$message}}</span>
#enderror
</div>
<div class="col-md-3">
<div class="form-group">
<label>ORDER STATUS</label>
<select name="ostatus" class="form-control" value="{{ $list->cust_order_status }}">
<option> ~ SELECT STATUS ~ </option>
<option>PROCESSED</option>
<option>COMPLETED</option>
<option>FAILED</option>
<option>CANCELLED</option>
<option>PENDING</option>
</select>
</div>
#error('ostatus')
<span id="field_error">{{$message}}</span>
#enderror
</div>
<div class="col-md-3">
<div class="form-group">
<label>INSTALLATION DONE BY</label>
<select name="doneby" class="form-control" value="{{ $list->inst_done_by }}">
<option> ~ DONE BY ~ </option>
<option>PCES</option>
<option>SELF</option>
</select>
</div>
#error('doneby')
<span id="field_error">{{$message}}</span>
#enderror
</div>
<div class="col-md-3">
<div class="form-group">
<label>ADDITIONAL COMMENTS</label>
<input type="text" name="comments" class="form-control" value="{{ $list->comments }}">
</div>
#error('comments')
<span id="field_error">{{$message}}</span>
#enderror
</div>
</div>
<!------------ Row 4 ------------>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>ADDRESS</label>
<input type="text" name="address" class="form-control" value="{{ $list->cust_address }}" >
</div>
#error('address')
<span id="field_error">{{$message}}</span>
#enderror
</div>
</div>
</div>
#endforeach
How can this problem be sorted out?
Just putting the value in select won't make the option selected. You have to add the selected attribute in an option to make it selected.
<select type="text" name="storename" class="form-control">
<option> ~ SELECT STORE NAME ~</option>
<option {{ $list->storename == 'PACKAGES' ? 'selected' : '' }}>PACKAGES</option>
<option {{ $list->storename == 'EMPORIUM' ? 'selected' : '' }}>EMPORIUM</option>
<option {{ $list->storename == 'FORTRESS' ? 'selected' : '' }}>FORTRESS</option>
</select>
Same applies for the ORDER STATUS selection. However I can see you are not using any value for options. It's good to use a value for each option. Consider using a unique value for each option.
Value is not getting displayed in select because you have used value in select tag instead of option tag and add keyword selected in option tag
<!-- The second value will be selected initially -->
<select name="choice">
<option value="first">First Value</option>
<option value="second" selected>Second Value</option>
<option value="third">Third Value</option>
</select>

Can't get input values with enctype="multipart/form-data"

I'm working with Forms on Laravel, and I'm still learning. I thought I had it all figured out already, but I've been alted by an issue:
When I have enctype="multipart/form-data", I can't get the input values. The file I upload still gets uploaded to the disk, but the rest of the values are not printed. If I remove enctype="multipart/form-data", I do get the values.
Form:
<form id="forms" method="POST" action="alteracaocomissao" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-row">
<div class="form-group col-md-6">
<label for="nomeentidade">Nome:</label>
<input type="text" class="form-control" id="nome1" name="nome1" placeholder="Nome entidade" required>
</div>
<div class="form-group col-md-6">
<label for="numentidade">Nº:</label>
<input type="text" class="form-control" id="num1" name="num1" placeholder="Número" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6 mb-3">
<label for="conta">Conta:</label>
<input type="text" class="form-control" id="conta" name="conta" placeholder="Conta" required>
</div>
<div class="form-group col-md-3 mb-3">
<label for="balcao">Local:</label>
<select class="form-control" id="local1" name="local1" required>
<option value="">Escolher...</option>
<option value="1">Local</option>
<option value="2">Local1</option>
</select>
</div>
<div class="form-group col-md-3 mb-3">
<label for="atleracao">Tipo de alteração:</label>
<select class="form-control" id="alteracao" name="alteracao" required>
<option value="">Escolher...</option>
<option value="1">Alterar1</option>
<option value="2">Alterar2</option>
</select>
</div>
</div>
<hr>
<div class="form-row" id="buildyourform">
<div class="form-group col-md-4">
<label for="comissao">TEST:</label>
<select class="form-control" id="TEST1" name="TEST1" required>
<option value="">Escolher...</option>
<option value="1">TEST</option>
<option value="2">TEST1</option>
</select>
</div>
<div class="form-group col-md-2">
<label for="desconto">Desconto solicitado:</label>
<div class="input-group">
<input type="text" class="form-control" id="desconto" name="desconto" placeholder="Número" required>
<span class=input-group-addon>%</span>
</div>
</div>
<div class="form-group col-md-2">
<label for="add"> &nbsp </label>
<input type="button" value="Adicionar campos" class="form-control btn btn-light" id="add" />
</div>
<div class="form-group col-md-1" id="2field">
<label for="remove"> &nbsp </label>
<input type="button" value="Remover" class="form-control btn btn-light" id="remove" />
</div>
<div class="form-group col-md-2">
</div>
</div>
<hr>
<div class="form-row">
<div class="form-group col-md-12">
<label for="fundamentacao">Fundamentação:</label>
<textarea type="text" class="form-control" id="fundamentacao" name="fundamentacao" placeholder="Fundamentação do pedido" required></textarea>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<label for="file2">Anexo:</label>
<input type="file" name="file2" id="file2" required>
</div>
</div>
<hr>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function AltComiss(Request $request)
{
session_start();
$array1 = $request -> all();
$path = $request->file('file2')->store('altComiss');
$arrayRm1 = array_shift($array1);
$_SESSION["testPostSection1"] = $array1;
return redirect('alteracaocomissao');
}
}
Route:
Route::post('alteracaocomissao', 'PostController#AltComiss');
Testing code:
#php
session_start();
if (isset($_SESSION["testPostSection1"])) {
echo '<pre>'; print_r($_SESSION["testPostSection1"]); echo '</pre>';
}
#endphp
I do have SESSION there to test if the values are getting saved, because I still havn't set up the database, and until I do I'm using SESSION to test. Obviously once I set the database up I will switch from SESSION to inserting the values into the database.
Thanks in advance!
I think it's will help you
use Illuminate\Http\Request;
//use these For Start your session and Store file
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
class PostController extends Controller{
public function AltComiss(Request $request){
$array1 = $request->all();
if ($request->hasFile('file2')) {
$file = $request->file('file2');
$destinationPath = 'altComiss';
$file->move($destinationPath,$file->getClientOriginalName());
}
$arrayRm1 = array_shift($array1);
Session::flash('allInput',$arrayRm1);
return redirect('alteracaocomissao');
}
}
On your view file for get the session data just use
#if (Session::has('allInput'))
<?php
echo '<pre>';
print_r(Session::get('allInput'));
echo '</pre>';
?>
#endif

Laravel - When edit, the link for the file upload does not show

Only the edit link doesn't show as all the other fields show the data that is saved in the database. In the database I save the file name only as when the link is requested, I change it to save only the file name
//this is code is in the RevisionsController
if($request->hasFile('link')){
$request->file('link');
$request->link->store('public');
$revision->link = $data . $request->link->store('public');
}else{
return 'No file Selected';
}
//this file is the edit.blade.php for the revisions
<div class="panel-body">
<form action="/revisions/{{ $revision->revision_id }}" method="POST" enctype="multipart/form-data">
{{csrf_field()}}
{{ method_field('PUT') }}
<div class="form-group">
<label for="publication_id">Publication</label>
<select class="selectpicker form-control" name="publication_id">
#foreach($publications as $publication)
{{$found = false}}
#if($publication->publication_id == $revision->publications['publication_id'])
{{$found=true}}
#endif
#if($found)
<option selected>{{$publication->name}}</option>
#else
<option>{{$publication->name}}</option>
#endif
#endforeach
</select>
</div>
<div class="form-group">
<label for="edition">Publication Edition</label>
<input type="text" style="background-color: #fff" name="edition" class="form-control" value="{{$revision->edition}}">
</div>
<div class="form-group">
<label for="link">Upload Publication File:</label>
<input type="file" name="link" value="Upload Publication" class="form-control" value="{{$revision->link}}">
</div>
<div class="form-group text-center">
<input type="submit" style="width: 150px;" class="btn btn-success" value="Submit">
</div>
</form>
</div>
You can't pre-fill the value of a <input> tag. If you want to show the current value to the user, think about displaying in a <div> before the input for example.
<div class="form-group">
<label for="link">Upload Publication File: ({{ $revision->link }})</label>
<input type="file" name="link" value="Upload Publication" class="form-control" value="{{$revision->link}}">
</div>

Laravel testing unreachable field error when there is two forms in page

I want to test a page which has two forms in.
When I want to test one of my forms the following error occurs:
Unreachable field "count"
Which count is a field in that form (it doesn't exit in the other form). When i remove the other form completely, it works correctly but I need to have both in the page.
It's my view:
<section class="content">
<div class="row">
<div class="col-md-6">
<div class="panel panel-green">
<div class="panel-heading">
<h3 class="panel-title">
Passenger Credit Adjustment
</h3>
</div>
<div class="panel-body">
<form method="post" action="{{route('credits.passenger.post', ['id' => $passenger->id])}}">
{{csrf_field()}}
<div class="form-group">
<label for="amount">Name</label>
<input type="text" class="form-control" id="name" name="name" disabled="disabled"
title="" value="{{$passenger->fullname}}">
</div>
<div class="form-group">
<label for="amount">Credit</label>
<input type="text" class="form-control" id="cashable" name="name" title=""
disabled="disabled" value="{{number_format($credit) . ' RLS'}}">
</div>
<div class="form-group">
<label for="amount">Add/DeductAmount</label>
<input type="text" class="form-control" id="amount" name="amount">
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" id="description" name="description">
</div>
<button type="submit" class="btn btn-danger">Apply</button>
</form>
</div>
<div class="panel-heading">
<h3 class="panel-title">
<i class="livicon" data-name="money" data-loop="true" data-color="#fff"
data-hovercolor="#fff" data-size="18"></i>
Voucher
</h3>
</div>
<div class="panel-body">
<form method="post"
action="{{route('credits.rideVoucher.post', ['id' => $passenger->id])}}">
{{csrf_field()}}
<div class="form-group">
<label for="type">Type</label>
<select id="single-prepend-text" class="form-control select2" title=""
name="type">
<option value="5k">5k</option>
<option value="10k">10k</option>
<option value="20k">20k</option>
<option value="15p">15%</option>
<option value="20p">20%</option>
<option value="25p">25%</option>
<option value="30p">30%</option>
<option value="50p">50% (Max 10k)</option>
<option value="100p">100% (Max 20k)</option>
</select>
</div>
<button type="submit" class="btn btn-danger" name="generate">Generate</button>
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-green">
<div class="panel-heading">
<h3 class="panel-title">
Free Ride
</h3>
</div>
<div class="panel-body">
<form method="post"
action="{{route('credits.passenger.freeRide.post', ['id' => $passenger->id])}}">
{{csrf_field()}}
<div class="form-group">
<label>Number of free ride(s):</label>
<input type="number" name="count" value="1" title="Count" id="count"
class="form-control">
</div>
<div class="form-group">
<label>Cap:</label>
<select class="form-control" name="cap" title="">
<option value="">[NOT SELECTED]</option>
<option value="50000">5,000 T</option>
<option value="100000">10,000 T</option>
<option value="150000">15,000 T</option>
<option value="200000">20,000 T</option>
</select>
</div>
<input type="submit" class="btn btn-danger" name="generate" value="Generate">
</form>
</div>
</div>
</div>
</div>
It's my test:
$this->visit($uri)
->type(3, 'count')
->select($cap, 'cap')
->press('Generate')
I'm using Laravel 5.1.
Both submit buttons have the same name. Try using different names. Also, instead of using press, you can use the submitForm method

Categories