Dynamic row is not saved in database with Laravel Controller - mysql

I have a form in my view which asks for education qualification of students(I have provided the code below). I want the user (in my case a Student) while filling up the form should be able to dynamically add his/her subjects, total marks and marks obtained in the database table. All thing works fine when i add only one subject. But the problem comes when i try to add multiple subjects. Looking forward for a solution from you people..
This is the form in my view :
<form action="/edu_details" method="post">
#csrf
<div class="jumbotron">
<div class="form-row">
<div class="form-group col-md-12">
<label for="institution">Institution Last Attended</label>
<input type="text" class="form-control" name="institution" id="institution" value="{{old('institution')}}">
#if ($errors->has('institution'))
<div class="error" style="color:red;">{{ $errors->first('institution') }}</div>
#endif
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="yop">Year of Passing</label>
<input type="text" class="form-control" name="yop" id="yop" value="{{old('yop')}}">
#if ($errors->has('yop'))
<div class="error" style="color:red;">{{ $errors->first('yop') }}</div>
#endif
</div>
<div class="form-group col-md-8">
<label for="board">Board</label>
<div class="col-sm-12">
<select name="board" id="board" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>SEBA</option>
<option>CBSE</option>
<option>Other</option>
</select>
</div>
#if ($errors->has('board'))
<div class="error" style="color:red;">{{ $errors->first('board') }}</div>
#endif
</div>
</div>
</div>
<div class="jumbotron">
<table class="table table-bordered">
<thead>
<tr>
<th>Name of the Subject</th>
<th>Total Marks</th>
<th>Marks Obtained</th>
<th>+</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" name="subj[]" class="form-control">
#if ($errors->has('subj'))
<div class="error" style="color:red;">{{ $errors->first('subj') }}</div>
#endif
</td>
<td>
<input type="text" name="totl" class="form-control">
#if ($errors->has('totl'))
<div class="error" style="color:red;">{{ $errors->first('totl') }}</div>
#endif
</td>
<td>
<input type="text" name="obtn" class="form-control">
#if ($errors->has('obtn'))
<div class="error" style="color:red;">{{ $errors->first('obtn') }}</div>
#endif
</td>
<td>
-
</td>
</tr>
</tbody>
</table>
</div>
<div class="jumbotron">
<div class="form-row">
<label for="sub_group">Group of subjects the applicant wishes to opt :</label>
<div class="form-group col-md-12">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sub_group" id="sub_group" value="Major">
<label class="form-check-label" for="sub_group">Major</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sub_group" id="sub_group" value="General">
<label class="form-check-label" for="sub_group">General</label>
</div>
</div>
#if ($errors->has('sub_group'))
<div class="error" style="color:red;">{{ $errors->first('sub_group') }}</div>
#endif
</div>
<div class="form-row">
<div class="col-sm-4">
<label for="pref1">1st Preference</label>
<select name="pref1" id="pref1" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
#if ($errors->has('pref1'))
<div class="error" style="color:red;">{{ $errors->first('pref1') }}</div>
#endif
</div>
<div class="form-group col-md-8">
<label for="prefgroup1"> </label>
<div class="col-sm-12">
<select name="prefgroup1" id="prefgroup1" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
#if ($errors->has('prefgroup1'))
<div class="error" style="color:red;">{{ $errors->first('prefgroup1') }}</div>
#endif
</div>
</div>
</div>
<div class="form-row">
<div class="col-sm-4">
<label for="pref2">2nd Preference</label>
<select name="pref2" id="pref2" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
#if ($errors->has('pref2'))
<div class="error" style="color:red;">{{ $errors->first('pref2') }}</div>
#endif
</div>
<div class="form-group col-md-8">
<label for="prefgroup2"> </label>
<div class="col-sm-12">
<select name="prefgroup2" id="prefgroup2" class="form-control">
<option hidden disabled selected value> -- Select an option -- </option>
<option>Sub1</option>
<option>Sub2</option>
<option>Sub3</option>
</select>
</div>
#if ($errors->has('prefgroup2'))
<div class="error" style="color:red;">{{ $errors->first('prefgroup2') }}</div>
#endif
</div>
</div>
</div>
<button type="submit" class="btn btn-primary btn-block">Submit Data</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(".addRow").on('click', function(){
addRow();
return false;
});
function addRow(){
var tr = '<tr>'+
'<td><input type="text" name="subj[]" class="form-control">#if ($errors->has('subj'))
<div class="error" style="color:red;">{{ $errors->first('subj') }}</div>
#endif </td>'+
'<td><input type="text" name="totl" class="form-control"></td>'+
'<td><input type="text" name="obtn" class="form-control"></td>'+
'<td>-</td>'+
'</tr>';
$('tbody').append(tr);
$('tbody').on('click','.delRow',function(){
$(this).parent().parent().remove();
return false;
});
}
</script>
This is my controller's save method :
public function store(Request $request)
{
$validatedData = $request->validate([
'institution' => 'required|max:255',
'yop' => 'required',
'board' => 'required|max:255',
'subj' => 'required|max:255',
'totl' => 'required',
'obtn' => 'required',
'sub_group' => 'required|max:255',
'pref1' => 'required|max:255',
'prefgroup1' => 'required|max:255',
'pref2' => 'required|max:255',
'prefgroup2' => 'required|max:255',
]);
//Insert student data if validated
$edudetail = new EduDetail();
$edudetail->user_id = Auth::user()->id;
//$edudetail->user_id = DB::table('pictures')->where('user_id', $user_id)->value('id');
$edudetail->institution = $request->input('institution');
$edudetail->yop = $request->input('yop');
$edudetail->board = $request->input('board');
foreach($request->get('subj') as $subj) {
$edudetail->subj[] = $subj;
$edudetail->save();
}
$edudetail->totl = $request->input('totl');
$edudetail->obtn = $request->input('obtn');
$edudetail->sub_group = $request->input('sub_group');
$edudetail->pref1 = $request->input('pref1');
$edudetail->prefgroup1 = $request->input('prefgroup1');
$edudetail->pref2 = $request->input('pref2');
$edudetail->prefgroup2 = $request->input('prefgroup2');
$edudetail->save();
return redirect('/student_dox_upload')->with('success','Education Details saved.');
}
I have tried to see if the values are passed as arrays in case of the subject field using print_r method.. And the values are seen in arrays... that's working fine.. but when i go to save only the last record of the dynamic row gets added.. For example if i add 3 subjects English, Maths and Science.. I want all 3 subjects to be added in a column but only Science is getting added.. Any help is appreciated..

Like the bottom, make a fillable for the input values ​​inside the model
$fillable=['institution',...];
Rules in the file in the request on the path
app \ Http \ Request
Define in a file
And then save the data in the form below
public function store(NewServiceRequest $request,Variable $variable,Variable $variable){
$variable->fill($request->only($variable->getFillable()));
$variable->save();
$variable->variable()->save($variable);
.
.
.
}

I did exactly what you said and it worked for the subjects field... But when I tried to implement your solution with the totl field and obtn field i got an error..
I tried this :
$edudetail = new EduDetail();
$edudetail->user_id = Auth::user()->id;
//$edudetail->user_id = DB::table('pictures')->where('user_id', $user_id)->value('id');
$edudetail->institution = $request->input('institution');
$edudetail->yop = $request->input('yop');
$edudetail->board = $request->input('board');
$edudetail->subj = json_encode($request->get('subj'));
$edudetail->totl = json_encode($request->get('totl'));
$edudetail->obtn = json_encode($request->get('obtn'));
$edudetail->sub_group = $request->input('sub_group');
$edudetail->pref1 = $request->input('pref1');
$edudetail->prefgroup1 = $request->input('prefgroup1');
$edudetail->pref2 = $request->input('pref2');
$edudetail->prefgroup2 = $request->input('prefgroup2');
$edudetail->save();
I got this error :
Illuminate \ Database \ QueryException (22007)
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '["100","100","100","100"]' for column cas_db.edu_details.totl at row 1 (SQL: insert into edu_details (user_id, institution, yop, board, subj, totl, obtn, sub_group, pref1, prefgroup1, pref2, prefgroup2, updated_at, created_at) values (1, Little Flower School, 2004, CBSE, ["Science","Maths","Computer","English"], ["100","100","100","100"], ["72","76","80","78"], General, Sub2, Sub1, Sub3, Sub2, 2019-07-28 11:21:24, 2019-07-28 11:21:24))

Related

How can I pass a select HTML element as a parameter in a onSubmit form call?

I am trying to pass the html elements of a form through the submit function as parameters. I can get correctly the nameInput element with flag #nameInput, but the select element (#skillSelect) is throwing this error:
- error TS2339: Property 'skillSelect' does not exist on type 'MemberFilterComponent'.
Here is my form template. How can I pass the select element to component as I did with the input text?:
<form
[formGroup]="filterMemberForm"
(ngSubmit)="onSubmit(nameInput, skillSelect)"
>
<div class="form-row">
<div class="col-md-3">
<label class="font-weight-bold"
>Name
<input
ngDefaultControl
type="text"
class="form-control"
label="'Name'"
formControlName="name"
placeholder=" Ex: Maria Novillo"
required
id="name"
#nameInput
(change)="mapChipValue(nameInput)"
/></label>
</div>
<div class="col-md-3" *ngIf="skills.length !== 0">
<label class="font-weight-bold">Skills:</label>
<select
id="skillId"
class="form-control"
formControlName="skillId"
#skillSelect
(change)="mapChipValue(skillSelect)"
>
<option value="">-- Select skills --</option>
<option *ngFor="let skill of skills" [value]="skill.idSkill">
{{ skill.skill }}
</option>
</select>
</div>
<div class="form-row">
<div class="col-md-3 mt-5">
<button type="submit" class="btn btn-primary">Apply</button>
</div>
</div>
</form>
In the html file:
<select class='select-option'
#mySelect
(change)='onOptionsSelected(mySelect.value)'>
<option class='option'
*ngFor='let option of dropDownData'
[value]="option.seo_val">{{option.text_val}}</option>
</select>
In the ts file:
onOptionsSelected(value:string){
console.log("the selected value is " + value);
}
why you need pass the "html element"? in filterMemberForm.value you has an object with the values. Really your form should be
<form
[formGroup]="filterMemberForm"
(ngSubmit)="onSubmit(filterMemberForm)"
>
onSubmit(form:FromGroup)
{
if (form.valid)
console.log(form.value)
else
form.markAllAsTouched()
}
if you need the selectOption name always can makes
onSubmit(form:FromGroup)
{
if (form.valid)
{
//if is not multiple select
const selectedItem=this.skills.find(x=>x.idSkill==form.value.skillId)
console.log(form.value,selectedItem)
//if is multiple select
const selectedItems=this.skills.filter(
x=>form.value.skillId.indexOf(x.idSkill)>=0)
console.log(form.value,selectedItems)
}
else
form.markAllAsTouched()
}

How to get the name of the select element from another html field in Angular

I have these two elements (select and a text field) I want to disable the text box if the name the user selects from the drop down is "Other".
ex. clModal.idustryName !== 'Other'
Currently I only know how to get the ceModal.industryId because that is what I'm saving. How can I reference the name?
Here are my two elements
<div class="form-group col-md-4">
<label for="ceIndustry">Industry</label>
<select class="form-control" [(ngModel)]="ceModal.industryId" (ngModelChange)="markDirty()" name="ceIndustry" #ceIndustry="ngModel" required>
<option [ngValue]=null>None selected</option>
<option *ngFor="let industry of industryList" [value]="industry.code">{{industry.name}}</option>
</select>
<div [hidden]="ceIndustry.valid || ceIndustry.pristine" class="alert alert-danger">Industry is required</div>
</div>
<div class="form-group col-md-4">
<label for="ceIndustryOther">Industry Other</label>
<input type="text" class="form-control" placeholder="ex. Other industry info" [(ngModel)]="ceModal.industryOther" (ngModelChange)="markDirty()" name="ceIndustryOther" [disabled]="(clModal.idustryName !== 'Other')">
</div>

How to insert two serial numbers with one insert into

I try to make it possible to lend two books at once my problem is that only one value gets forwarded. The form looks like this:
In the view I tried to rename the field to "serial number2" because I thought that he just ignored the second serial number because of the same name.
This is my View:
<div class="card">
<div class="card-header">{{ __('Lend a book') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('borrow.store') }}">
#csrf
<div class="form-group row">
<label for="serialnumber" class="col-md-4 col-form-label text-md-right">{{ __('Please scan the Serialnumber of the book') }}</label>
<div class="col-md-6">
<input id="serialnumber" type="text" class="form-control{{ $errors->has('serialnumber') ? ' is-invalid' : '' }}" name="serialnumber" value="{{ old('serialnumber') }}" required #if (Session::has('autofocus')) autofocus #endif>
#if ($errors->any())
<div class="alert alert-danger">The book is not in stock.
<ul>
</ul>
</div>
#endif
</div>
</div>
<div class="form-group row">
<label for="serialnumber" class="col-md-4 col-form-label text-md-right">{{ __('') }}</label>
<div class="col-md-6">
<input id="serialnumber" type="text" class="form-control{{ $errors->has('serialnumber') ? ' is-invalid' : '' }}" name="serialnumber" value="{{ old('serialnumber') }}" required #if (Session::has('autofocus')) autofocus #endif>
#if ($errors->any())
<div class="alert alert-danger">The book is not in stock..
<ul>
</ul>
</div>
#endif
</div>
</div>
<div class="form-group row">
<label for="comment" class="col-md-4 col-form-label text-md-right">{{ __('comment') }}</label>
<div class="col-md-6">
<!-- <input id="ma_id" type="text" class="form-control{{ $errors->has('ma_id') ? ' is-invalid' : '' }}" value="{{ old('ma_id') }}" required> -->
<input id="comment" type="text-field" class="form-control" name="comment" placeholder="Test">
#if ($errors->has('comment'))
<span class="invalid-feedback">
<strong>{{ $errors->first('comment') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="ma_id" class="col-md-4 col-form-label text-md-right">{{ __('scan membercard to identify yourself') }}</label>
<div class="col-md-6">
<!-- <input id="ma_id" type="text" class="form-control{{ $errors->has('ma_id') ? ' is-invalid' : '' }}" value="{{ old('ma_id') }}" required> -->
<input id="ma_id" type="password" class="form-control" name="ma_id" required>
#if ($errors->has('ma_id'))
<span class="invalid-feedback">
<strong>{{ $errors->first('ma_id') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('send') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
This is my Controller:
public function store(bookRequest $request)
{
//if( !book::find($request->get('serialnumber'))->exists() ) {
$this->middleware('guest');
request()->validate([
'serialnumber' => 'required',
'serialnumber' => 'required',
'ma_id' => 'required'
]);
book::create($request->all());
return redirect()->route('book.index');
}
There is no error but the problem is that he writes only one serial number into the database as described above and completely ignores the other one that comes into the second field. My question is what I have to adjust so that the second serial number with the same data can be written into the database.
You cannot have two items with the same name (for example serialnumber) in the same form, otherwise only one value is going to be processed. Please check the inspection tool in the browser. If you need multiple inputs with the same "name" append [] to the name (for example name=serialnumber[]).
More info at HTML 5 spec
In the other hand, you have multiple items with the same ID. That's another issue to fix in some point.
Once you have your data in the controller you will have to generate your insert data accordingly
$requestData = $request->validated();
$data = [
[
'serialnumber' => $requestData['serialnumber'][0],
'comment' => $requestData['comment'],
'ma_id' => $requestData['ma_id']
],
[
'serialnumber' => $requestData['serialnumber'][1],
'comment' => $requestData['comment'],
'ma_id' => $requestData['ma_id']
]
];
Book::insert($data);
Make input's name as array.
Replace name="serialnumber" with name="serialnumber[]". When form is submitted, you will receive an array with both serial numbers.

Add value to select tag - HTML

I'm working on a Laravel application. In my view i have a form, what I'm trying to do is when submitting the form and one field is not valid, it returns the values of valid fields, so the user will have to edit the wrong field only, not the full form.
This is my form and an example. In here, the user didn't enter the url, so it shows an error message and return the values of the correct fields (client, domain provider, etc...) The problem i'm facing is this is not working for the avatar field and service field!
I'm returning the value by adding value="{{ old('client') }}" to the input tag , as shown in code below:
<div class="form-group row">
<label class="col-lg-3 col-form-label">Client:</label>
<div class="col-lg-9">
<input type="text" class="form-control" name="client" value="{{ old('client') }}">
<small class="error">{{$errors->first('client')}}</small>
</div>
</div>
QUESTION: How to add it for avatar and select service fields?
Avatar: - currently its not returning the value
<div class="form-group row">
<label class="col-lg-3 col-form-label">Attach avatar:</label>
<div class="col-lg-9">
<input type="file" class="form-input-styled" data-fouc="" name="avatar" value="{{ old('avatar') }}">
<small class="error">{{$errors->first('avatar')}}</small>
</div>
</div>
Select service:
<div class="form-group row">
<label class="col-lg-3 col-form-label">Select Service:</label>
<div class="col-lg-9">
<select data-placeholder="Select service" class="form-control form-control-select2" data-fouc="" name="service" >
<option></option>
<option value="Website">Website</option>
<option value="Application">Application</option>
</select>
<small class="error">{{$errors->first('service')}}</small>
</div>
</div>
For the select tag ,
you must put a condition for every option tag ,
if the last submitted value is equal to this option value put selected
attribute to this option
something like that ....
<div class="form-group row">
<label class="col-lg-3 col-form-label">Select Service:</label>
<div class="col-lg-9">
<select data-placeholder="Select service" class="form-control form-control-select2" data-fouc="" name="service" >
<option></option>
<option value="Website" {{ old('service') == "Website" ? "selected" : ""}}>Website</option>
<option value="Application" {{ old('service') == "Application" ? "selected" : "">Application</option>
</select>
<small class="error">{{$errors->first('service')}}</small>
</div>
</div>
hi for select tag this work for me
<div class="form-group row">
<label class="col-lg-3 col-form-label">Select Service:</label>
<div class="col-lg-9">
<select data-placeholder="Select service" class="form-control form-control-select2" data-fouc="" name="service" >
<option></option>
#foreach($Listservice as $key)
<option value ="{{$key->id}}"> {{$key->Website}}
{{$key>Application}}</option>
#endforeach
</select>
</div>
</div>
and in your model add these
$listservice = Service::get();
i hope it s help

Fullcalendar on Symfony add/modify events through modal and send back Json file to Database

I am working on a project with Symfony 2.8. My main goal is to create a dynamic calendar based on Fullcalendar library.
I add my events called "dispos" (avalabilities in English) and "Rdvs" (appointments" in English) through a Json request and ajax. This works fine.
Now, I would like to transform availabilites into appointements (which are both considered as events in Fullcalendar).
E.g : When someone clicks on one availability a modal shows up, then the person fills the form in it and clicks "save" button.
When the "save" button is clicked, all informations entered in the form are sent and saved (through a Json request) into my Database and the appointment is taken
--> all events of the current should be reloaded through ajax, the event should be displayed with the title of the event entered (name of the patient) and the modal should contain all informations given/wrote before "save" action.
I tried to do it but my ajax is not working since events do not reload after saving everything else is working.
Anyway, I think I did it wrong somewhere. The code I will show you in my Controller returns a view because I didn't manage to return a response (+ I think routing or something is bad but don't know how to fix it...)
Any clue or advice woud be really appreciated :)
So here is my code :
TakeRdvAction in my controller :
/* ----------------- /
/ --- TAKE RDV ---- /
/ ----------------- */
public function takeRdvAction(){
$request = $this->get('request_stack')->getCurrentRequest();
parse_str($request->getContent(), $myArray);
/*$request->isXmlHttpRequest()*/
if (1) {
$dateHeureDispo=$myArray['heureDispo'];
$dateDispo= new \DateTime($dateHeureDispo);
$heureDispo = $dateDispo->format('H:i');
$dateDispo=$dateDispo->format('d-m-Y');
$civilite=$myArray['civilite'];
$nom=$myArray['inputNom'];
$prenom=$myArray['inputPrenom'];
$naissance=$myArray['birth'];
$email=$myArray['email'];
$tel=$myArray['tel'];
$telFixe=$myArray['telFixe'];
$adresse=$myArray['adresse'];
$cp=$myArray['cp'];
$ville=$myArray['ville'];
$pays=$myArray['pays'];
$medecin_traitant=$myArray['medecin_traitant'];
$ame=$myArray['ame'];
$cmu=$myArray['cmu'];
$takeRDv="http://connect.mysite.com/nameofapi2/takeappt?center=13&motive=238&prenom=".urlencode($prenom)."&nom=".urlencode($nom)."&email=".urlencode($email)."&birthdate=".$naissance."&address=".urlencode($adresse)."&cp=".$cp."&city=".urlencode($ville)."&country=".urlencode($pays)."&tel=".$tel."&titre=1&source=1&origine=1&daterdv=".$dateDispo."&time=".$heureDispo."&slot=1%E1%90%A7&civilite=".$civilite."&origin=smwh&referer=1";
$streamContext = stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
]);
$json = file_get_contents($takeRDv, false, $streamContext);
$response = new jsonResponse();
$response->setContent($json);
return $this->indexAction();
}
else {
return new response("Ajax request failed");
}
}
If I put if ($request->isXmlHttpRequest()), the controller goes directly to "else" end returns "Ajax request failed"
Ajax.js file (It's the last ajax function we are talking about):
$(document).ready(function () {
/* TakeRdvs */
$("#monBouton").click(function(){
if (nom.value != "" && prenom.value != "" && email.value != "")
{
$.ajax({
url: "{{ path('takeRdv') }}",
method: 'POST',
data: {},
success: function(data) {
$("#calendarModal").modal('hide');
$("#calendarModal").on('hidden.bs.modal', function (e) {
$('#calendar').fullCalendar('refetchEvents');
});
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert('Error: ' + errorThrown);
}
});
}
else if (nom.value == "")
{
alert('Veuillez renseigner le Nom');
return false;
}
else if (prenom.value == "")
{
alert('Veuillez renseigner le prénom');
return false;
}
else if (email.value == "")
{
alert("Veuillez renseigner l'adresse mail");
return false;
}
});
});
Other ajax functions work just fine, I made them after trying to take an appointment on an availability. When I implemented FosJsRouting, I thought it would be easier to try to make my takeRdvs action work. But the truth is, I don't know how to do it since it's a different action from the others and I am lost now :'(
My modal showing up when a event is clicked (got cut in several part sorry could not fix it):
×
close
<div class="form-group">
<div class="col-sm-12">
<h4 id="modalTitle" class="modal-title modify"></h4>
</div>
</div>
<div class="col-sm-4 form-group">
<label for="motif">
Motif de la consultation :
</label>
<select class="form-control" id="motif" data-placeholder="Choisissez un motif" style="width: 100%;" name="motif"> {# multiple data-max-options="1" #}
<option value="238"> Bilan de la vue</option>
<option value="Visite de controle"> Visite de contrôle</option>
<option value="Chirurgie réfractive"> Chirurgie réfractive</option>
<option value="Rééducation visuelle"> Rééducation visuelle</option>
<option value="Urgences"> Urgences</option>
</select>
</div>
<div class="form-group create">
<div class="col-sm-2">
<label class="control-label" for="civilite">Civilité</label>
<select class="custom-select" id="civilite" name="civilite">
<option value="Mme">Mme</option>
<option value="M">M.</option>
</select>
</div>
<div class="col-sm-5">
<label class="control-label" for="inputNom">Nom</label>
<input name="inputNom" type="text" class="form-control" id="inputNom" placeholder="Doe" required >
</div>
</div>
<div class="form-group">
<div class="col-sm-5 create">
<label class="control-label" for="inputPrenom">Prénom</label>
<input name="inputPrenom" type="text" class="form-control" id="inputPrenom" placeholder="Jane" required >
</div>
</div>
<div class="form-group">
<div class="col-sm-6">
<label class="control-label" for="email">Email</label>
<input name="email" type="email" class="form-control" id="email" placeholder="jane.doe#example.com" required >
</div>
</div>
{# fin de la condition #}
<div class="form-group">
<div class="col-sm-6">
<label class="control-label" for="naissance">Date de naissance</label>
<input name="birth" type="text" class="form-control" id="naissance" placeholder="01-01-2001" required>
</div>
<div class="col-sm-6">
<label class="control-label" for="tel">Mobile</label>
<input name="tel" type="tel" class="form-control" id="tel" placeholder="0607080910" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-6">
<label class="control-label" for="telFixe">Téléphone fixe</label>
<input name="telFixe" type="tel" class="form-control" id="telFixe" placeholder="0101010101">
</div>
</div>
<div class="form-group">
<div class="col-sm-5">
<label class="control-label" for="adresse">Adresse</label>
<input name="adresse" type="text" class="form-control" id="adresse" placeholder="1 Bd de Strasbourg 83000 Toulon" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-3">
<label class="control-label" for="cp">Code postal</label>
<input name="cp" type="text" class="form-control" id="cp" placeholder="83000" required>
</div>
</div>
<div class="form-group">
<div class="form-group">
<div class="col-sm-4">
<label class="control-label" for="ville">Ville</label>
<input name="ville" type="text" class="form-control" id="ville" placeholder="Toulon" required>
</div>
</div>
</div>
<div class="form-group">
<div class="form-group">
<div class="col-sm-4">
<label class="control-label" for="pays">Pays</label>
<input name="pays" type="text" class="form-control" id="pays" placeholder="France" required>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-6">
<label class="control-label" for="medecin_traitant">Médecin traitant</label>
<input name="medecin_traitant" type="text" class="form-control" id="medecin_traitant" placeholder="Dr Bicharzon" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-6">
<label class="control-label" for="ame">
Bénéficiare de l'AME ?
</label>
<select class="form-control" name="ame" title="ame" id="ame" required>
<option value="oui">Oui</option>
<option value="non">Non</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-6">
<label class="control-label" for="cmu">
Bénéficiare de la CMU ?
</label>
<select class="form-control" name="cmu" title="cmu" id="cmu" required>
<option value="oui">Oui</option>
<option value="non">Non</option>
</select>
</div>
</div>
<input title="heureDispo" class="visually-hidden form-control" name="heureDispo" type="text" id="heureDispo">
<div class="form-group boutonsModale col-sm-6">
<button type="button" class="btn btn-default btn-danger" data-dismiss="modal">Annuler</button>
<button type="submit" class="btn btn-primary" id="monBouton">Enregistrer</button>
</div>
</form>
</div>
{#{% endfor %}#}
<div class="modal-footer paddingTop">
{#<button type="button" class="btn btn-default btn-danger" data-dismiss="modal">Annuler</button>#}
{#<input type="submit" class="btn btn-primary">Enregistrer</input>#}
{#<button type="button" class="btn btn-default" data-dismiss="modal">Fermer</button>#}
</div>
</div>
</div>
</div>
</div>
Routing.yml :
Take RDV
take_rdv:
path: /prise-rdv
defaults: {_controller: RdvProBundle:Default:takeRdv}
methods: [POST]
options:
expose: true
I don't know how to change the route if I need to... + I would like the route no to show like the other routes I created but as it's coded now, it's shown...
I am junior junior as dev so I a sorry if my code is not clean :s
Thank you in advance for all the help you will provide.
It is huge. I'm not sure about your problem(s?) but if I understand :
First problem :
ajax is not working since events do not reload
If your #button is replaced in your page after your first call, the attached event is destroyed. Change your listener :
$("#monBouton").click(function(){
by
$('body').on('click', '#monBouton', function () { will solve the problem.
Second problem :
If I put if ($request->isXmlHttpRequest()), the controller goes directly to "else"
I suggest to pass $request as argument of your action and just put your condition within an if statement :
public function takeRdvAction(Request $request)
{
if ($request->isXmlHttpRequest()) {
[...]
}
}
Thirdly :
To use FosJsRouting, you exposed your route in your yaml. That's good. To use it in javascript, you have to include the given script in your base.html.twig and use Routing.generate just as defined in the doc :
$.ajax({
url: Routing.generate('take_rdv', {/* $(yourform).serialize() ?*/}),
method: 'POST',
data: {},
success: function(data) {
$("#calendarModal").modal('hide');
$("#calendarModal").on('hidden.bs.modal', function (e) {
$('#calendar').fullCalendar('refetchEvents');
});
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert('Error: ' + errorThrown);
}
});
Update
With my suggestions, you've to change how you use $request in your action :
$myArray = $request->request->all();
$civilite=$myArray['civilite'];
[...and so on...]
Bonus : Symfony is a powerfull framework. I suggest you to learn about using this framework and especially, in your case, about Forms
enjoy :)
UPDATE 2
if ($request->isXmlHttpRequest()) { is never true cause you are not doing an ajax call. I just see that, but your button is of type submit then, your browser send a basic HTTP request.
Add this to your js code :
$('body').on('click', '#monBouton', function (event) {
event.preventDefault();
[...$.ajax blablabla]
});