Laravel 8 - Submit button not working with multiple forms - php

I'm trying to use a submit button that creates a registry (Memo) and it's products asociated to that Memo, however, I cannot get this to work.
I think it might be because I have a form inside another form. However I believe since my submit button its outside the inner form, should still work (it worked before).
I'll put my create.blade.php code below:
<form method="post" action="{{ route('memos.store') }}">
<div class="form-group">
#csrf
<label for="lbl_memo_attendant" id="lbl_memo_attendant"><strong>Solicitante:</strong></label>
<input type="text" class="form-control" name="memo_petitioner" value="{{ Auth::user()->name }}" readonly="readonly"/>
</div>
<div class="form-group">
<label for="lbl_attendant_department"><strong>Departamento solicitante:</strong></label>
<input type="text" class="form-control" name="memo_petitioner_department" value="{{ Auth::user()->department }}" readonly="readonly"/>
</div>
<div class="form-group">
<label for="lbl_product_type" name="lbl_memo_product_type"><strong>Tipo de solicitud:</strong></label></br>
<select name="memo_product_type">
<option value="Bienes" selected>Bienes</option>
<option value="Servicio">Servicio</option>
</select>
</div>
<div class="form-group">
<label for="lbl_program" name="lbl_memo_program"><strong>Programa:</strong></label></br>
<select name="memo_program">
<option value="Hospital Williams" selected>Hospital Williams</option>
<option value="Gasto Operacional DSSM">Gasto Operacional DSSM</option>
<option value="Cadi Umag">Cadi Umag</option>
</select>
</div>
<div class="container">
<h2 align="center">Ingresar productos:</h2>
<form name="add_name" id="add_name" action="{{ route('products.store') }}">
<div class="form-group">
<div class="alert alert-danger print-error-msg" style="display:none">
<ul></ul>
</div>
<div class="alert alert-success print-success-msg" style="display:none">
<ul></ul>
</div>
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td class="col-6"><input type="text" name="product_name" placeholder="Producto" class="form-control name_list" /></td>
<td><label>Unidad:</label>
<select>
<option value="Unidad" selected>Unidad</option>
<option value="Cajas">Cajas</option>
<option value="Global">Global</option>
</select>
</td>
<td><input placeholder="Cantidad" type="number" name="product_qty"></input></td>
<td><button type="button" name="add" id="add" class="btn btn-success">Agregar otro producto</button></td>
</tr>
</table>
</div>
</form>
</div>
</div>
<div class='div-btn-create'>
<button type="submit" class="btn btn-success" id="btn_create_memo">Crear memorando</button>
</div>
</form>

HTML does not support nested forms, so the browser turns them into one. Which is why your button is outside the form. Remove the nested form and everything will work.

Related

Page 404 not found laravel 8

i am trynig to insert data in database when i click submit button it returns 404 not found
here is the controller
function ajouterfrm()
{
return view('dashboards.directeur.ajouter');
}
function add(Request $request)
{
$request->input();
}
and here is the routes
Route::get('rapport/ajouter',[DirecteurController::class,'ajouterfrm'])->name('directeur.ajouter');
Route::post('add',[DirecteurController::class,'add'])->name('directeur.add');
my html :
here is my html
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3" style="margin-top: 50px">
<h4>Ajouter Rapport</h4>
<hr>
<form action="add" method="POST">
<input type="text" name="data_1" required>
<input type="text" name="data_2" required>
<!-- others inputs and selects ... -->
</form>
</div>
</div>
</div>
i tried to unname the last route but without any results
any suggestions ?
thanks
Your form should look like this :
<form action="{{ route('directeur.add') }}" method="post">
#csrf // for preventing forgery token attacks otherwise you will get an "419 error"
<your inputs ...>
<form>
bonne chance.
Your add route doesn't do anything at this point, so no view or response is returned for the request.
You need to return something from your controller. Please see:
https://laravel.com/docs/8.x/responses
But for now you can try in your add function
return response('test');
here is my html
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3" style="margin-top: 50px">
<h4>Ajouter Rapport</h4>
<hr>
<form action="add" method="POST">
<div class="form-group">
<label for="">Titre</label>
<input type="text" name="titre" class="form-control" placeholder="Entrer le titre du rapp">
</div>
<div class="form-group">
<label for="">Description</label>
<textarea name="description" class="form-control" rows="3" placeholder="Entrer la description ici...">
</textarea>
</div>
<div class="form-group">
<label for="">Médecin</label>
<select class="form-control" name="medecin" id="">
<option value="med">Médecin 1</option>
<option value="med">Médecin 2</option>
<option value="med">Médecin 3</option>
</select>
</div>
<div class="form-group">
<label for="">Patient</label>
<select class="form-control" name="patient" id="">
<option value="pat">Patient 1</option>
<option value="pat">Patient 2</option>
<option value="pat">Patient 3</option>
</select>
</div>
<br>
<button type="submit" class="btn btn-primary btn-block">Ajouter</button>
</form>
</div>
</div>
</div>

Dynamic Form with Upload

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}

Laravel data not going appropriately into the database

What i need is if i get first element id of MOB/TR/1743 then press the mark button data which relevant to that id must go the database.
This is what should in the database.
But this is what i'm getting into the database. That means getting another column data into database.
Here is the view of that.
<div class="row">
<section id="feature" class="section-padding wow fadeIn delay-05s">
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-6 col-xs-12">
<div class="wrap-item text-center">
<div class="jumbotron">
<div class="item-img">
<img src="images/ser02.png">
</div>
<div class="form-group">
<form action="search" method="post" class="form-inline">
<select name="institute" id="institute">
<option selected="selected" value="id">Trainee Id</option>
<option value="full_name">Trainee Name</option>
<label for="Search">Name</label>
</select>
<input type="text" name="search" /><br>
<input type="hidden" value="{{ csrf_token() }}" name="_token" />
<input type="submit" name="submit" value="Search">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
<div class="col-md-12 col-sm-6 col-xs-12">
<div class="panel panel-default">
<div class="panel-body">
<table class="table table-striped">
<thead>
<th>Trainee ID</th>
<th>Name with Initials</th>
<th>Time</th>
<th>Mark Here!</th>
</thead>
<tbody>
<form action="{{route('TraineeAttendance.store')}}" method="post" >
{{ csrf_field() }}
#foreach($items as $item)
<tr>
<td>
<div class="form-group">
<input type="text" name="trainee_id" class="form-control" value="{{ $item->trainee_id }}">
</div>
</td>
<td>
<div class="form-group">
<input type="text" name="name" class="form-control" value="{{ $item->name_with_initials }}">
</div>
</td>
<td>
<label><input type="checkbox" name="time" id="time" value="time"> Time</label>
</td>
<td>
<input type="submit" class="btn btn-info">
</td>
</tr>
#endforeach
</form>
</tbody>
</table>
</div>
</div>
</div>
Why i`m getting wrong data?
try this hope it helps
#foreach($items as $item)
<form action="{{route('TraineeAttendance.store')}}" method="post" >
{{ csrf_field() }}
<tr>
<td>
<div class="form-group">
<input type="text" name="trainee_id" class="form-control" value="{{ $item->trainee_id }}">
</div>
</td>
<td>
<div class="form-group">
<input type="text" name="name" class="form-control" value="{{ $item->name_with_initials }}">
</div>
</td>
<td>
<label><input type="checkbox" name="time" id="time" value="time"> Time</label>
</td>
<td>
<input type="submit" class="btn btn-info">
</td>
</tr>
</form>
#endforeach

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

Submit same forms with two buttons and send thank you mail only on the second submit button

I am having the issue on submitting the same form with two submit buttons. I am submitting the form for the first time with ajax submission and on the second time I want to submit the whole form but when I send the form the thank you mail hits two times . I want to send the thank you mail only on the second submit button and not on the first button. I have the button type ="button" ,
Here is my form code:-
<form name="basicform" id="basicform" method="post" action="<?php bloginfo("template_directory"); ?>/forms/callback/callback_process.php">
<input type="hidden" value="<?php echo $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" name="source" id="source" />
<input type="hidden" value="submited" name="basicform"/>
<input type="hidden" name="form_name" id="form_name" value="Header Contact Form" class="basicformname" >
<div id="sf1" class="frm">
<fieldset>
<!--<legend>Take a step for a Good health.</legend>-->
<div class="wor-alert"><p class="text-center fill-fields">Please Fill All This Fields*</p></div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="travcure-icon travcure-user"></i></span>
<input type="text" placeholder="Name" id="uname" name="uname" class="form-control" autocomplete="off">
<span class="nameerr" id="unameerr"><?php echo #$_SESSION['unameerr'] ? $_SESSION['unameerr']:''; unset($_SESSION['unameerr']); ?></span>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="travcure-icon travcure-envelope"></i></span>
<input type="text" placeholder="Email" id="uemail" name="uemail" class="form-control" autocomplete="on">
<span class="emailerr" id="uemailerr"><?php echo #$_SESSION['uemailerr'] ? $_SESSION['uemailerr']:''; unset($_SESSION['uemailer']); ?></span>
</div>
</div>
<div class="form-group">
<input type="text" placeholder="Phone Number" id="uphone" name="uphone" class="uphone form-control" autocomplete="off">
<span class="phoneerr" id="uphoneerr"><?php echo #$_SESSION['uphoneerr'] ? $_SESSION['uphoneerr']:''; unset($_SESSION['uphoneerr']); ?></span>
<input type="hidden" class="uphonefull" name="uphonefull">
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="travcure-icon travcure-user-md"></i></span>
<span class="plain-select">
<select name="utreatments" data-style="btn-primary" id="utreatments" class="form-control selectpicker">
<option value="">Select Treatment</option>
<option value="Alternative Treatment">Alternative Treatment</option>
<option value="2">Treatment Two</option>
<option value="3">Treatment Three</option>
<option value="4">Treatment Four</option>
<option value="5">Treatment Five</option>
</select>
</span>
</div>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn open1" type="button" id="head-frm-btn">Next <span class="fa fa-arrow-right"></span></button>
</div>
</fieldset>
</div>
<div id="sf2" class="frm" style="display: none;">
<fieldset>
<!--<legend>What treatment are you looking for?</legend>-->
<div class="form-group">
<span class="plain-select">
<select name="ucity" id="ucity" class="form-control">
<option value="">Select City</option>
<option value="Mumbai">Mumbai</option>
<option value="Pune">Pune</option>
<option value="Banglore">Banglore</option>
<option value="Nagpur">Nagpur</option>
<option value="Goa">Goa</option>
</select>
</span>
</div>
<div class="form-group">
<span class="plain-select">
<select name="uhospital" id="uhospital" class="form-control">
<option value="">Select Hospital</option>
<option value="Hospital One">Hospital One</option>
<option value="Hospital Two">Hospital Two</option>
<option value="Hospital Three">Hospital Three</option>
<option value="Hospital Four">Hospital Four</option>
<option value="Hospital Five">Hospital Five</option>
</select>
</span>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn back2" type="button"><span class="fa fa-arrow-left"></span> Back</button>
<button class="btn open2" type="button" id="head-frm-btn-two">Next <span class="fa fa-arrow-right"></span></button>
</div>
</fieldset>
</div>
<div id="sf3" class="frm" style="display: none;">
<fieldset>
<!--<legend>Almost done</legend>-->
<div class="waittext">
<p>Our customer care executive will get in contact with you soon. Please bear with us.</p>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn back3" type="button"><span class="fa fa-arrow-left"></span> Back</button>
<input class="btn open3" value="Submit" type="submit" name="head_form_submit">
</div>
</fieldset>
</div>
</form>
I submit this form using ajax for the first submit button and then on the second button I send the whole information but I want to send the thank you mail on the second button and not on the ajax submission of the button. I want to get th email of both the forms but i want to send the thank you mail only on the second submit button.
you can use two submit buttons
<button type="submit" name="submit">Submit form</button>
<button type="submit" name="submit_and_send_email">Submit and Send Email</button>
in php you can access the post like this
<?php
if(isset($_POST['submit']))
{
//submit only
}
if(isset($_POST['submit_and_send_email']))
{
//submit and send email
}
?>

Categories