undefined offset: 1 when storing data from dynamic form - php

I'm trying to save data to the database using a dynamic form using javascript, I followed several methods from the internet, but when I try to send data to the database, there is an error undefined offset: 1 in the following line:
'question' => $question[$i], (On controller)
can anyone help me?
here is the form.blade.php
<form action="{{ url('/rating/post-form') }}" method="POST">
#csrf
<div class=" col-sm-12 nopadding form-group">
<label for="survei_tag">Kategori</label>
<input type="text" class="form-control" id="survei_tag" name="survei_tag">
</div>
<div class="col-sm-12 nopadding form-group">
<label for="judul">Judul</label>
<input type="text" class="form-control" id="judul" name="judul">
</div>
<div id="education_fields"></div>
<div class="col-sm-12 nopadding">
<div class="form-group">
<label for="pertanyaan">Pertanyaan</label>
<input type="text" class="form-control" id="question" name="question[]"
placeholder="Tambahkan Pertanyaan">
</div>
</div>
<div class="col-sm-3 nopadding">
<div class="form-group">
<input type="text" class="form-control" id="opsi_1" name="opsi_1[]"
placeholder="Opsi 1">
</div>
</div>
<div class="col-sm-3 nopadding">
<div class="form-group">
<input type="text" class="form-control" id="opsi_2" name="opsi_2[]"
placeholder="Opsi 2">
</div>
</div>
<div class="col-sm-3 nopadding">
<div class="form-group">
<input type="text" class="form-control" id="opsi_3" name="opsi_3[]"
placeholder="Opsi 3">
</div>
</div>
<div class="col-sm-3 nopadding">
<div class="form-group">
<div class="input-group">
<div class="form-group">
<input type="text" class="form-control" id="opsi_4" name="opsi_4[]"
placeholder="Opsi 4">
</div>
<div class="input-group-btn">
<button class="btn btn-success" type="button"
onclick="education_fields();">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</button>
</div>
</div>
</div>
</div>
<div class="clear"></div>
<button type="submit" class="btn btn-primary pull-right">
Simpan
</button>
</form>
here is the main.js
var room = 1;
function education_fields() {
room++;
var objTo = document.getElementById('education_fields')
var divtest = document.createElement("div");
divtest.setAttribute("class", "form-group removeclass" + room);
var rdiv = 'removeclass' + room;
divtest.innerHTML = `
<div class="col-sm-12 nopadding">
<div class="form-group">
<label for="question">Pertanyaan</label>
<input type="text" class="form-control" id="question" name="question[]"
placeholder="Tambahkan Pertanyaan">
</div>
</div>
<div class="col-sm-3 nopadding">
<div class="form-group">
<input type="text" class="form-control" id="opsi_1" name="opsi_1[]"
placeholder="Opsi 1">
</div>
</div>
<div class="col-sm-3 nopadding">
<div class="form-group">
<input type="text" class="form-control" id="opsi_2" name="opsi_2[]"
placeholder="Opsi 2">
</div>
</div>
<div class="col-sm-3 nopadding">
<div class="form-group">
<input type="text" class="form-control" id="opsi_3" name="opsi_3[]"
placeholder="Opsi 3">
</div>
</div>
<div class="col-sm-3 nopadding">
<div class="form-group">
<div class="input-group">
<div class="form-group">
<input type="text" class="form-control" id="opsi_4" name="opsi_4[]"
placeholder="Opsi 4">
</div>
<div class="input-group-btn">
<button class="btn btn-danger" type="button" onclick="remove_education_fields(' + room + ');">
<span class="glyphicon glyphicon-minus" aria-hidden="true"></span>
</button>
</div>
</div>
</div>
</div>
<div class="clear">
</div>
`;
objTo.appendChild(divtest);
}
function remove_education_fields(rid) {
$
('.removeclass' + rid).remove();
}
here is the model :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Traits\Uuids;
class questions extends Model
{
use HasFactory,Uuids;
protected $table = 'survei_questions';
public $timestamps = true;
protected $fillable = [
'survei_tag',
'question_number',
'question',
'sq_instansi_id',
'sq_user_id',
'judul',
'opsi_1',
'opsi_2',
'opsi_3',
'opsi_4'
];
}
here is the controller :
public function storeQuestions(Request $request){
$request->validate([
'survei_tag' =>'required|string|max:255',
'judul' => 'required|string',
'question' => 'required|string',
'opsi_1' => 'required|string',
'opsi_2' => 'required|string',
'opsi_3' => 'required|string',
'opsi_4' => 'required|string',
]);
$survei_tag = $request->survei_tag;
$judul = $request->judul;
$question = $request->question;
$opsi_1 = $request->opsi_1;
$opsi_2 = $request->opsi_2;
$opsi_3 = $request->opsi_3;
$opsi_4 = $request->opsi_4;
for ($i = 1; $i <= count($question); $i++) {
$saveData = [
'survei_tag' => $survei_tag,
'question_number' => $i,
'question' => $question[$i],
'sq_instansi_id' => Auth::user()->instansiID,
'sq_user_id' => Auth::id(),
'judul' => $judul,
'opsi_1' => $opsi_1[$i],
'opsi_2' => $opsi_2[$i],
'opsi_3' => $opsi_3[$i],
'opsi_4' => $opsi_4[$i]
];
DB::table('survei_questions')->insert($saveData);
}
return Redirect::to('/rating/create-form')->with('success','Data berhasil disimpan');
}

Related

Udefined variable $product error on update form submition

Please am trying to update my product entry in the database but I get an undefined variable error on submit. The form is supposed to update my database entry but when I route to the update method without passing the product id, I get a missing parameter product error and when add itas is the code below, I get an undefined variable product error
This is my update function
public function update(Request $request, Product $product)
{
$my_array = [$request->file('primary_image'), $request->file('image_1'), $request->file('image_2')];
$insert_array = [];
foreach ($my_array as $item) {
$save_url = '';
if ($item) {
$image = $item;
$name_gen = md5(rand(1000, 10000)) .'.'. $image->getClientOriginalExtension();
Image::make($image)->resize(523,605)->save('upload/products/'.$name_gen);
$save_url = 'upload/products/'.$name_gen;
}
array_push($insert_array, $save_url);
}
$product = Product::find($product->id);
$product->update([
'name' => $request->name,
'category' => $request->category,
'price' => $request->price,
'description' => $request->description,
'status' => $request -> status,
'estimated_delivery_time' => $request->estimated_delivery_time,
'available_quantity' => $request->available_quantity,
'colors' => $request->colors,
'supplier_name' => $request->supplier_name,
'supplier_phone' => $request->supplier_phone,
'video_description' => $request->video_description,
'primary_image' => $insert_array[0],
'image_1' => $insert_array[1],
'image_2' => $insert_array[2],
]);
$notification = array(
'message' => 'Product created successfully',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
this is my blade component
<form method="POST" action="{{route('products.update', ['products' => $product->id])}}" enctype="multipart/form-data">
{{ method_field('PUT') }}
#csrf
<h4 class="card-title">Edit Product</h4><br><br>
<div class="row">
<div class="col">
<div class="row mbr-1">
<label for="example-text-input" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-6">
<input name="name" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input"
class="col-sm-2 col-form-label">Category</label>
<div class="col-sm-6">
<input name="category" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Price</label>
<div class="col-sm-6">
<input name="price" class="form-control" type="number"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Status</label>
<div class="col-sm-6">
<input name="status" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Product
Description</label>
<div class="col-sm-8">
<textarea id="elm1" name="description" placeholder="Please enter a vivid description of the product"></textarea>
</div>
</div>
<!-- end row -->
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Tags</label>
<div class="col-sm-6">
<input name="tags" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Estimated
Delivery
Time</label>
<div class="col-sm-6">
<input name="estimated_delivery_time" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Available
Quantity</label>
<div class="col-sm-6">
<input name="available_quantity" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Colors</label>
<div class="col-sm-6">
<input name="colors" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Supplier's
Name</label>
<div class="col-sm-6">
<input name="supplier_name" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Supplier's
Contact</label>
<div class="col-sm-6">
<input name="supplier_contact" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Video Url /
Link</label>
<div class="col-sm-6">
<input name="video_description" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Primarry
Image</label>
<div class="col-sm-6">
<input name="primary_image" accept="image/*" class="form-control"
type="file" id="image">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label"> Primary Image
Preview</label>
<div class="col-sm-6">
<img id="showImage" class="" width="300px" src="{{ (!empty($product->image))? url($product->image):url('upload/no_image.jpg') }}" alt="Product image">
{{-- <img id="showImage" class="" width="300px"src="{{ !empty($product->image) ? url($product->image):url('upload/no_image.jpg') }}"alt="Hero image"> --}}
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Add Second Image
</label>
<div class="col-sm-6">
<input name="image_1" accept="image/*" class="form-control"
type="file" id="image" multiple="">
</div>
</div>
<!-- end row -->
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Add Third Image
</label>
<div class="col-sm-6">
<input name="image_2" accept="image/*" class="form-control"
type="file" id="image" multiple="">
</div>
</div>
</div>
</div>
<input type="submit" class="btn btn-info waves-effect waves-light" value="Edit Product">
</form>
Please what am I doing wrong?
Thanks for taking the time to review my code
Some refinements to add:
You have product from route model binding at first no need to find it again here: $product = Product::find($product->id);
You are passing the products but your route needs product.
Instead of {{ method_field('PUT') }} use #method('PUT') in your form.
If you had validation you had the data after validation and this code
$product->update([ 'name' => $request->name, 'category' => $request->category, 'price' => $request->price, 'description' => $request->description, 'status' => $request -> status, 'estimated_delivery_time' => $request->estimated_delivery_time, 'available_quantity' => $request->available_quantity, 'colors' => $request->colors, 'supplier_name' => $request->supplier_name, 'supplier_phone' => $request->supplier_phone, 'video_description' => $request->video_description, 'primary_image' => $insert_array[0], 'image_1' => $insert_array[1], 'image_2' => $insert_array[2], ]);
would be transferred to
$validatedData = $request->validate([ .... ]); $product->update($validatedData + [ 'primary_image' => $insert_array[0], 'image_1' => $insert_array[1], 'image_2' => $insert_array[2], ]);
or even moving the validation to a separate form request class.

Why nothing is inserted in the table even if it is related to sql and laravel

I created a modal using Bootstrap, to add a new row into table.
I don't know why and how doesn't work.
This is table_edit.blade.php MODAL:
<div id="formModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Adaugare Rolete</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<span id="form_result"></span>
<form method="post" id="sample_form" class="form-horizontal" action ="{{route('tabledit.store')}}">
#csrf
<div class="form-group">
<label class="control-label col-md-4" >ProdusID : </label>
<div class="col-md">
<input type="text" name="ProdusID" id="ProdusID" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4">Denumire : </label>
<div class="col-md">
<input type="text" name="Denumire" id="Denumire" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Cant1 : </label>
<div class="col-md">
<input type="text" name="Cant1" id="Cant1" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Cant2 : </label>
<div class="col-md">
<input type="text" name="Cant2" id="Cant2" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Cant3 : </label>
<div class="col-md">
<input type="text" name="Cant3" id="Cant3" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Cant4 : </label>
<div class="col-md">
<input type="text" name="Cant4" id="Cant4" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Data : </label>
<div class="col-md">
<input type="date" name="Data" id="Data" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Pret Intrare : </label>
<div class="col-md">
<input type="text" name="Pret_Intrare" id="Pret_Intrare" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Pret Iesire : </label>
<div class="col-md">
<input type="text" name="Pret_Iesire" id="Pret_Iesire" class="form-control" />
</div>
</div>
<br />
<div class="form-group" align="center">
<input type="hidden" name="action" id="action" value="Add" />
<input type="hidden" name="hidden_id" id="hidden_id" />
<input type="submit" name="action_button" id="action_button" class="btn btn-warning" value="Add" />
</div>
</form>
</div>
</div>
</div>
</div>
Here we can see I do an easy html bootstrap, it's oke, it's work, but affter, maybe in Controller it's the problem.
This is Controller:
public function store(Request $request){
$rules = array(
'ProdusID' => 'required',
'Denumire' => 'required',
'Cant1' => 'required',
'Cant2' => 'required',
'Cant3' => 'required',
'Cant4' => 'required',
'Data' => 'required',
'Pret_Intrare' => 'required',
'Pret_Iesire' => 'required'
);
$error = Validator::make($request->all(), $rules);
if($error->fails()){
return response()->json(['errors' => $error->errors()->all()]);
}
$form_data = array(
'ProdusID' => $request->ProdusID,
'Denumire' => $request->Denumire,
'Cant1' => $request->Cant1,
'Cant2' => $request->Cant2,
'Cant3' => $request->Cant3,
'Cant4' => $request->Cant4,
'Data' => $request->Data,
'Pret_Intrare' => $request->Pret_Intrare,
'Pret_Iesire' => $request->Pret_Iesire
);
Rolete::create($form_data);
return response()->json(['succes' => 'Data Added successfully.']);
}
And web:
Route::post('tabledit/store', 'TableditControllerRolete#store')->name('tabledit.store');
This is an image with modal
(!) UPDATE: I don't have any error, but he doesn't adding into table.
your form don't have an action attribute. you should fix that line with this:
<form method="post" id="sample_form" class="form-horizontal" action ="{{route('tabledit.store'}}">

form in codeigniter cannot submit in controller

i have a problem regarding with form submission in my code. I have a form that would let the user to input his/her whole information profile and also in my form, i got dynamic input type boxes. In short i have a very large form in my html with JavaScript dynamic input type.
Now my problem is, i am printing the data submitted in my form from view to controller and it doesn't print anything or when i press submit button, the page only refreshes not submitting to the controller.
Here is my full view code:
<form name="Register" class="m-t" role="form" action="<?php echo site_url("user_login/register_user/reg_new") ?>" method="post">
<div class="container">
<div class="picture-container">
<div class="picture">
<img src="https://lh3.googleusercontent.com/LfmMVU71g-HKXTCP_QWlDOemmWg4Dn1rJjxeEsZKMNaQprgunDTtEuzmcwUBgupKQVTuP0vczT9bH32ywaF7h68mF-osUSBAeM6MxyhvJhG6HKZMTYjgEv3WkWCfLB7czfODidNQPdja99HMb4qhCY1uFS8X0OQOVGeuhdHy8ln7eyr-6MnkCcy64wl6S_S6ep9j7aJIIopZ9wxk7Iqm-gFjmBtg6KJVkBD0IA6BnS-XlIVpbqL5LYi62elCrbDgiaD6Oe8uluucbYeL1i9kgr4c1b_NBSNe6zFwj7vrju4Zdbax-GPHmiuirf2h86eKdRl7A5h8PXGrCDNIYMID-J7_KuHKqaM-I7W5yI00QDpG9x5q5xOQMgCy1bbu3St1paqt9KHrvNS_SCx-QJgBTOIWW6T0DHVlvV_9YF5UZpN7aV5a79xvN1Gdrc7spvSs82v6gta8AJHCgzNSWQw5QUR8EN_-cTPF6S-vifLa2KtRdRAV7q-CQvhMrbBCaEYY73bQcPZFd9XE7HIbHXwXYA=s200-no" class="picture-src" id="wizardPicturePreview" title="">
<input type="file" name="employee-picture" id="wizard-picture" class="">
</div>
<h6 class="">Choose Picture</h6>
</div>
</div>
<div class="register-body">
<p>Personal Information</p>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="user_id" id="user_id" class="form-control input-sm" placeholder="User ID" required>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="first_name" id="first_name" class="form-control input-sm" placeholder="First Name" style="text-transform: capitalize;" required>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="middle_name" id="middle_name" class="form-control input-sm" placeholder="Middle Name" style="text-transform: capitalize;" required>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="last_name" id="last_name" class="form-control input-sm" placeholder="Last Name" style="text-transform: capitalize;" required>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="email" name="email" id="email" class="form-control input-sm" placeholder="Email Address" required>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="contact_no" id="contact_no" class="form-control input-sm" placeholder="Contact Number" required>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-sm" placeholder="Password" required>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password_confirmation" id="password_confirmation" class="form-control input-sm" placeholder="Confirm Password" required>
</div>
</div>
</div>
<label for="cvil_stat">Permanent address</label>
<div class="row">
<div class="col-sm-3 col-sm-3 col-md-3">
<label for="cvil_stat">Province</label>
<br>
<select class="form-group" name="province" id="province_id">
<option> ----- Select Option ----- </option>
<?php foreach($provinces as $province)
echo '<option value='.$province->province_id.'>'.$province->province_name.'</option>'?>
</select>
</div>
<div class="col-sm-3 col-sm-3 col-md-3">
<label for="gender">City</label>
<br>
<select class="form-group" name="city" id="city_id">
<option> ----- Select Option ----- </option>
</select>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<br>
<input type="text" name="street_name" id="street_name" class="form-control input-sm" placeholder="House no. / Street name" required>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="philhealth_no" id="philhealth_no" class="form-control input-sm" placeholder="Philhealth Number" style="text-transform: capitalize;" required>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="pagibig_no" id="pagibig_no" class="form-control input-sm" placeholder="Pag-ibig Number" required>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="tin_no" id="tin_no" class="form-control input-sm" placeholder="TIN Number" style="text-transform: capitalize;" required>
</div>
</div>
<div class="col-sm-6 col-sm-6 col-md-6">
<input type="text" name="dependentnumber" id="dependentnumber" class="form-control input-sm" placeholder="Number of Dependent Child" style="text-transform: capitalize;" required>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="sss_no" id="sss_no" class="form-control input-sm" placeholder="SSS Number" required>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="place_ofb" id="place_ofb" class="form-control input-sm" placeholder="Place of Birth" style="text-transform: capitalize;" required>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-sm-6 col-md-6">
<label for="gender">Birthday</label>
<br>
<select class="form-group" name="bday_month">
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
<select class="form-group" name="bday_day">
<?php for($i=1;$i<32;$i++) { ?>
<option><?php echo $i ?></option>
<?php } ?>
</select>
<select class="form-group" name="bday_year">
<?php $y = date('Y'); ?>
<?php for($j=0;$j<120;$j++) { ?>
<option><?php echo $y-$j ?></option>
<?php } ?>
</select>
</div>
<div class="col-sm-6 col-sm-6 col-md-6">
<label for="gender">Religion</label>
<br>
<select class="form-group" name="religion">
<option> ----- Select Option ----- </option>
<?php foreach($religions as $religion)
echo '<option value='.$religion->religion_id.'>'.$religion->religion_name.'</option>'?>
</select>
</div>
</div>
<div class="row">
<div class="col-sm-6 col-sm-6 col-md-6">
<label for="cvil_stat">Civil Status</label>
<br>
<select class="form-group" name="civil_stat" id="civil_stat">
<option> ----- Select Option ----- </option>
<option value="Single">Single</option>
<option value="Married">Married</option>
<option value="Legally Separated">Legally Separated</option>
<option value="Widowed">Widowed</option>
</select>
</div>
<div class="col-sm-6 col-sm-6 col-md-6">
<label for="gender">Citizenship</label>
<br>
<select class="form-group" name="citizenship">
<option> ----- Select Option ----- </option>
<?php foreach($citizenships as $citizenship)
echo '<option value='.$citizenship->citizenship_id.'>'.$citizenship->citizenship_name.'</option>'?>
</select>
</div>
</div>
<p>Family Background</p>
<div class="row" id="field_div">
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="fathers_name" id="fathers_name" class="form-control input-sm" placeholder="Father's Name" style="text-transform: capitalize;" required>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
<input type="text" name="fathers_occu" id="fathers_occu" class="form-control input-sm" placeholder="Occupation" style="text-transform: capitalize;" required>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
<input type="text" name="fdate_birth" id="fdate_birth" class="form-control input-sm" placeholder="Date of birth" style="text-transform: capitalize;" required>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="mothers_name" id="mothers_name" class="form-control input-sm" placeholder="Mother's Name" style="text-transform: capitalize;" required>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
<input type="text" name="mothers_occu" id="mothers_occu" class="form-control input-sm" placeholder="Occupation" style="text-transform: capitalize;" required>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="form-group">
<input type="text" name="mdate_birth" id="mdate_birth" class="form-control input-sm" placeholder="Date of birth" style="text-transform: capitalize;" required>
</div>
</div>
</div>
<p>Educational Background</p>
<div class="control-btns">
<div class="row">
<div class="col-xs-3 col-sm-3 col-md-3">
<button class="btn btn-primary btn-sm" onclick="add_field_educ(event);">+</button>
<button class="btn btn-danger btn-sm" onclick="remove_field_educ(event);">-</button>
</div>
</div>
</div>
<div id="wrapper_educ">
<div id="educ_div">
</div>
</div>
<p>Work Experience</p>
<div class="control-btns">
<div class="row">
<div class="col-xs-3 col-sm-3 col-md-3">
<button class="btn btn-primary btn-sm" onclick="add_field_work(event);">+</button>
<button class="btn btn-danger btn-sm" onclick="remove_field_work(event);">-</button>
</div>
</div>
</div>
<div id="wrapper_work">
<div id="work_div">
</div>
</div>
<div class="register_footer">
<button type="submit" value="Register" class="btn btn-primary">Register</button>
<button class="btn btn-primary">Clear</button>
</div>
</div>
</div>
</form>
Here is my controller code that i want to print my submitted code:
public function reg_new() {
$this->form_validation->set_message('is_unique', 'The %s is already taken.');
$this->form_validation->set_error_delimiters('<div class="error"><li>','</li></div>');
if ($this->form_validation->run('reg_config') == FALSE) {
$this->index();
} else {
$register_data = array(
'User_Id' => $this->input->post('user_id'),
'First_Name' => $this->input->post('first_name'),
'Middle_Name' => $this->input->post('middle_name'),
'Last_Name' => $this->input->post('last_name'),
'Email' => $this->input->post('email'),
'Contact_no' => $this->input->post('contact_no'),
'Password' => $this->bcrypt->hash_password($this->input->post('password')),
'City_id' => $this->input->post('city'),
'Street' => $this->input->post('street_name'),
'Philhealth_num' => $this->input->post('philhealth_no'),
'Pagibig_num' => $this->input->post('pagibig_no'),
'Tin_num' => $this->input->post('tin_no'),
'Dependent_children' => $this->input->post('dependentnumber'),
'Sss_num' => $this->input->post('sss_no'),
'Place_of_birth' => $this->input->post('place_ofb'),
'Gender' => $this->input->post('gender'),
'Birthdate' => $this->input->post('bday_year') . "-" . date("m",strtotime($this->input->post('bday_month'))) . "-" . $this->input->post('bday_day'),
'Religion' => $this->input->post('religion'),
'Citizenship' => $this->input->post('citizenship'),
'Civil_status' => $this->input->post('civil_stat'),
'fathers_name' => $this->input->post('fathers_name'),
'fathers_occupation' => $this->input->post('fathers_occu'),
'father_bdate' => $this->input->post('fdate_birth'),
'mothers_name' => $this->input->post('mothers_name'),
'mothers_occupation' => $this->input->post('mothers_occu'),
'mother_bdate' => $this->input->post('mdate_birth'),
'User_Status' => $this->user_stat,
'Delete_Status' => 0,
'Position_Id' => 0,
);
if($this->input->post('civil_stat') == 'Married') {
$spouse_data = array (
'Spouse_name' => $this->input->post('spouse_name'),
'Spouse_occupation' => $this->input->post('spouse_occu'),
'Spouse_bdate' => $this->input->post('spouse_dateofb'),
);
}
foreach ($this->input->post('educ_opt') as $educ) {
$count_educ = $count_educ++;
}
print_r($register_data);
print_r($count_educ);
print_r($spouse_data);
}
}
please take a look with my code why i cant submit it from controller.
thanks.
Thanks guys.. i just figured it out, its because of my form validation that i thought it doesn't submit because it did not satisfy my form validation and for that i returned $this->index();

Calling controllers different methods from view page using codeigniter

I am working on codeigniter project in which viwe page having textboxes, radio buttons and button.I fetched textbox values from database and manually enter from date and to date then by clicking on save button insert query gets empty values because in controller i called all functions serially in index function.What is the best solution to call controller methods on each event of view page?
leave_appl_view.php
<form action="leave_apply_control/index" method="post" id="myForm">
<div class="row">
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Emp ID</label>
<?php if(!empty($emp_leave))
{
foreach ($emp_leave as $liv)
{ ?>
<input type="text" class="form-control" name="id" value="<?php echo $liv->employee_id ?>" >
</div>
</div>
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Employee Name</label>
<input type="text" class="form-control" name="empname" id="empname" value="<?php echo $liv->employee_name ?>" >
</div>
</div>
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Email address</label>
<input type="email" class="form-control" name="empmail" id="empmail" value="<?php echo $liv->email ?>" >
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Leave type: </label>
<input type="radio" id="half" name="type" onclick="Checkradiobutton()" value="half"> Half Day
<input type="radio" id="full" name="type" onclick="Checkbutton()" value="full" Checked> Full Day
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating" id="frm_dt">
<label class="control-label">From Date: </label>
<input type="text" class="form-control" id="datepicker1" name="from" ></p>
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating" id="to_dt">
<label class="control-label">To Date: </label>
<input type="text" class="form-control" id="datepicker2" name="to" ></p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Balance Leaves</label>
<input type="text" class="form-control" name="bal_leave" value="<?php echo $liv->balanced_leaves ?>" >
</div>
<?php }
}
?>
</div>
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Applied Leaves</label>
<input type="text" class="form-control" name="totaldays" id="totaldays" readonly>
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Applied Date</label>
<input type="text" class="form-control" name="todayDate" id="todayDate" value="<?php echo date("d/m/Y"); ?>" readonly>
</div>
</div>
</div>
<div class="row">
<div class="col-md-7">
<div class="form-group label-floating">
<label class="control-label">Reason </label>
<TEXTAREA class="form-control" id="reason" name="reason"> </TEXTAREA>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary" id="submit" name="submit" onclick="<?php // echo base_url() ?>leave_apply_control/save_data">Apply</button>
<button type="reset" class="btn btn-primary" onclick="<?php echo base_url();?>leave_apply_control" >Reset</button>
</form>
leave_Apply_control.php
<?php
class leave_apply_control extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation'));
$this->load->library('calendar');
$this->load->database();
$this->load->model('user_model');
$this->load->helper('date');
}//contruct close
function index()
{
$id=$this->session->userdata('id');
$data['foren_id'] = $this -> user_model -> get_emp_id($id);
foreach($data['foren_id'] as $fk)
{
//echo "////control page fk is ".$fk->emp_id;
}
$data2['emp_leave'] = $this->user_model->get_leave_count($fk->emp_id);
$this->load->view('leave_application',$data2);
$this->save_data();
}
public function save_data()
{
$fd = date('Y-m-d',strtotime($this->input->post('from')));
$td = date('Y-m-d',strtotime($this->input->post('to')));
$data = array(
'employee_id' => $this->input->post('id'),
'emp_name' => $this->input->post('empname'),
'leave_from' => $fd,
'leave_to' => $td,
'no_of_days' => $this->input->post('totaldays'),
'applied_date' => date('Y-m-d H:i:s'),
'reason' => $this->input->post('reason'),
'mail' => $this->input->post('empmail'),
'leave_type' => $this->input->post('type'),
'balance_leaves' => $this->input->post('bal_leave'),
'status' => 1,
);
$this->user_model->insert_leave($data);
}
}

Password field is not arriving to php using ajax

So i am having an issue debugging where the issue is. Could be Jquery or PHP issue. I have done quite a bit of alert testing and i believe the issue lies in the if statement of ajax, eg i am posting pass if the field is not disabled when posting. Because when doing a echo $_POST['pass'] and alert(data), i get nothing. So i believe the php is working fine please tell me where the issue lies and if you know also the remedy to this situation. Thanks Guys.
Ajax
$(document).on('submit', '#editaccsount', function(event) {
event.preventDefault();
myData = {
contactname: $('input[name=contactname]').val(),
business: $('input[name=business]').val(),
email: $('input[name=email]').val(),
code: $('input[name=code]').val(),
phone: $('input[name=phone]').val(),
priceband: $('input[name=priceband]').val(),
address: $('input[name=address]').val(),
active: $('input[name=active]').val(),
mon: $('input[name=mon]').val(),
tue: $('input[name=tue]').val(),
wed: $('input[name=wed]').val(),
thu: $('input[name=thu]').val(),
fri: $('input[name=fri]').val(),
sat: $('input[name=sat]').val(),
sund: $('input[name=sund]').val(),
adminname: $('input[name=adminname]').val(),
accountid: $('input[name=accountid]').val(),
isadmin: $('input[name=isadmin]').val(),
};
var isDisabled = $('input[name=pass]').prop('disabled');
if (isDisabled == false) {
myData.pass = $('input[name=pass]').val();
}
$.ajax({
url: 'php/editaccount.php',
type: "POST",
data: myData,
success: function(data) {
if ($('input[name=isadmin]').val() == 1) {
$('input[name=accountsearch]').val($('input[name=email]').val());
$('input[name=accountsearch]').submit();
} else {
$('input[name=accountsearch]').val($('input[name=business]').val());
$('input[name=accountsearch]').submit();
}
alert(data);
}
});
});
This is my PHP that does all the updating based on conditions:
<?php
require '../../core/init.php';
$auth = new Auth();
$accid = $_POST['accountid'];
echo $accid, '<br/>';
echo 'Password is: ', $_POST['pass'], '<br/>';
echo 'Is Admin: ', $_POST['isadmin'];
if (!empty($_POST['pass'])) {
echo 'Pass Detected';
echo 'Password: ', $_POST['pass'], "<br/>";
$salt = $auth->randomString();
$newpass = $salt . $_POST['pass'];
$newpass = $auth->hashData($newpass);
if ($_POST['isadmin'] == 0) {
$customer = DBPDO::getInstance()->update('customer', $accid, array(
'businessName' => $_POST['business'],
'contactName' => $_POST['contactname'],
'email' => $_POST['email'],
'code' => $_POST['code'],
'phone' => $_POST['phone'],
'priceBand' => $_POST['priceband'],
'deliveryAddress' => $_POST['address'],
'is_active' => $_POST['active'],
'mon' => $_POST['mon'],
'tue' => $_POST['tue'],
'wed' => $_POST['wed'],
'thu' => $_POST['thu'],
'fri' => $_POST['fri'],
'sat' => $_POST['sat'],
'sun' => $_POST['sund'],
'password' => $newpass,
'user_salt' => $salt
));
} elseif ($_POST['isadmin'] == 1) {
$admin = DBPDO::getInstance()->update('admin', $accid, array(
'adminName' => $_POST['adminname'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
'password' => $newpass,
'user_salt' => $salt
));
}
} elseif (empty($_POST['pass'])) {
echo 'Pass NOT detected';
if ($_POST['isadmin'] == 0) {
$customer = DBPDO::getInstance()->update('customer', $accid, array(
'businessName' => $_POST['business'],
'contactName' => $_POST['contactname'],
'email' => $_POST['email'],
'code' => $_POST['code'],
'phone' => $_POST['phone'],
'priceBand' => $_POST['priceband'],
'deliveryAddress' => $_POST['address'],
'mon' => $_POST['mon'],
'tue' => $_POST['tue'],
'wed' => $_POST['wed'],
'thu' => $_POST['thu'],
'fri' => $_POST['fri'],
'sat' => $_POST['sat'],
'sun' => $_POST['sund']
));
} elseif ($_POST['isadmin'] == 1) {
$admin = DBPDO::getInstance()->update('admin', $accid, array(
'adminName' => $_POST['adminname'],
'email' => $_POST['email'],
'phone' => $_POST['phone']
));
}
}
What gets outputted as HTML:
<?php
require '../../core/init.php';
$data['result_1'] = '';
$searchquery = $_POST['accountsearch'];
if ($_POST['accounttype'] == 'Users') {
$customer = DBPDO::getInstance()->get('customer', array(
array('businessName', '=', "'$searchquery'" )
));
foreach ($customer->results() as $row)
{
$data['result_1'] .= '
<div class="row row-centered">
<input class="btn btn-lg btn-primary" type="button" name="editbtn" value="Edit Account">
<input class="btn btn-lg btn-danger remove-btn" type="button" name="removebtn" value="Remove Account" uid="'.$row->id.'">
</div><br/>
<form id="editaccount" action="php/editaccount.php" method="POST">
<h3>General Details</h3>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="contactname">Customer Name</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="contactname" class="form-control" value="'.$row->contactName.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="business">Business</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="business" class="form-control" value="'.$row->businessName.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="email">Email</label>
<div class="input-group input-group-lg">
<input type="email" disabled="disabled" name="email" class="form-control" value="'.$row->email.'">
</div>
</div>
</div>
</div>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="code">Code</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="code" class="form-control" value="'.$row->code.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="phone">Phone</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="phone" class="form-control" value="'.$row->phone.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="priceband">Price Band</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="priceband" class="form-control" value="'.$row->priceBand.'">
</div>
</div>
</div>
</div>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="address">Delivery Address</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="address" class="form-control" value="'.$row->deliveryAddress.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="pass">Active</label>
<div class="input-group input-group-lg">
<input type="test" disabled="disabled" name="active" class="form-control" value="'.$row->is_active.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="pass">Password</label>
<div class="input-group input-group-lg">
<input type="password" disabled="disabled" name="userpass" class="form-control" value="'.$row->password.'">
</div>
<div class="input-group input-group-lg">
<input id="editpasswordbtn" class="btn btn-sm btn-primary" type="button" value="Edit Password">
</div>
</div>
</div>
</div>
<div class="wrning" style="color:red;display:none;">
<b>Warning: </b> If you do NOT want to edit Password please click Cancel!<br/><br/>
<input type="button" class="btn btn-sm btn-danger" value="Cancel Password Change">
</div>
<br/><br/>
<h3>Standing Order Details</h3>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="mon">Monday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="mon" class="form-control" value="'.$row->mon.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="tue">Tuesday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="tue" class="form-control" value="'.$row->tue.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="wed">Wednesday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="wed" class="form-control" value="'.$row->wed.'">
</div>
</div>
</div>
</div>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="thu">Thursday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="thu" class="form-control" value="'.$row->thu.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="fri">Friday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="fri" class="form-control" value="'.$row->fri.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="sat">Saturday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="sat" class="form-control" value="'.$row->sat.'">
</div>
</div>
</div>
</div>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="sund">Sunday</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="sund" class="form-control" value="'.$row->sun.'">
</div>
</div>
</div>
</div>
<div class="row row-centered">
<input type="hidden" name="accountid" value="'.$row->id.'">
<input type="hidden" name="isadmin" value="0">
<input class="btn btn-lg btn-rj" disabled="disabled" type="submit" name="editbtn" value="Send to Database">
</div>
</form>
';
}
} elseif ($_POST['accounttype'] == 'Admins') {
$customer = DBPDO::getInstance()->get('admin', array(
array('email', '=', "'$searchquery'" )
));
foreach ($customer->results() as $row)
{
$data['result_1'] .= '
<div class="row row-centered">
<input class="btn btn-lg btn-primary" type="button" name="editbtn" value="Edit Account">
<input class="btn btn-lg btn-danger remove-btn adminremove-btn" type="button" name="adminremovebtn" value="Remove Account" uid="'.$row->id.'">
</div><br/>
<form id="editaccount" action="php/editaccount.php" method="POST">
<h3>General Details</h3>
<div class="row row-centered">
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="contactname">Admin Name</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="adminname" class="form-control" value="'.$row->adminName.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="contactname">Email</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="email" class="form-control" value="'.$row->email.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="business">Phone</label>
<div class="input-group input-group-lg">
<input type="text" disabled="disabled" name="phone" class="form-control" value="'.$row->phone.'">
</div>
</div>
</div>
<div class="col-md-3 col-centered" style="width:300px;">
<div class="form-group">
<label for="email">Email</label>
<div class="input-group input-group-lg">
<input type="hidden" name="isadmin" value="1">
<input type="password" disabled="disabled" name="pass" class="form-control" value="'.$row->password.'">
</div>
</div>
</div>
<div class="row row-centered">
<input type="hidden" name="accountid" value="'.$row- >id.'">
<input type="hidden" name="isadmin" value="1">
<input class="btn btn-lg btn-rj" disabled="disabled" type="submit" name="editbtn" value="Send to Database">
</div>
</div>
</form>
';
}
}
echo json_encode($data);
exit;
?>
You are checking the property of disabled with:
var isDisabled = $('input[name=pass]').prop('disabled');
If I recall this will return a string empty or disabled as a way to use this would be:
$('input[name=pass]').prop('disabled', 'disabled');
If you're looking for a true/false maybe try with:
var isDisabled = $('input[name=pass]').is(':disabled');
and it will return true or false.

Categories