including the appended variable to the data with other variable in ajax - ajax

Hello and good day everyone, I am having a problem in ajax. I have a variable named fd that contains object. But i cant find a way to put it together with other variable in "data:". I just want to pass the data of fd and also the other variables from other inputs of my form. Thank you very much.
$('#register_btn').click(function(e) {
var fd = new FormData();
var files = $('#image')[0].files[0];
fd.append('image',files);
var val1 = $('#email').val();
var val2 = $('#firstname').val();
var val3 = $('#middlename').val();
var val4 = $('#lastname').val();
var val5 = $('#emp_status').val();
var val6 = $('input[name=date_hired]').val();
var val7 = $('#account_type').val();
var val8 = $('#notes').val();
var val9 = $('#position').val();
var val10 = $('#cp_number').val();
var val11 = $('#tel_number').val();
var val12 = $('#address').val();
$.ajax({
type: 'POST',
url: 'process/add_employee_process.php',
contentType: false,
cache: false,
processData:false,
data: {email: val1, firstname: val2, middlename: val3, lastname: val4, emp_status: val5, date_hired: val6, account_type: val7, notes: val8, position: val9, cp_number: val10, tel_number: val11, address: val12,fd},
success:function(html) {
if(html=="Failed"){
$("#gifcheckmark").attr('src','../campfire.gif');
$("#gifcheckmark").css('display','inline');
$("#gifcheckmark").css('width','inline');
$("#notif_message").text("Failed");
$("#register_btn").css('display','none');
$("#decline_btn").css('display','none');
}
else{
$("#notif_message").text("Success");
$("#gifcheckmark").css('display','inline');
$("#gifcheckmark").css('width','inline');
$("#register_btn").css('display','none');
$("#decline_btn").css('display','none');
setInterval(successGif, 1600);
$('#qr').load('../phpqrcode/index.php');
$('#qr').show();
}
}
});
});
This is the form
<form role="form" style="width: 100%;">
<div class="box-body">
<div class="box-header with-border col-xs-12">
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-remove"></i></button>
</div>
</div>
<div class="form-group col-md-3">
<label for="">Email </label>
<input type="email" id="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
</div>
<div class="form-group col-md-3">
<label for="">Firstname </label>
<input type="text" id="firstname" class="form-control" id="" placeholder="Firstname">
</div>
<div class="form-group col-md-3">
<label for="">Middlename </label>
<input type="text" id="middlename" class="form-control" id="" placeholder="Middlename">
</div>
<div class="form-group col-md-3">
<label for="">Lastname </label>
<input type="text" id="lastname" class="form-control" id="" placeholder="Lastname">
</div>
<div class="form-group col-md-3">
<label for="">Employee Status </label>
<input type="text" id="emp_status" class="form-control" id="" placeholder="Employee Status">
</div>
<div class="form-group col-md-3">
<label for="">Date Hired </label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="text" name="date_hired" class="form-control pull-right" id="datepicker">
</div>
</div>
<div class="form-group col-md-3">
<label for="">Account Type </label>
<input type="text" id="account_type" class="form-control" id="" placeholder="Account Type">
</div>
<div class="form-group col-md-3">
<label for="">Notes </label>
<input type="text" id="notes" class="form-control" id="" placeholder="Notes">
</div>
<div class="form-group col-md-3">
<label for="">Positions </label>
<input type="text" id="position" class="form-control" id="" placeholder="Positions">
</div>
<div class="form-group col-md-3">
<label for="">Cellphone Number </label>
<input type="text" id="cp_number" class="form-control" id="" placeholder="Cellphone Number">
</div>
<div class="form-group col-md-3">
<label for="">Telephone Number </label>
<input type="text" id="tel_number" class="form-control" id="" placeholder="Telephone Number">
</div>
<div class="form-group col-md-3">
<label for="">Address </label>
<input type="text" id="address" class="form-control" id="" placeholder="Address">
</div>
<div class="form-group col-xs-12">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" id="image" name="image">
</div>
</div>
<!-- /.box-body -->
<div class="col-xs-2">
<center><button type="button" id="confirm_btn" class="btn btn-primary" data-toggle="modal" data-target="#modal-default">
Submit
</button></center>
</div>
</form>
This is the add_employee_process.php
$db = mysqli_connect('localhost', 'root','','cjwebsolutions');
$image = $_FILES['image']['name'];
$target = "image/".basename($image);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
$emp_id = make_emp_id($_POST['date_hired']);
$_POST['emp_id'] = $emp_id;
// Required field names
$required_fields = array('email','firstname','middlename','lastname', 'emp_status', 'date_hired','account_type','notes','position','cp_number','tel_number','address','emp_id');
$data_insert_fields = array('email','firstname','middlename','lastname', 'emp_status', 'date_hired','account_type','notes','position','cp_number','tel_number','address','emp_id');
if (check_empty_fields($required_fields)) {
echo "Failed";
}
else
{
data_insert($data_insert_fields,"cj_accounts");
}

Related

Laravel - Display Image in Modal while editing

I am a beginner of laravel. I want to edit a record which contains image, but it just show the input type for file same as insert records, instead of displaying the image. I have no ideas how to make it. Any comments are appreciated. Thanks.
Modal
<div class="modal-body">
<form method="POST" id="memberCardForm" name="memberCardForm" class="form-horizontal" enctype="multipart/form-data">
#csrf
<input type="hidden" name="id" id="id">
<div class="form-group">
<label for="">Card Type</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="card_type" name="card_type" maxlength="50" required>
</div>
</div>
<div class="form-group">
<label for="">Description</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="card_desc" name="card_desc" maxlength="50" required>
</div>
</div>
<div class="form-group">
<label for="">Entitlement</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="card_entitlement" name="card_entitlement" maxlength="50">
</div>
</div>
<div class="form-group">
<label for="">Card Image</label>
<div class="col-sm-12">
<input type="file" class="form-control" id="image" name="image" required>
</div>
</div>
<div class="modal-footer">
Close
<button type="submit" id="saveBtn" class="btn btn-success">Save</button>
</div>
</form>
</div>
JS for Edit record
//Edit
$('body').on('click', '.editRecord', function() {
var id = $(this).data('id');
$.get("{{ url('membercards/edit') }}" + '/' + id, function(data) {
$('#exampleModalLabel').html("Edit Member Card");
$('#saveBtn').html('Save');
$('#exampleModal').modal('show');
$('#id').val(data.id);
$('#card_type').val(data.card_type);
$('#card_desc').val(data.card_desc);
$('#card_entitlement').val(data.card_entitlement);
$('#image').val(data.image);
})
});

In the contact creation I want to assign a customer or supplier from a dropdown

I have two problems.
1. Problem: When I add my selection to the razor page all required fields validation from the model do not work anymore.
Validation from model without selection
No Validation when I add my selection
When dropdown are in the Razor html no validation works and it crashes with NULL Values which I want to prevent. Like it is without select options.
Why is that?
2. Problem: When I select a customer from my drop down (CustomerDropDown.CusId) I would like to fill the ID into my ID field of contacts (contacts.CustomerId). How can I reach the selected value from dropdown and post it to my contact contacts.CustomerId?
== c# code ==
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.Rendering;
using WorkCollaboration.Data;
using WorkCollaboration.Models;
namespace WorkCollaboration.Pages.Contacts
{
public class CreateModel : PageModel
{
private readonly WorkCollaboration.Data.WorkCollaborationContext _context;
public CreateModel(WorkCollaboration.Data.WorkCollaborationContext context)
{
_context = context;
}
public async Task<IActionResult> OnGetAsync()
{
CustomerDropDownDisp = await _context.CustomerDropDown.ToListAsync(); // Added for DropDown
SupplierDropDownDisp = await _context.SupplierDropDown.ToListAsync(); // Added for DropDown
return Page();
}
[BindProperty]
public Contact Contact { get; set; }
public IEnumerable<CustomerDropDown> CustomerDropDownDisp { get; set; }
public IEnumerable<SupplierDropDown> SupplierDropDownDisp { get; set; }
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Contact.Add(Contact);
await _context.SaveChangesAsync();
return RedirectToPage("/ContactsOverview/Index");
}
}
}
== Razor Page Code ==
#page
#using WorkCollaboration.Models
#model WorkCollaboration.Pages.Contacts.CreateModel
#{
ViewData["Title"] = "Create";
ViewData["RandomId"] = Guid.NewGuid().GetHashCode();
}
<h1>Create</h1>
<h4>Contact</h4>
<p>
<a asp-page="/ContactsOverview/Index">Back to Index</a>
</p>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Contact.ContactId" class="control-label"></label>
<input asp-for="Contact.ContactId" value='#ViewData["RandomId"]' readonly="readonly" class="form-control" />
<span asp-validation-for="Contact.ContactId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.LastName" class="control-label"></label>
<input asp-for="Contact.LastName" class="form-control" />
<span asp-validation-for="Contact.LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.FirstName" class="control-label"></label>
<input asp-for="Contact.FirstName" class="form-control" />
<span asp-validation-for="Contact.FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.CustomerId" class="control-label"></label>
</div>
<select id="CusId" asp-for="CustomerDropDownDisp" asp-items="#(new SelectList(Model.CustomerDropDownDisp,"CusId","CusName"))">
<option value="">--Choose Customer--</option>
<option value="1" selected>None</option>>
</select>
<div class="form-group">
<input asp-for="Contact.CustomerId" class="form-control" />
<span asp-validation-for="Contact.CustomerId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.SupplierId" class="control-label"></label>
</div>
<select id="SupId" asp-for="SupplierDropDownDisp" asp-items="#(new SelectList(Model.SupplierDropDownDisp,"SupId","SupName"))">
<option value="">--Choose Supplier--</option>
<option value="1" selected>None</option>>
</select>
<div class="form-group">
<input asp-for="Contact.SupplierId" class="form-control" />
<span asp-validation-for="Contact.SupplierId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.PrivateStreet" class="control-label"></label>
<input asp-for="Contact.PrivateStreet" class="form-control" />
<span asp-validation-for="Contact.PrivateStreet" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.PrivateStreetNo" class="control-label"></label>
<input asp-for="Contact.PrivateStreetNo" class="form-control" />
<span asp-validation-for="Contact.PrivateStreetNo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.PrivateStreetAdditionalInfo" class="control-label"></label>
<input asp-for="Contact.PrivateStreetAdditionalInfo" class="form-control" />
<span asp-validation-for="Contact.PrivateStreetAdditionalInfo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.PrivateZip" class="control-label"></label>
<input asp-for="Contact.PrivateZip" class="form-control" />
<span asp-validation-for="Contact.PrivateZip" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.PrivateTown" class="control-label"></label>
<input asp-for="Contact.PrivateTown" class="form-control" />
<span asp-validation-for="Contact.PrivateTown" class="text-danger"></span>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Contact.PrivateCountry, htmlAttributes: new { #class = "form-group" })
<div class="form-group">
#Html.DropDownListFor(model => model.Contact.PrivateCountry, new List<SelectListItem>
{
new SelectListItem {Text = "CH", Value = "CH", Selected = true },
new SelectListItem {Text = "D", Value = "D" },
new SelectListItem {Text = "FL", Value = "FL" },
}, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Contact.PrivateCountry, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<label asp-for="Contact.PrivatePhone" class="control-label"></label>
<input asp-for="Contact.PrivatePhone" class="form-control" />
<span asp-validation-for="Contact.PrivatePhone" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.BusinessPhone" class="control-label"></label>
<input asp-for="Contact.BusinessPhone" class="form-control" />
<span asp-validation-for="Contact.BusinessPhone" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.MobilePhone" class="control-label"></label>
<input asp-for="Contact.MobilePhone" class="form-control" />
<span asp-validation-for="Contact.MobilePhone" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.Mail" class="control-label"></label>
<input asp-for="Contact.Mail" class="form-control" />
<span asp-validation-for="Contact.Mail" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
Back to List
</div>
</form>
</div>
</div>
Thank you for your help on how to get selected value (dropdown) to the corresponding contacts Id field.
Here is a demo:
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Contact.ContactId" class="control-label"></label>
<input asp-for="Contact.ContactId" value='#ViewData["RandomId"]' readonly="readonly" class="form-control" />
<span asp-validation-for="Contact.ContactId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.LastName" class="control-label"></label>
<input asp-for="Contact.LastName" class="form-control" />
<span asp-validation-for="Contact.LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.FirstName" class="control-label"></label>
<input asp-for="Contact.FirstName" class="form-control" />
<span asp-validation-for="Contact.FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.CustomerId" class="control-label"></label>
</div>
<select id="CusId" asp-for="CustomerDropDownDisp" asp-items="#(new SelectList(Model.CustomerDropDownDisp,"CusId","CusName"))">
<option value="">--Choose Customer--</option>
<option value="1" selected>None</option>>
</select>
<div class="form-group">
<input asp-for="Contact.CustomerId" class="form-control" />
<span asp-validation-for="Contact.CustomerId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.SupplierId" class="control-label"></label>
</div>
<select id="SupId" asp-for="SupplierDropDownDisp" asp-items="#(new SelectList(Model.SupplierDropDownDisp,"SupId","SupName"))">
<option value="">--Choose Supplier--</option>
<option value="1" selected>None</option>>
</select>
<div class="form-group">
<input asp-for="Contact.SupplierId" class="form-control" />
<span asp-validation-for="Contact.SupplierId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.PrivateStreet" class="control-label"></label>
<input asp-for="Contact.PrivateStreet" class="form-control" />
<span asp-validation-for="Contact.PrivateStreet" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.PrivateStreetNo" class="control-label"></label>
<input asp-for="Contact.PrivateStreetNo" class="form-control" />
<span asp-validation-for="Contact.PrivateStreetNo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.PrivateStreetAdditionalInfo" class="control-label"></label>
<input asp-for="Contact.PrivateStreetAdditionalInfo" class="form-control" />
<span asp-validation-for="Contact.PrivateStreetAdditionalInfo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.PrivateZip" class="control-label"></label>
<input asp-for="Contact.PrivateZip" class="form-control" />
<span asp-validation-for="Contact.PrivateZip" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.PrivateTown" class="control-label"></label>
<input asp-for="Contact.PrivateTown" class="form-control" />
<span asp-validation-for="Contact.PrivateTown" class="text-danger"></span>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Contact.PrivateCountry, htmlAttributes: new { #class = "form-group" })
<div class="form-group">
#Html.DropDownListFor(model => model.Contact.PrivateCountry, new List<SelectListItem>
{
new SelectListItem {Text = "CH", Value = "CH", Selected = true },
new SelectListItem {Text = "D", Value = "D" },
new SelectListItem {Text = "FL", Value = "FL" },
}, new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Contact.PrivateCountry, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<label asp-for="Contact.PrivatePhone" class="control-label"></label>
<input asp-for="Contact.PrivatePhone" class="form-control" />
<span asp-validation-for="Contact.PrivatePhone" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.BusinessPhone" class="control-label"></label>
<input asp-for="Contact.BusinessPhone" class="form-control" />
<span asp-validation-for="Contact.BusinessPhone" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.MobilePhone" class="control-label"></label>
<input asp-for="Contact.MobilePhone" class="form-control" />
<span asp-validation-for="Contact.MobilePhone" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Contact.Mail" class="control-label"></label>
<input asp-for="Contact.Mail" class="form-control" />
<span asp-validation-for="Contact.Mail" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
Back to List
</div>
</form>
</div>
</div>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<script>
$("#CusId").on("change", function () {
$("#Contact_CustomerId").val($("#SupId").val());
});
$("#SupId").on("change", function () {
$("#Contact_SupplierId").val($("#SupId").val());
});
</script>
result:

How to send some data and image from AJAX to Controller in Laravel?

I'm a freshmen in the office and I was assigned by my boss to fix the code of the website that the developer is not in the company anymore. He used mostly AJAX to do things which I'm not used to it. I rarely use AJAX in my project, so I'm very very new about this.
From the old code, I'm trying to send an image with some data from an input form. I can console.log(form_data) to see its values, but I don't know why the content of image cannot be sent to Controller. It's getting 'null' and I can't store it.
Here is the code in input form
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">{{$head}}</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="input-form" class="needs-validation" novalidate method="POST" enctype="multipart/form-data" name="input-form">
{{ csrf_field() }}
<div class="form-horizontal">
<div class="form-group required">
<label class="col-sm-12 control-label" for="thai_name">{{trans('home.thai_name')}}</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="thai_name" name="thai_name" value="{{isset($user) ? $user->thai_name : ''}}" required/>
</div>
</div>
<div class="form-group required">
<label class="col-sm-12 control-label" for="eng_name">{{trans('home.eng_name')}}</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="eng_name" name="eng_name" value="{{isset($user) ? $user->eng_name : ''}}" required/>
</div>
</div>
<div class="form-group required">
<label class="col-sm-12 control-label" for="email">{{trans('home.email')}}</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="email" name="email" value="{{isset($user) ? $user->email : ''}}" required/>
</div>
</div>
<div class="form-group required">
<label class="col-sm-12 control-label" for="password">{{trans('home.password')}}</label>
<div class="col-sm-12">
<input type="password" id="password" class="form-control" required/>
</div>
</div>
<div class="form-group required">
<label class="col-sm-12 control-label" for="password_confirmation">{{trans('home.password_confirmation')}}</label>
<div class="col-sm-12">
<input type="password" id="password_confirmation" class="form-control" required/>
</div>
</div>
<div class="form-group col-sm-12">
<a class="btn-link" id="message" style="text-align: center;"></a>
</div>
<div class="form-group required">
<label class="col-sm-12 control-label" for="phone">{{trans('home.phone')}}</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="phone" name="phone" value="{{isset($user) ? $user->telephone : ''}}" required/>
</div>
</div>
<div class="form-group">
<label class="col-sm-12 control-label" for="fax">{{trans('home.fax')}}</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="fax" name="fax" value="{{isset($user) ? $user->fax : ''}}"/>
</div>
</div>
<div class="form-group required">
<label class="col-sm-12 control-label">{{trans('home.system_role')}}</label>
<div class="col-sm-12">
<div class="form-check form-check-inline">
<input class="form-check-input role" type="radio" id="admin" name="role" value="1"
{{isset($user) && $user->system_role_id == 1 ? 'checked' : ''}} required>
<label class="form-check-label" for="admin">ผู้ดูแลระบบ</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input role" type="radio" id="content_admin" name="role" value="2"
{{isset($user) && $user->system_role_id == 2 ? 'checked' : ''}} required>
<label class="form-check-label" for="content_admin">ผู้ดูแลข้อมูล</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input role" type="radio" id="user" name="role" value="3"
{{isset($user) && $user->system_role_id == 3 ? 'checked' : ''}} required>
<label class="form-check-label" for="user">ผู้ใช้งาน</label>
</div>
</div>
</div>
<div class="form-group required">
<label class="col-sm-12 control-label">{{trans('home.faction')}}</label>
<div class="col-sm-12">
<select id="faction" class="form-control">
#foreach($factions as $faction)
<option value="{{$faction->id}}" {{isset($user) && $user->faction_id == $faction->id ? 'selected' : ''}}>
{{trans('home.'.$faction->name)}}
</option>>
#endforeach
</select>
</div>
</div>
<div class="form-group required">
<label class="col-sm-12 control-label">{{trans('home.position')}}</label>
<div class="col-sm-12">
<select id="position" class="form-control">
#foreach($positions as $position)
<option value="{{$position->id}}" {{isset($user) && $user->position_id == $position->id ? 'selected' : ''}}>
{{trans('home.'.$position->name)}}
</option>>
#endforeach
</select>
</div>
</div>
<div class="form-group required">
<div class="col-sm-12">
<label class="control-label" for="photo">รูปถ่าย</label>
<input type="file" class="form-control-file" id="photo" name="photo" accept="image/*" required/>
</div>
</div>
<div class="form-group">
<label class="col-sm-12 control-label">{{trans('home.acting')}} / {{trans('home.past')}}</label>
<div class="col-sm-12">
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="acting" name="acting" {{isset($user) && $user->is_acting ? 'checked' : ''}}>
<label class="form-check-label" for="acting">{{trans('home.acting')}}</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="past" name="past" {{isset($user) && $user->is_past ? 'checked' : ''}}>
<label class="form-check-label" for="past">{{trans('home.past')}}</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-12" for="start">{{trans('home.start')}}</label>
<div class="col-sm-12">
<div class="input-group" id="start" name="start">
<input type="text" class="form-control" readonly="readonly"/>
<div class="input-group-append">
<span class="input-group-text input-group-addon">
<span class="fa fa-calendar"></span>
</span>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-12" for="end">{{trans('home.end')}}</label>
<div class="col-sm-12">
<div class="input-group" id="end" name="end">
<input type="text" class="form-control" readonly="readonly"/>
<div class="input-group-append">
<span class="input-group-text input-group-addon">
<span class="fa fa-calendar"></span>
</span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="row col-sm-12 text-center">
<input type="button" class="btn btn-default" value="{{trans('home.save')}}"
onclick="{{$func}}"
/>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$('#start').datetimepicker({
sideBySide: true,
locale: 'th',
format: 'YYYY-MM-DD',
ignoreReadonly: true,
defaultDate: "{{isset($user) ? $user->start : ''}}"
});
$('#end').datetimepicker({
sideBySide: true,
locale: 'th',
format: 'YYYY-MM-DD',
ignoreReadonly: true,
defaultDate: "{{isset($user) ? $user->end : ''}}"
});
$('#password, #password_confirmation').on('keyup', function () {
if ( $('#password').val() == $('#password_confirmation').val() ) {
if ( $('#password').val() != '') {
$('#message').html('{{trans("home.password_match")}}').css('color', 'green');
} else {
$('#message').html('{{trans("home.password_null")}}').css('color', 'red');
$('#password').removeClass('valid').addClass('invalid');
$('#password_confirmation').removeClass('valid').addClass('invalid');
}
} else {
$('#message').html('{{trans("home.password_mismatch")}}').css('color', 'red');
$('#password').removeClass('valid').addClass('invalid');
$('#password_confirmation').removeClass('valid').addClass('invalid');
}
});
});
</script>
Here is the code in user.js where I'm using AJAX to send form_data contains image and some data
function addUser() {
var validate = validateUser("add");
if (validate.error == '') {
$('#modal1').modal({backdrop: "static"});
var form_data = {
thai_name: validate.thai_name,
eng_name: validate.eng_name,
email: validate.email,
password: validate.password,
password_confirmation: validate.password_confirmation,
telephone: $('#phone').val(),
fax: $('#fax').val(),
system_role_id: $('input[name=role]:checked').val(),
faction_id: $('#faction').val(),
position_id: $('#position').val(),
photo: $('#photo').val(),
photo_file: $('#photo').prop("files")[0],
is_acting: 0,
is_past: 0,
start: $('#start').data('date'),
end: $('#end').data('date'),
};
if ($('#acting').is(':checked')) {
form_data.is_acting = 1;
}
if ($('#past').is(':checked')) {
form_data.is_past = 1;
}
console.log(form_data);
$.ajax({
type: "post",
enctype: "multipart/form-data",
url: baseurl + "/admin/user/add",
data: form_data,
processData: false,
contentType: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function () {
$('#input-area').modal('hide');
listUser();
}
});
}
}
Here is some code in my controller
public function addUser(Request $request) {
$user = new User();
$user->thai_name = request()->thai_name;
$user->eng_name = request()->eng_name;
$user->email = request()->email;
$user->password = Hash::make(request()->password);
$user->telephone = request()->telephone;
$user->fax = request()->fax;
$user->system_role_id = request()->system_role_id;
$user->faction_id = request()->faction_id;
$user->position_id = request()->position_id;
// Store Profile Photo
$filename = "profile-photo-" . time() . "-name-" . basename(request()->photo);
request()->file('photo_file')->storeAs('', $filename, 'images');
$user->photo = $filename;
$user->is_acting = request()->is_acting;
$user->is_past = request()->is_past;
$user->start = request()->start;
$user->end = request()->end;
$user->save();
}
Which part I did wrong? Any ideas?
Please help me figure this out.
Thank you in advanced.

I am using vform in vue.js to get data from a row for editing but this is the error i get. Cannot read property 'fill' of undefined

this is the variable that holds the form data
userForm: new Form({
last_name: '',
first_name: '',
other_name: '',
email: '',
dob: '',
gender: '',
phone_number: '',
}),
on click of the edit button, i use the row id to fetch the data from backend and am using vform method fill to fill the userForm variable with the response data
'click .edit': function (e, value, row){
axios
.get('/data/admin/'+row.id)
.then(response => {
this.loading = false;
//let data = response;
this.userForm.fill(response.data);
$('#adminEditModal').modal('show');
}).catch(error => {
this.loading = false;
this.error = error.response.data.message || error.message;
});
},
this is my form
<form #submit.prevent="UpdateUser" ref="userForm">
<div class="modal-body">
<div class="login-logo">
<img src="" width="100" height="auto" alt="user">
</div>
<div class="form-group">
<label>Last Name</label>
<input v-model="userForm.last_name" type="text" name="last_name"
class="form-control" :class="{ 'is-invalid': userForm.errors.has('last_name') }">
<has-error :form="userForm" field="last_name"></has-error>
</div>
<div class="form-group">
<label>Firat Name</label>
<input v-model="userForm.first_name" type="text" name="first_name"
class="form-control" :class="{ 'is-invalid': userForm.errors.has('first_name') }">
<has-error :form="userForm" field="first_name"></has-error>
</div>
<div class="form-group">
<label>Other Name</label>
<input v-model="userForm.other_name" type="text" name="other_name"
class="form-control" :class="{ 'is-invalid': userForm.errors.has('other_name') }">
<has-error :form="userForm" field="other_name"></has-error>
</div>
<div class="form-group">
<label>Email</label>
<input v-model="userForm.email" type="email" name="email"
class="form-control" :class="{ 'is-invalid': userForm.errors.has('email') }">
<has-error :form="userForm" field="email"></has-error>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input v-model="userForm.dob" type="date" name="dob"
class="form-control" :class="{ 'is-invalid': userForm.errors.has('dob') }">
<has-error :form="userForm" field="dob"></has-error>
</div>
<div class="form-group">
<label>Gender</label>
<select v-model="userForm.gender" type="text" name="gender"
class="form-control" :class="{ 'is-invalid': userForm.errors.has('gender') }" >
<option>Male</option>
<option>Female</option>
</select>
<has-error :form="userForm" field="gender"></has-error>
</div>
<div class="form-group">
<label>Phone Number</label>
<input v-model="userForm.phone_number" type="text" name="phone_number"
class="form-control" :class="{ 'is-invalid': userForm.errors.has('phone_number') }">
<has-error :form="userForm" field="phone_number"></has-error>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success">Update <i class="fas fa-upload"></i></button>
</div>
</form>

how to save data/records from html form to DB2 database

this is my html form
<form class="form-horizontal" id="myCredantials" >
<div class="form-group">
<label class="col-sm-2 control-label">Username : </label>
<div class="col-sm-10 col-md-2">
<input type="text" class="form-control" id="tbUsername" placeholder="Username">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Email : </label>
<div class="col-sm-10 col-md-2">
<input type="text" class="form-control" id="tbEmail" placeholder="Email">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Gender : </label>
<div class="col-sm-10 col-md-2">
<select id="cbmGender">
<option value="-1">Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Date : </label>
<div class="col-sm-10 col-md-2">
<input type="text" class="form-control" id="tbDate">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Price : </label>
<div class="col-sm-10 col-md-2">
<input type="text" class="form-control" value="0" id="tbPrice" placeholder="Prices">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Perc % : </label>
<div class="col-sm-10 col-md-2">
<input type="text" class="form-control" value="0" id="tbPercentage" placeholder="%">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Password : </label>
<div class="col-sm-10 col-md-2">
<input type="password" class="form-control" id="tbPassword" placeholder="Password">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Confirm Password : </label>
<div class="col-sm-10 col-md-2">
<input type="password" class="form-control" id="tbConfirmpassword" placeholder="Confirm">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="button" class="btn btn-primary " id="btnSubmit" onclick="onSave()" value="Save" >
<input type="button" class="btn btn-primary " id="btnClear" onclick="onClearError()" value="Remove Error" >
<input type="button" class="btn btn-primary " id="btnGet" onclick="doGet()" value="Get" >
<input type="button" class="btn btn-primary " id="btnGet" onclick="doPost()" value="Post" >
</div>
</div>
<div style="width:700px;">
<table cellpadding="0" cellspacing="0" border="0" width="700" id="tbl_Credantials">
</table>
</div>
</form>

Resources