Value Old() On Laravel Blade, Seems Not To Be Working - php

I want to retrieve the old value input that user has entered on form, so he/she won't need to re-enter all the form inputs again.
In order to do that, I have added these:
<input type="text" class="form-control" name="uid" value="{{ (!empty($member)) ? $member->mbr_national_code : old('uid') }}">
<input type="text" class="form-control" name="mobile" value="{{ (!empty($member)) ? $member->mbr_mobile : old('mobile') }}">
<input type="text" class="form-control" name="phone" value="{{ (!empty($member)) ? $member->mbr_phone : old('phone') }}">
<input type="text" class="form-control" name="post_code" value="{{ (!empty($member)) ? $member->mbr_post_code : old('post_code') }}">
<input type="text" class="form-control" name="email" value="{{ (!empty($member)) ? $member->user->usr_email : old('email') }}">
<textarea id="" class="form-control" cols="30" rows="3" name="address" placeholder="Street, block, etc">{{ (!empty($member)) ? $member->mbr_address : old('address') }}</textarea>
So I tried retrieving old requests by saying:
old('input_name')
But it does not seem to be working, and when the form refreshes, no data added from the last request.
So how to fix this? I would really appreciate any idea or suggestion from you guys...
Thanks.

Try passing second param to old method
<input type="text" class="form-control" name="uid" value="{{old('uid',$member->mbr_national_code??null) }}">
Full code
<input type="text" class="form-control" name="uid" value="{{old('uid',$member->mbr_national_code??null) }}">
<input type="text" class="form-control" name="mobile" value="{{ old('mobile',$member->mbr_mobile??null) }}">
<input type="text" class="form-control" name="phone" value="{{ old('phone',$member->mbr_phone??null) }}">
<input type="text" class="form-control" name="post_code" value="{{ old('post_code',$member->mbr_post_code??null) }}">
<input type="text" class="form-control" name="email" value="{{old('email',$member->user->usr_email??null) }}">
<textarea id="" class="form-control" cols="30" rows="3" name="address" placeholder="Street, block, etc">{{ old('address',$member->mbr_address??null) }}</textarea>

try this,
it is much simpler
<input type="text" class="form-control" name="mobile" value="{{ old('mobile') ?? $member->mbr_mobile }}">

Related

How to send multiple variable from blade to controller

<input type="date" class="form-control" name="from" value="{{ date('Y-m-d') }}">
<input type="date" class="form-control" name="to" value="{{ date('Y-m-d') }}">
i want to send 'from' and 'to' to controller from blade. how to write that in the tag. the necessary code is mentioned above. Thanks for your help in advance.
<form method="post" action="{{ URL("/totalsales/") }}">
<input type="datetime-local" class="form-control" name="from" >
<input type="datetime-local" class="form-control" name="to" >
<input type="submit" class = "btn btn-primary">
this is the method to send data in controller
your route should be post .
Use form:
<form method="post" action="{{ url("/totalsales/{{ }}") }}">
<input type="date" class="form-control" name="from" value="{{ date('Y-m-d') }}">
<input type="date" class="form-control" name="to" value="{{ date('Y-m-d') }}">
<input type="submit" value="Send">
</form>

MediumText and textArea in my form show.blade

I need your help because I am stuck... I should create a form with a text long.
In my function up(), I have this
$table->mediumText('resume');
In my page create.blade.php, the create works.
<fieldset class="form-group {{ $errors->has('resume') ? 'has-error' : '' }}">
<label for="form-group-input-1">Résumé</label>
<textarea name="resume" id="resume" class="form-control" required="required" value="{{ old('resume')}}"/></textarea>
{!! $errors->first('resume', '<span class="help-block">:message</span>') !!}
</fieldset>
However, I don't get the value (the text) on my page show.blade.php
<fieldset class="form-group">
<label for="form-group-input-1">Résumé</label>
<textarea name="resume" class="form-control" id="form-group-input-1" readonly="true" value="{{$livres->resume}}"/></textarea>
</fieldset>
If I don't use textarea but input type=text it works.
Do you have an idea please?
instead of this
<fieldset class="form-group">
<label for="form-group-input-1">Résumé</label>
<textarea name="resume" class="form-control" id="form-group-input-1" readonly="true"
value="{{$livres->resume}}"/></textarea>
</fieldset>
use this
<fieldset class="form-group">
<label for="form-group-input-1">Résumé</label>
<textarea name="resume" class="form-control" id="form-group-input-1" readonly="true"/>{{$livres->resume}}</textarea>
</fieldset>
Change your code like this (textarea does not get value):
<textarea name="resume" class="form-control" id="form-group-input-1" readonly="true"/>{{$livres->resume}}</textarea>

How to make two rows insert with one button Laravel using ajax

i want to make two inserts in same table.
The table is based on this fields (locale, project_id(foreign key), title, caption).
And the controller looks like here:
public function storeTranslation(Request $request)
{
$projecttranslation = new ProjectTranslation();
$projecttranslation->locale = $request->input("locale");
$projecttranslation->project_id = $request->input("project");
$projecttranslation->title = $request->input("title");
$projecttranslation->caption = $request->input("caption");
$projecttranslation->save();
}
The form for the moment looks like here:
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="locale" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
<form enctype="multipart/form-data" id="myFormTraduccion2" name="myFormTraduccion2"> <!--SECOND FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="title">Language:</label>
<input type="text" id="locale" name="locale" value="es" disabled class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit2" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!--SECOND FORM TO TRANSLATE END HERE -->
</div>
</div>
And the ajax look like this:
$("#createtranslatesubmit").click(function(){
$("#myFormTraduccion").submit();
});
$("#myFormTraduccion").submit(function(e){
e.preventDefault();
$.ajax({
url:'/admin/projects/postUploadTranslation',
type:'POST',
data:$('#myFormTraduccion').serializeArray(),
success: function(){
$("#form2").fadeOut(1000);
$("#form3").fadeIn(2000);
}
});
});
This create only with the first form, the first translation.
I think i should change the view code to this (Two same inputs for each field of database):
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="locale" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale" name="locale" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
</div>
</div>
That's correct? The problem to "store" the data, i think will be a foreach in controller.
And finally, i don't have any idea, how to pass the data in the ajax, with a formdata maybe?
Thanks a lot, any help will be appreciated!
When you submit a form, you send to the server the data of that specific form. Your approach of using multiple forms doesn't work here, because you want to send all data with only 1 specific form submit.
So you have to only create 1 form and separate the different translations with a numeric reference.
Your HTML (note the -0 and -1 used to separate id and name of each input element) :
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale-0" name="locale-0" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project-0" name="project-0" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title-0" name="title-0" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption-0" name="caption-0" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale-1" name="locale-1" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project-1" name="project-1" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title-1" name="title-1" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption-1" name="caption-1" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form>
The controller:
public function storeTranslation(Request $request)
{
$projecttranslation0 = new ProjectTranslation();
$projecttranslation0->locale = $request->input("locale-0");
$projecttranslation0->project_id = $request->input("project-0");
$projecttranslation0->title = $request->input("title-0");
$projecttranslation0->caption = $request->input("caption-0");
$projecttranslation0->save();
$projecttranslation1 = new ProjectTranslation();
$projecttranslation1->locale = $request->input("locale-1");
$projecttranslation1->project_id = $request->input("project-1");
$projecttranslation1->title = $request->input("title-1");
$projecttranslation1->caption = $request->input("caption-1");
$projecttranslation1->save();
}
Of course, it can be easily generalized for N multiple transations and not only 2.
try this out:
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="ProjectTranslation[0][locale]" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="ProjectTranslation[0][project]" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="ProjectTranslation[0][title]" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="ProjectTranslation[0][caption]" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale" name="ProjectTranslation[1][locale]" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="ProjectTranslation[1][project]" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="ProjectTranslation[1][title]" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="ProjectTranslation[1][caption]" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
</div>
So you will get array of ProjectTranslation at controller side
Now at controller side
public function storeTranslation(Request $request)
{
$form_data = $request->get('ProjectTranslation');
foreach($form_data as $form){
$projecttranslation = ProjectTranslation::create($form);
$projecttranslation->save();
}
}

POST returning empty array inside MVC Php app

I am having difficulty understanding why my application is sending an empty POST array upon form submission in my PHP MVC app.
I have the following code in my Register/Index view:
<form class="form-register" action="/register/checkregister" method="post">
<img src="/img/logo.png" class="logo">
<label for="name" class="sr-only">Name</label>
<input type="text" id="name" class="form-control" placeholder="Name" required autofocus>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
<label for="retypePassword" class="sr-only">Re-Type Password</label>
<input type="password" id="retypePassword" class="form-control" placeholder="Re-type Password" required>
<br>
<button class="btn btn-lg btn-success btn-block" type="submit">Register</button><br>
Back to Login Page
</form>
I was expecting this to give me access to the $_Post array from within my register/checkRegister method. However when I check the $_Post variable it shows that it has been submitted but with no values.
I am new to development in this way, if anyone could help with what I am doing wrong that would be great.
I tried to check within my checkRegister method in the Register controller by using this code:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo "<pre>";
var_dump($_POST);
echo "</pre>";
$user_name = Request::post('name');
echo 'user_name = '.$user_name . '<br><br>';
echo "POST";
}
But it returns an empty array for Post.
My code is on GitHub:
https://github.com/imoprojects/upbook
Thank you for any assistance,
Ian
you have not added the name attribute to your input tags
add name attribute to every input tag like this
<form class="form-register" action="/register/checkregister" method="post">
<img src="/img/logo.png" class="logo">
<label for="name" class="sr-only">Name</label>
<input type="text" name="name" id="name" class="form-control" placeholder="Name" required autofocus>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" name="inputEmail" id="inputEmail" class="form-control" placeholder="Email address" required>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="inputPassword" id="inputPassword" class="form-control" placeholder="Password" required>
<label for="retypePassword" class="sr-only">Re-Type Password</label>
<input type="password" name="retypePassword" id="retypePassword" class="form-control" placeholder="Re-type Password" required>
<br>
<button class="btn btn-lg btn-success btn-block" type="submit">Register</button><br>
Back to Login Page
</form>

TokenMismatchException in VerifyCsrfToken.php line 46 ocassionally showing

I already set the token in the form:
<form action="{{ route('user.store') }}" method="post">
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
<legend>Agregar nuevo usuario.</legend>
<div class="form-group">
<label>Código empresa</label>
<input type="number" class="form-control input-sm" name="enterprise" id="enterprise">
</div>
<div class="form-group">
<label>Nombre</label>
<input type="text" class="form-control input-sm" name="name" id="name">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control input-sm" name="email" id="email">
</div>
<div class="form-group">
<label>Usuario</label>
<input type="text" class="form-control input-sm" name="username" id="username">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control input-sm" name="password" id="password">
</div>
<div class="form-group">
<label class="checkbox-inline">
<input type="checkbox" name="create_content" id="create_content"> Crea contenido
</label>
<label class="checkbox-inline">
<input type="checkbox" name="active" id="active"> Activo
</label>
</div>
<button type="submit" class="btn btn-sm btn-primary" id="btn_Crear">Create</button>
</form>
Occasionally I'm receiving the TokenmismathException, and I'm not able to post anymore, If I comment out the line //'App\Http\Middleware\VerifyCsrfToken', in the Kernel.php file and try to post, it works, And if I uncomment the same line again 'App\Http\Middleware\VerifyCsrfToken',, now I don't receive the TokenmismatchException, until it stops working.
I'm not using ajax
Does anyone know why this is happening.
We had the exact same problem and never found a good solution. We did find a workaround although.
In your .env file, set the Session storage to Redis (yap, you have to install Redis on your server than). This worked for us, never encountered the same problem again.
Note, this works for us, but it of course is not a solution, merely a work-around until someone found the right solution.

Categories