PHP Ajax - Show part of the data in a table? - php

There are texareas in my table: "Case Description", "Case Notes", etc..
I want the case description not to show the entire textarea of more than 500 characters in the <td>...
I want it more like:
This is a case description....
When the Edit button is clicked, it pops a modal to edit the data via ajax (already done).
Here are my codes
Edit-report.php:
<!-- Start PHP While -->
<?php
$mysqli = new mysqli('localhost', 'mushref', 'Almadina1!', 'security_db')
or die('Dramatic Error: ' . mysqli_error($mysqli));
if( isset($_POST['id']) ) {
$id = $_POST['id'];
$ccc_employee = $_POST['cccEmployee'];
$IR_number = $_POST['irNumber'];
$case_type = $_POST['caseType'];
$caseLocation = $_POST['caseLocation'];
$startDate = $_POST['startDateTime'];
$endDate = $_POST['endDateTime'];
$case_description = $_POST['caseDesc'];
$action_taken = $_POST['actionsTaken'];
$details = $_POST['caseDetails'];
$notes = $_POST['caseNotes'];
$recommendation = $_POST['caseRecommendation'];
// Query to update data
$result = mysqli_query($mysqli, "UPDATE cases_reports SET
cccEmployee='$ccc_employee',
irNumber='$IR_number',
caseType='$case_type',
caseLocation='$caseLocation',
startDateTime='$startDate',
endDateTime='$endDate',
caseDesc='$case_description',
actionsTaken='$action_taken',
caseDetails='$details',
caseNotes='$notes',
caseRecommendation='$recommendation'
WHERE id='$id'");
}
$selectquery = "SELECT * FROM cases_reports";
$table = mysqli_query($mysqli, $selectquery);
$nums = mysqli_num_rows($table);
while($res = mysqli_fetch_array($table)) {
?>
<tr id="<?php echo $res['id']?>">
<td data-target="cccEmployee"><?php echo $res['cccEmployee'];?></td>
<td data-target="IR_number"><?php echo $res['irNumber'];?></td>
<td data-target="case_type"><?php echo $res['caseType'];?></td>
<td data-target="case_location"><?php echo $res['caseLocation'];?></td>
<td data-target="startDateTime"><?php echo $res['startDateTime'];?></td>
<td data-target="endtDateTime"><?php echo $res['endDateTime'];?></td>
<td data-target="case_description"><?php echo $res['caseDesc'];?></td>
<td data-target="actions_taken"><?php echo $res['actionsTaken'];?></td>
<td data-target="case_details"><?php echo $res['caseDetails'];?></td>
<td data-target="case_notes"><?php echo $res['caseNotes'];?></td>
<td data-target="case_recommendation"><?php echo $res['caseRecommendation'];?></td>
<td class="table-actions">
<a href="#" data-role="update" data-id="<?php echo $res['id']; ?>">
<i class="fas fa-edit"></i>
</a>
<!-- <a href="#!" class="table-action table-action-delete" data-toggle="tooltip" data-original-title="Delete product">
<i class="fas fa-trash"></i>
</a> -->
</td>
<td>
<div class="dropdown">
<a class="btn btn-sm btn-icon-only text-light" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-ellipsis-v"></i>
</a>
<div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
<a class="dropdown-item" href="#">Print PDF</a>
<a class="dropdown-item" href="#">Export Excel</a>
<a class="dropdown-item" href="#">Export Access</a>
</div>
</div>
</td>
</tr>
<div class="modal fade" id="reportsModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal- modal-dialog-centered modal-" role="document">
<div class="modal-content">
<div class="modal-header">
<h6 class="modal-title" id="modal-title-default"> Case Details </h6>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="case-data">
<div class="form-row">
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="cccEmployee">CCC Employee</label>
<input type="text" class="form-control" id="cccEmployee" name="cccEmployee">
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="IR_number">IR Number</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">IR#</span>
</div>
<input type="number" class="form-control" id="IR_number" name="IR_number">
</div>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="case_type">Case Type</label>
<select data-toggle="select" class="form-control" name="case_type" id="case_type">
<option value="" disabled selected>Select Case Type</option>
<option value="Accident">Accident</option>
<option value="Theft">Theft</option>
<option value="Death">Death</option>
<option value="Harrasment">Harrasment</option>
<option value="badBehaviour">Bad Behaviour</option>
<option value="Drugs">Drugs</option>
<option value="Drunk">Drunk</option>
<option value="Fight">Fight</option>
<option value="Medical">Medical</option>
<option value="illegalEntry">Illegal Entry</option>
<option value="illegalWorker">Illegal Worker</option>
<option value="Fire">Fire</option>
<option value="Electric">Electric</option>
<option value="illegalHunting">Illegal Hunting</option>
<option value="oilLeak">Oil Leak</option>
<option value="petrolLeak">Petrol Leak</option>
<option value="Kidnapping">Kidnapping</option>
<option value="Blackmail">Blackmail</option>
<option value="Misunderstanding">Misunderstanding</option>
<option value="propertyDamage">Property Damage</option>
<option value="smoke_NoFire">Smoke, No Fire</option>
<option value="stuck_InElevator">Stuck in Elevator</option>
<option value="Scooter">Scooter</option>
<option value="Drifting">Drifting</option>
<option value="LostPerson">Lost Person</option>
<option value="Fraud_Forgery">Fraud/Forgery</option>
<option value="Other">Other</option>
</select>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="case_location">Location</label>
<select class="form-control" name="case_location" id="case_location" data-toggle="select">
<option value="" disabled selected>Select Location</option>
<option value="Baylasun">Baylasun</option>
<option value="Baylasun Hotel">Baylasun Hotel</option>
<option value="AL-Waha">AL-Waha</option>
<option value="Al-Morooj">Al-Morooj</option>
<option value="Royal Green">Royal Green</option>
<option value="Beach Towers">Beach Towers</option>
<option value="Emmar Building">Emmar Building</option>
<option value="Industrial Area (East)">Industrial Area (East)</option>
<option value="Industrial Area (West)">Industrial Area (West)</option>
<option value="Hejaz Gate">Hejaz Gate</option>
<option value="Gate 3">Gate 3</option>
<option value="Marina 1">Marina 1</option>
<option value="Marina 2">Marina 2</option>
<option value="Marina 3">Marina 3</option>
<option value="Tala Garden">Tala Garden</option>
<option value="AL-Shorooq">AL-Shorooq</option>
<option value="Yam Beach">Yam Beach</option>
<option value="Sales Center">Sales Center</option>
</select>
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="startDate">Start Date & Time</label>
<input class="form-control" type="datetime-local" name="startDate" id="startDateTime">
</div>
</div>
<div class="col-md-6 mb-3">
<div class="form-group">
<label class="form-control-label" for="endDateTime">End Date & Time</label>
<input class="form-control" type="datetime-local" name="endDateTime" id="endDateTime">
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="case_description">Case Description</label>
<textarea class="form-control" name="case_description" id="case_description" rows="3"></textarea>
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="actions_taken">Actions Taken</label>
<textarea class="form-control" name="actions_taken" id="actions_taken"></textarea>
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="details">Details</label>
<textarea class="form-control" name="case_details" id="case_details"></textarea>
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="case_notes">Notes</label>
<textarea name="case_notes" id="case_notes" class="form-control"></textarea>
</div>
</div>
<div class="col-md-12 mb-3">
<div class="form-group">
<label class="form-control-label" for="case_recommendation">Recommendations</label>
<textarea name="case_recommendation" id="case_recommendation" class="form-control"></textarea>
</div>
</div>
<input type="hidden" id="caseId" class="form-control"/>
</div>
</div>
<div class="modal-footer">
<button type="button" id="save_report_changes" class="btn btn-primary">Save changes</button>
</div>
<?php } //End php While ?>
Ajax:
$(document).ready(function() {
// Append values in input fields
$(document).on('click', 'a[data-role=update]', function(){
var id = $(this).data('id');
var cccEmployee = $("#"+id).children('td[data-target=cccEmployee]').text();
var irNumber = $("#"+id).children('td[data-target=IR_number]').text();
var caseType = $("#"+id).children('td[data-target=case_type]').text();
var caseLocation = $("#"+id).children('td[data-target=case_location]').text();
var startDateTime = $("#"+id).children('td[data-target=startDateTime]').text();
var endDateTime = $("#"+id).children('td[data-target=endDateTime]').text();
var caseDesc = $("#"+id).children('td[data-target=case_description]').text();
var actionsTaken = $("#"+id).children('td[data-target=actions_taken]').text();
var caseDetails = $("#"+id).children('td[data-target=case_details]').text();
var caseNotes = $("#"+id).children('td[data-target=case_notes]').text();
var caseRecommendation = $("#"+id).children('td[data-target=case_recommendation]').text();
//Append the variables
$('#cccEmployee').val(cccEmployee);
$('#IR_number').val(irNumber);
$('#case_type').val(caseType);
$('#case_location').val(caseLocation);
$('#startDateTime').val(startDateTime);
$('#endDateTime').val(endDateTime);
$('#case_description').val(caseDesc);
$('#actions_taken').val(actionsTaken);
$('#case_details').val(caseDetails);
$('#case_notes').val(caseNotes);
$('#case_recommendation').val(caseRecommendation);
$('#caseId').val(id);
$('#reportsModal').modal('toggle');
});
//Create event to get data from fields and update.
$('#save_report_changes').click(function() {
var id = $('#caseId').val();
var cccEmployee = $('#cccEmployee').val();
var irNumber = $('#IR_number').val();
var caseType = $('#case_type').val();
var caseLocation = $('#case_location').val();
var startDateTime = $('#startDateTime').val();
var endDateTime = $('#endDateTime').val();
var caseDesc = $('#case_description').val();
var actionsTaken = $('#actions_taken').val();
var caseDetails = $('#case_details').val();
var caseNotes = $('#case_notes').val();
var caseRecommendation = $('#case_recommendation').val();
$.ajax({
url: '/edit-report',
method: 'post',
data: {
id: id,
cccEmployee: cccEmployee,
irNumber: irNumber,
caseType: caseType,
caseLocation: caseLocation,
startDateTime: startDateTime,
endDateTime: endDateTime,
caseDesc: caseDesc,
actionsTaken: actionsTaken,
caseDetails: caseDetails,
caseNotes: caseNotes,
caseRecommendation: caseRecommendation
},
success: function(response) {
$("#"+id).children('td[data-target=cccEmployee]').text(cccEmployee);
$("#"+id).children('td[data-target=IR_number]').text(irNumber);
$("#"+id).children('td[data-target=case_type]').text(caseType);
$("#"+id).children('td[data-target=case_location]').text(caseLocation);
$("#"+id).children('td[data-target=startDateTime]').text(startDateTime);
$("#"+id).children('td[data-target=endDateTime]').text(endDateTime);
$("#"+id).children('td[data-target=case_description]').text(caseDesc);
$("#"+id).children('td[data-target=actions_taken]').text(actionsTaken);
$("#"+id).children('td[data-target=case_details]').text(caseDetails);
$("#"+id).children('td[data-target=case_notes]').text(caseNotes);
$("#"+id).children('td[data-target=case_recommendation]').text(caseRecommendation);
$('#reportsModal').modal('toggle');
},
});
});
});

You can use mb_strimwidth("Hello World", 0, 10, "...");
where "Hello Word is your description"
This will return This will return Hello W....

Related

how to separate multiple values or split multiple values using implode based on array?

I input data with dynamics form, where one column contains multiple data using implode (,). but when I input, the dynamic form manages to do as many inputs as I want, but for "mekanik" table it becomes one.
pict one : modal for input data
for "keluhan" has been filled each table is different, but "mekanik" become one value. how to separate it?
in the mechanical column, the first row should be the operator and the second row should be abdullah muis.
pict two : table
here's my code:
this is my input "keluhan" and "mekanik" modal code, i use dynamic_fuild for dynamic form.
enter image description here
<?php
// mengambil data barang dari tabel dengan kode terbesar
$query = mysqli_query($connect, "SELECT max(kode_pg) as kodeTerbesar FROM tb_pg_cs");
$data = mysqli_fetch_array($query);
$kodepg = $data['kodeTerbesar'];
// mengambil angka dari kode barang terbesar, menggunakan fungsi substr dan diubah ke integer dengan (int)
$urutan = (int) substr($kodepg, 2, 7);
// nomor yang diambil akan ditambah 1 untuk menentukan nomor urut berikutnya
$urutan++;
// membuat kode barang baru
// string sprintf("%03s", $urutan); berfungsi untuk membuat string menjadi 3 karakter
// misalnya string sprintf("%03s", 22); maka akan menghasilkan '022'
// angka yang diambil tadi digabungkan dengan kode huruf yang kita inginkan, misalnya PC
$huruf = "SC";
$kodepg1 = $huruf . sprintf("%07s", $urutan);
?>
<?php
include "support/fungsi_romawi.php";
$bulan = date('n');
$romawi = getRomawi($bulan);
// mengambil data barang dari tabel dengan kode terbesar
$query = mysqli_query($connect, "SELECT max(kode_pg) as kodeTerbesar FROM tb_pg_cs");
$data = mysqli_fetch_array($query);
$kodepg = $data['kodeTerbesar'];
// mengambil angka dari kode barang terbesar, menggunakan fungsi substr dan diubah ke integer dengan (int)
$urutan = (int) substr($kodepg, 6, 6);
// nomor yang diambil akan ditambah 1 untuk menentukan nomor urut berikutnya
$urutan++;
// membuat kode barang baru
// string sprintf("%03s", $urutan); berfungsi untuk membuat string menjadi 3 karakter
// misalnya string sprintf("%03s", 22); maka akan menghasilkan '022'
// angka yang diambil tadi digabungkan dengan kode huruf yang kita inginkan, misalnya PC
$tahun = date ('Y');
$nomor = "FORM/SC/".$romawi."/".$tahun."/";
$kode = sprintf("%07s", $urutan);
$noformpg = $nomor. $kode;
?>
<div class="modal fade" id="addnew" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<center><h4 class="modal-title" id="myModalLabel">Tambah Data</h4></center>
</div>
<div class="modal-body">
<div class="container-fluid">
<form method="POST" action="system/add_pg.php" id="form_materi">
<input class="form-control" type="hidden" name="kodepg" value="<?php echo $kodepg1 ?>" required>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">NO. PENGAJUAN</label>
</div>
<div class="col-sm-9">
<input class="form-control" type="text" name="noformpg" value="<?php echo $noformpg ?>" required>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">TANGGAL</label>
</div>
<div class="col-sm-9">
<input class="form-control" type="date" name="waktupg" placeholder="Masukkan Hari dan Tanggal Pengajuan" autocomplete="off" required>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">DEPARTEMEN</label>
</div>
<div class="col-sm-9">
<select class="form-control selectpicker" type="text" name="deptpg" id="deptpg" data-live-search="true" required>
<option selected value> -- Pilih Departemen -- </option>
<?php
$query = mysqli_query($connect, "SELECT nama_dept FROM tb_dept");
while ($data = mysqli_fetch_array($query)) {
?>
<option value="<?=$data['nama_dept'];?>"><?php echo $data['nama_dept'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">LOKASI</label>
</div>
<div class="col-sm-9">
<select class="form-control selectpicker" type="text" name="lokasipg" id="lokasipg" data-live-search="true" required>
<option selected value> -- Pilih Lokasi -- </option>
<option value="JLA 1">JLA 1</option>
<option value="JLA 2">JLA 2</option>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">NAMA MESIN</label>
</div>
<div class="col-sm-9">
<select class="form-control selectpicker" type="text" name="merkpg" id="merkmes" onchange="auto_complete()" data-live-search="true" required>
<option selected value> -- Pilih Mesin -- </option>
<optgroup label="Mesin Forklift">
<?php
$query = mysqli_query($connect, "SELECT * FROM tb_mesin_forklift");
while ($data = mysqli_fetch_array($query)) {
?>
<label><label>
<option value="<?=$data['merk_mesin'];?>"><?php echo $data['merk_mesin'];?> (<?php echo $data['tahun_mesin'];?>)</option>
<?php
}
?>
</optgroup>
<optgroup label="Mesin Produksi">
<?php
$query = mysqli_query($connect, "SELECT * FROM tb_mesin_produksi");
while ($data = mysqli_fetch_array($query)) {
?>
<label><label>
<option value="<?=$data['nama_mesin'];?>"><?php echo $data['nama_mesin'];?> (<?php echo $data['tahun_mesin'];?>)</option>
<?php
}
?>
</optgroup>
</select>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">TAHUN</label>
</div>
<div class="col-sm-9">
<input class="form-control" type="text" id="tahunmes" name="tahunpg" placeholder="Masukkan Tahun Mesin" required autocomplete="off">
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">OPERATOR</label>
</div>
<div class="col-sm-9">
<select class="form-control selectpicker" type="text" name="oppg[]" id="oppg" multiple data-live-search="true" data-none-selected-text="Pilih Operator">
<?php
$query = mysqli_query($connect, "SELECT nama_op FROM tb_operator");
while ($data = mysqli_fetch_array($query)) {
?>
<option value="<?=$data['nama_op'];?>"><?php echo $data['nama_op'];?></option>
<?php
}
?>
</select>
</div>
</div>
<div id="dynamic_field">
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">KELUHAN</label>
</div>
<div class="col-sm-7">
<textarea class="form-control" type="text" name="keluhanpg[]" placeholder="Masukkan Keluhan" autocomplete="off" rows="3"></textarea>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">MEKANIK</label>
</div>
<div class="col-sm-7">
<select class="form-control selectpicker" type="text" name="mkpg[]" id="mkpg" multiple data-live-search="true" data-none-selected-text="Pilih Mekanik">
<?php
$query = mysqli_query($connect, "SELECT nama_mk FROM tb_mekanik");
while ($data = mysqli_fetch_array($query)) {
?>
<option value="<?=$data['nama_mk'];?>"><?php echo $data['nama_mk'];?></option>
<?php
}
?>
</select>
</div>
<div class="col-sm-2">
<button class="btn btn-success" type="button" name="add" id="add">
<i class="glyphicon glyphicon-plus"></i>
</button>
</div>
</div>
</div>
<div class="row form-group">
<div class="col-sm-3">
<label class="control-label modal-label">SHIFT</label>
</div>
<div class="col-sm-9">
<select class="form-control selectpicker" type="text" name="shiftpg" id="shiftpg" data-live-search="true">
<option selected value> -- Pilih Shift -- </option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<input type="hidden" class="form-control" name="statuspg" value="PROGRESS">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
<button type="submit" name="add" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save</a>
</div>
</form>
</div>
</div>
</div>
</div>
I use javascript to be able to add input fields because they are dynamics.
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<div id="row'+i+'"><div class="row form-group"><div class="col-sm-3"><label class="control-label modal-label">KELUHAN</label></div><div class="col-sm-7"><textarea class="form-control" type="text" name="keluhanpg[]" placeholder="Masukkan Keluhan" autocomplete="off" rows="3" required></textarea></div></div><div class="row form-group"><div class="col-sm-3"><label class="control-label modal-label">MEKANIK</label></div><div class="col-sm-7"><select class="form-control selectpicker" type="text" name="mkpg[]" id="mkpg" multiple data-live-search="true" data-none-selected-text="Pilih Mekanik" required> <?php $query = mysqli_query($connect, "SELECT nama_mk FROM tb_mekanik"); while ($data = mysqli_fetch_array($query)) { ?> <option value="<?=$data['nama_mk'];?>"><?php echo $data['nama_mk'];?></option> <?php } ?></select></div><div class="col-sm-2"><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove"><i class="fa fa fa-trash"></i></button></div></div></div>');
$(".selectpicker").selectpicker('refresh');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
// alert(button_id);
$('#row'+button_id+'').remove();
});
});
and this is the code for my input process.
<?php
session_start();
include_once('koneksi.php');
if(isset($_POST['add'])){
$kodepg = $_POST['kodepg'];
$noformpg = $_POST['noformpg'];
$waktupg = $_POST['waktupg'];
$deptpg = $_POST['deptpg'];
$lokasipg = $_POST['lokasipg'];
$merkpg = $_POST['merkpg'];
$tahunpg = $_POST['tahunpg'];
$op = isset($_POST['oppg']) && is_array($_POST['oppg']) ? $_POST['oppg'] : [];
$oppg = implode(", ", $op);
$keluhanpg = isset($_POST['keluhanpg']) && is_array($_POST['keluhanpg']) ? $_POST['keluhanpg'] : [];
$mk = isset($_POST['mkpg']) && is_array($_POST['mkpg']) ? $_POST['mkpg'] : [];
$mkpg = implode(", ", $mk);
$shiftpg = $_POST['shiftpg'];
$statuspg = $_POST['statuspg'];
foreach ($_POST['keluhanpg'] as $key => $value) {
$sql = "INSERT INTO tb_pg_cs (kode_pg,noform_pg,waktu_pg,dept_pg,lokasi_pg,merk_pg,tahun_pg,op_pg,keluhan_pg,mk_pg,shift_pg,status_pg) VALUES ('" . $kodepg . "','".$noformpg . "','".$waktupg . "','".$deptpg . "','".$lokasipg . "','".$merkpg . "','".$tahunpg . "','".$oppg . "','".$_POST['keluhanpg'][$key] . "','".$mkpg. "','".$shiftpg . "','".$statuspg . "')";
//use for MySQLi OOP
if($connect->query($sql)){
$_SESSION['success'] = 'Data added successfully';
}
else{
$_SESSION['error'] = 'Something went wrong while adding';
}
}
}
header("location: ".$base_url."index.php?page=pengajuan");
?>

MethodNotAllowedHttpException error in Laravel 5.7 using ajax [duplicate]

This question already has answers here:
Stop form refreshing page on submit
(20 answers)
How can I prevent refresh of page when button inside form is clicked?
(16 answers)
Closed 4 years ago.
Im trying to insert data with ajax, but i got error :
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException
This is my form in add-gallery.blade.php:
<form method="post" action="" enctype="multipart/form-data" autocomplete="off" class="mt-4 mb-4 card p-4">
<div class="form-group">
<div class="row">
<div class="col-md-2 align-self-center">
<label for="gallery_name">Name</label>
</div>
<div class="col-md-6 align-self-center">
<input type="text" name="gallery_name" id="gallery_name" class="form-control" placeholder="Type Gallery Name" />
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-2 align-self-center">
<label for="category">Category</label>
</div>
<div class="col-md-3 align-self-center">
<select class="form-control custom-select" name="category" id="category">
<option value="">Select Category</option>
<option value="Home">Home</option>
<option value="Office">Office</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-2 align-self-center">
<label for="subcategory">Sub Category</label>
</div>
<div class="col-md-3 align-self-center">
<select class="form-control custom-select" name="subcategory" id="subcategory">
<option value="">Select Sub Category</option>
<option value="Interior">Interior</option>
<option value="Eksterior">Eksterior</option>
<option value="Decoration">Decoration</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-2 align-self-center">
<label for="subsubcategory">Subsub Category</label>
</div>
<div class="col-md-3 align-self-center">
<select class="form-control custom-select" name="subsubcategory" id="subsubcategory">
<option value="">Select Subsub Category</option>
<option value="Room">Room</option>
<option value="Bathroom">Bathroom</option>
<option value="Kitchen">Kitchen</option>
<option value="Terrace">Terrace</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-2">
<label for="gallery_image">Image</label>
</div>
<div class="col-md-10">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail w-100" style="display: none;">
<img src="http://placehold.it/1186x800" />
</div>
<div id="toAnnotate" class="fileinput-preview fileinput-exists thumbnail w-100"></div>
<div>
<span class="btn btn-xs btn-success btn-file"><span class="fileinput-new">Select image</span><span class="fileinput-exists">Change</span><input type="file" name="gallery_image" id="gallery_image"></span>
Remove
</div>
</div>
<p><small><em>Image Size : 1186px (width), 800px (height)</em></small></p>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-2 align-self-center"> </div>
<div class="col-md-10 align-self-center">
<button type="button" class="btn btn-secondary">Cancel</button>
<button type="submit" class="btn btn-success" id="save">Save</button>
</div>
</div>
</div>
</form>
And below is my ajax in same page add-gallery.blade.php:
<script type="text/javascript">
$(document).ready(function (e) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
//Save
$('#save').on('click', function () {
var gallery_name = $('#gallery_name').val();
var category = $('#category').val();
var subcategory = $('#subcategory').val();
var subsubcategory = $('#subsubcategory').val();
var gallery_image = $('#gallery_image').val();
var dataSubmit = $(this).serialize();
var top = top;
var left = left;
var file_data = $('#gallery_image').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url : "{{ url('galleries/store') }}",
dataType : 'text', // what to expect back from the server
cache : false,
contentType : false,
processData : false,
data : form_data,
type : 'post',
dataType : 'json',
success: function (response) {
console.log(response);
},
error: function (response) {
console.log(response);
}
});
});
});
</script>
This is my routes file web.php:
Route::post('galleries/store','Dashboard\GalleryController#store');
And this is my controller GalleryController.php:
public function store(Request $request){
if($request->hasFile('gallery_image')){
$fileImage = $request->file('gallery_image');
$fileImageExtension = $fileImage->extension();
$renameFileImage = 'ImageGalleries'.time().'.'.$fileImageExtension;
$pathFileImage = $fileImage->storeAs('public/galleries_documents/images_galleries', $renameFileImage); /images_articles
$galleries = new Gallery;
$points = new Point;
$galleries->name = $request->gallery_name;
$galleries->category = $request->category;
$galleries->sub_category = $request->subcategory;
$galleries->sub_subcategory = $request->subsubcategory;
$galleries->image = $renameFileImage;
$galleries->lang_id = session('lang_id');
$galleries->save();
$lastInsertId = $galleries->id;
for($i = 0; $i < count($request->node); $i++) {
$node[] = [
'node_position_top' => $request->node[$i],
'node_position_left' => $request->node[$i],
'id_gallery' => $lastInsertId,
];
$points->lang_id = session('lang_id');
}
Point::insert($node); //save to table
print_r($node[]);
exit();
}
}

Ajax Multiple Submission of the form and get email about the form information

I have issues with submitting a form through ajax. I am submitting the same form two times and I want to email for the same form. On the first submission, I want the name, email, contact information and on the second submission of the form I want the city related information about the user . The form is working fine but I get the mail only when the user submits the form on the second attempt. When a user submits the forms through ajax I did not get any email information about the user name and email etc. Also, i am using a google captcha in that form. the code is working fine just I am not receiving any email on the first submission of the form . Need help..!! Any help would be appreciated Thanks..!!
Here is my code :-
if(isset($_POST['contraform']) && $_POST['contraform']=='submited'){
$secret = "6Lf0MAoUAAAAAMWzxj-kbdy-u32onIYhZ0TdzpDc";
$response = $_POST['g-recaptcha-response'];
$remoteip = $_SERVER['REMOTE_ADDR'];
$url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
$result = json_decode($url, TRUE);
if ($result['success'] == 1) {
if(isset($_POST["source"])){
$source = $_POST["source"];
}
if(isset($_POST["uname"])){
$name = $_POST["uname"];
}
if(isset($_POST["uemail"])){
$email = $_POST["uemail"];
}
if(isset($_POST["uphonefull"])){
$phone = $_POST["uphonefull"];
}
if(isset($_POST["utreatments"])){
$treatment = $_POST["utreatments"];
}
if(isset($_POST["form_name"])){
$form_name = $_POST["form_name"];
}
if(isset($_POST["ucity"])){
$city = $_POST["ucity"];
}
if(isset($_POST["uhospital"])){
$hospital = $_POST["uhospital"];
}
if(isset($_POST["thanks"])){
$thanks = $_POST["thanks"];
}
header_contact_forms($source,$name,$email,$phone,$treatment,$form_name, $city,$hospital);
}
}
Here is my form code :-
<form name="contraform" id="contraform" method="post" action="<?php bloginfo("template_directory"); ?>/forms/process.php">
<input type="hidden" name="form_name" id="form_name" value="Header Contact Form" class="basicformname" >
<input type="hidden" value="<?php echo $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" name="source" id="source" />
<input type="hidden" value="submited" name="basicform"/>
<div id="sf1" class="frm">
<fieldset>
<!--<legend>Take a step for a Good health.</legend>-->
<div class="wor-alert"><p class="text-center fill-fields">Please Fill All This Fields*</p></div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="abc-icon abc-user"></i></span>
<input type="text" placeholder="Name" id="uname" name="uname" class="form-control" autocomplete="off">
<span class="nameerr" id="unameerr"><?php echo #$_SESSION['unameerr'] ? $_SESSION['unameerr']:''; unset($_SESSION['unameerr']); ?></span>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="abc-icon abc-envelope"></i></span>
<input type="email" placeholder="Email" id="uemail" name="uemail" class="form-control" autocomplete="on">
<span class="emailerr" id="uemailerr"><?php echo #$_SESSION['uemailerr'] ? $_SESSION['uemailerr']:''; unset($_SESSION['uemailer']); ?></span>
</div>
</div>
<div class="form-group">
<input type="text" placeholder="Phone Number" id="uphone" name="uphone" class="uphone form-control" autocomplete="off">
<span class="phoneerr" id="uphoneerr"><?php echo #$_SESSION['uphoneerr'] ? $_SESSION['uphoneerr']:''; unset($_SESSION['uphoneerr']); ?></span>
<input type="hidden" class="uphonefull" name="uphonefull">
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon" id="basic-addon"><i class="abc-icon abc-user-md"></i></span>
<span class="plain-select">
<select name="utreatments" data-style="btn-primary" id="utreatments" class="form-control selectpicker">
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
</select>
</span>
</div>
</div>
<div class="col-md-12 text-center">
<div class="head-captcha">
<div id="recaptcha3"></div>
<input type="hidden" class="hiddenRecaptcha required" name="bhiddenRecaptcha" id="bhiddenRecaptcha">
</div>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn open1" type="button" id="head-frm-btn">Next <span class="fa fa-arrow-right"></span></button>
</div>
</fieldset>
</div>
<div id="sf2" class="frm" style="display: none;">
<fieldset>
<!--<legend>What treatment are you looking for?</legend>-->
<div class="form-group">
<span class="plain-select">
<select name="ucity" id="ucity" class="form-control">
<option value="none">Select City</option>
<option value="Mumbai">Mumbai</option>
<option value="Pune">Pune</option>
<option value="Banglore">Banglore</option>
<option value="Nagpur">Nagpur</option>
<option value="Goa">Goa</option>
</select>
</span>
</div>
<div class="form-group">
<span class="plain-select">
<select name="uhospital" id="uhospital" class="form-control">
<option value="">Select Hospital</option>
<option value="Hospital One">Hospital One</option>
<option value="Hospital Two">Hospital Two</option>
<option value="Hospital Three">Hospital Three</option>
<option value="Hospital Four">Hospital Four</option>
<option value="Hospital Five">Hospital Five</option>
</select>
</span>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn back2" type="button"><span class="fa fa-arrow-left"></span> Back</button>
<button class="btn open2" type="button" id="head-frm-btn-two">Next <span class="fa fa-arrow-right"></span></button>
</div>
</fieldset>
</div>
<div id="sf3" class="frm" style="display: none;">
<fieldset>
<!--<legend>Almost done</legend>-->
<div class="waittext">
<p>Our customer care executive will get in contact with you soon. Please bear with us.</p>
</div>
<div class="clearfix" style="height: 10px;clear: both;"></div>
<div class=" text-center">
<button class="btn back3" type="button"><span class="fa fa-arrow-left"></span> Back</button>
<input class="btn open3" value="Submit" type="submit" name="head_form_submit">
</div>
</fieldset>
</div>
</form>
Here is the Ajax Form Submission Code :-
var eml = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
$("#head-frm-btn, #head_form_submit").click(function () {
$(".wor-alert").hide();
var source = $("#source").val();
var uname = $("#uname").val();
var uemail = $("#uemail").val();
var uphone = $("#uphone").val();
// var ucity = $("#ucity").val();
//var uhospital = $("#uhospital").val();
var utreatments = $("#utreatments").val();
var uphonefull = $(".uphonefull").val();
var form_name = $("#form_name").val();
var basicform = "submited";
// Returns successful data submission message when the entered information is stored in database.
var dataString = 'uname=' + uname + '&uemail=' + uemail + '&uphonefull=' + uphonefull +'&utreatments='+utreatments+'&basicform='+basicform+'&form_name ='+form_name + '&source=' + source;
if (uname == '' || uemail == '' || uphone == '' || eml.test($.trim(uemail)) == false) {
$(".wor-alert").stop().fadeIn('slow');
return false;
}else {
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "wp-content/themes/theme-name/forms/process.php",
data: dataString,
cache: false,
success: function (response) {
$('#response').html(response);
}
});
}
return false;
});
header_contact_froms function :-
function header_contact_forms($source,$name,$email,$phone,$treatment,$form_name, $city,$hospital){
include('mail.php');
if($city){include('thankyou.php');}
}

Select Option in foreach only get value in last select option (Code Igniter)

Why I can't post a select option value in foreach loop? It just save the last select option value.
It's only save the "Rutinitas" value in Printer (which is the last select option), but doesn't save the CPU or Mouse "Rutinitas" option value.
This is the controller
public function tampilkanKomponen2($tgl_cek,$nama_alat) {
if($this->session->userdata('logged_operator')) {
$session_data = $this->session->userdata('logged_operator');
$nip = $session_data['nip'];
$nama = $session_data['username'];
$hasil = $this->operator_model->getByAlat($nama_alat)->result();
$hasil2 = $this->operator_model->showPengecekan()->result();
$data =array (
'nip' => $nip,
'nama' => $nama,
'hasil2' => $hasil2,
'hasil' => $hasil,
//'hasil2' => $hasil2,
'tgl_cek' => $tgl_cek,
'nama_alat' => $nama_alat
//'id_komponen2' =>$id_komponen
);
$this->load->view('tambah_pengecekan3_v',$data);
}
else {
echo "<script> history.go(-1); </script>";
}
}
public function simpanPengecekan() {
if($this->operator_model->check_logged()==TRUE) {
$session_data = $this->session->userdata('logged_operator');
$nip = $session_data['nip'];
$id_komponen = $this->input->post('id_komponen');
$tgl_cek = $this->input->post('tgl_cek');
$rutinitas = $this->input->post('rutinitas');
$kriteria = $this->input->post('kriteria');
$tindakan = $this->input->post('tindakan');
$nama_alat = $this->input->post('nama_alat');
$data =array (
'id_komponen' => $id_komponen,
'tgl_cek' => $tgl_cek,
'rutinitas' => $rutinitas,
'kriteria' => $kriteria,
'tindakan' => $tindakan,
'operator' => $nip
);
$hasil = $this->operator_model->tambahPengecekan($data);
//redirect(base_url('operator'));
$this->tampilkanKomponen2($tgl_cek,$nama_alat);
//redirect_back();
}
else {
echo "<script> history.go(-1); </script>";
}
}
This is the view
<div class="container">
<h4> Buat Pengecekan Baru </h4>
<form method="post" action="simpanPengecekan">
<div style="border-bottom : solid black 1px;margin-bottom:30px;">
<div class="row row-form">
<div class="col-md-2"> <label class="control-label"> Tanggal Pengecekan </label> </div>
<div class="col-md-10"><input type="text" name="tgl_cek" class="form-control datepicker" value="<?php echo $tgl_cek; ?>"></div>
</div>
<div class="row row-form">
<div class="col-md-2"> <label class="control-label">Nama Alat</label> </div>
<div class="col-md-10">
<input type="text" name="nama_alat" class="form-control" value="<?php echo $nama_alat; ?>" readonly>
</div>
</div>
<button type="button" class="btn btn-primary"> Pilih Alat </button>
</div>
<h6>Komponen</h6>
<?php $no=0;foreach($hasil as $row) { ?>
<div>
<input type="hidden" name="id_komponen" id="id<?php echo +$no;?>" class="form-control" value="<?php echo $row->id_komponen;?>" disabled>
<p> <?php echo $row->nama_komponen;?> </p>
<button type="button" class="btn btn-primary" onClick="javascript:showhide('pengecekan','<?php echo +$no;?>')"> Isi Pemeriksaan </button>
<div id="pengecekan<?php echo +$no;?>" style="display:none;">
<div class="row row-form">
<div class="col-md-2"> <label class="control-label"> Rutinitas </label> </div>
<div class="col-md-10">
<select name="rutinitas" class="form-control">
<option value="Harian"> Harian </option>
<option value="Mingguan"> Mingguan </option>
<option value="Bulanan"> Bulanan </option>
<option value="3 Bulanan"> 3 Bulanan </option>
<option value="6 Bulanan"> 6 Bulanan </option>
<option value="Tahunan"> Tahunan </option>
</select>
</div>
</div>
<div class="row row-form">
<div class="col-md-2"> <label class="control-label"> Kriteria </label> </div>
<div class="col-md-10"><input type="text" class="form-control" name="kriteria"></div>
</div>
<div class="row row-form">
<div class="col-md-2"> <label class="control-label"> Tindakan </label> </div>
<div class="col-md-10"><input type="text" class="form-control" name="tindakan"></div>
</div>
<input type="submit" class="btn btn-primary" value="Simpan"> <br><br><br>
</div>
</div>
<?php $no++; } ?>
</form>
<br><button type="button" class="btn btn-wide btn-primary btn-batal">Selesai</button> <br> <br>
<button type="button" class="btn btn-wide btn-default btn-batal">Batal</button>
</div>
This is the model
THis is the result in database
Change select name to rutinitas[]
<select name="rutinitas[]" class="form-control">
<option value="Harian"> Harian </option>
<option value="Mingguan"> Mingguan </option>
<option value="Bulanan"> Bulanan </option>
<option value="3 Bulanan"> 3 Bulanan </option>
<option value="6 Bulanan"> 6 Bulanan </option>
<option value="Tahunan"> Tahunan </option>
</select>
if you want to insert all selected value in same column of table then use implode
$rutinitas = implode(',',$this->input->post('rutinitas'));

PHP Multiple input search to query mysql database

I'm trying to query Mysql with a form with multiple input search but for some reason, in the end, nothing come out.
I'm using some code that I found here on stack - PHP Multiple input search
Here my html form
<form id="SearchFlightForm" method="post" action="searchresult.php" accept-charset='UTF-8'>
<div class="col-md-12 product-search-title">Search For Flights Deals </div>
<div class="col-md-12 search-col-padding">
<label class="radio-inline">
<input type="radio" name="inlineradiooptions" id="inlineRadio1" value="One Way"> One Way</label>
<label class="radio-inline">
<input type="radio" name="inlineradiooptions" id="inlineRadio2" value="Round Trip"> Round Trip</label>
</div>
<div class="clearfix"></div>
<div class="col-md-6 col-sm-6 search-col-padding">
<label>Leaving From</label>
<div class="input-group">
<input type="text" name="departure_city" class="form-control" required placeholder="E.g. London">
<span class="input-group-addon"><i class="fa fa-map-marker fa-fw"></i></span></div></div>
<div class="col-md-6 col-sm-6 search-col-padding">
<label>Leaving To</label>
<div class="input-group">
<input type="text" name="destination_city" class="form-control" required placeholder="E.g. New York">
<span class="input-group-addon"><i class="fa fa-map-marker fa-fw"></i></span></div></div>
<div class="clearfix"></div>
<div class="col-md-6 col-sm-6 search-col-padding">
<label>Departure</label>
<div class="input-group">
<input type="text" id="departure_date" name="departure_date" class="form-control" placeholder="DD/MM/YYYY">
<span class="input-group-addon"><i class="fa fa-calendar fa-fw"></i></span>
</div>
</div>
<div class="col-md-6 col-sm-6 search-col-padding">
<label>Return</label>
<div class="input-group">
<input type="text" id="return_date" class="form-control" name="return_date" placeholder="DD/MM/YYYY">
<span class="input-group-addon"><i class="fa fa-calendar fa-fw"></i></span>
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-3 col-sm-3 search-col-padding">
<label>Adult</label><br>
<input id="adult_count" name="adult_count" value="1" class="form-control quantity-padding">
</div>
<div class="col-md-3 col-sm-3 search-col-padding">
<label>Child</label><br>
<input type="text" id="child_count" name="child_count" value="0" class="form-control quantity-padding">
</div>
<div class="col-md-3 col-sm-3 search-col-padding">
<label>Infants</label><br>
<select class="selectpicker" name="infants">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div class="col-md-3 col-sm-3 search-col-padding">
<label>Class</label><br>
<select class="selectpicker" name="flightclass">
<option value="economy">Economy</option>
<option value="business">Business</option>
</select>
</div>
<div class="clearfix"></div>
<div class="col-md-12 search-col-padding">
<button type="submit" name="submit" id="submit" class="search-button btn transition-effect">Search Flights</button>
</div>
<div class="clearfix"></div>
</form>
Here the action file
<?php
// Process the search query
include('ligacaoBD.php');
if(isset($_POST['submit'])) {
// define the list of fields
$fields = array('id', 'inlineradiooptions', 'departure_city', 'destination_city',
'departure_date', 'return_date', 'adult_count', 'child_count', 'infants', 'flightclass');
$conditions = array();
}
// builds the query
$query = "SELECT id, airline_image, flight_class, departure_date, departure_time, departure_location, arrival_date, arrival_time, arrival_location, flight_stops, flight_duration FROM flight_details WHERE id=1";
// if there are conditions defined
if(count($conditions) > 0) {
// append the conditions
$query .= " WHERE " . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
}
// loop through the defined fields
foreach($fields as $field){
// if the field is set and not empty
if(isset($_POST[$field]) && $_POST[$field] != '') {
// create a new condition while escaping the value inputed by the user (SQL Injection)
$conditions[] = "'$field' LIKE '%" . mysqli_real_escape_string($mysqli_link, $_POST[$field]) . "%'";
}
}
// if there are conditions defined
if(count($conditions) > 0) {
// append the conditions
$query .= " WHERE " . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
}
$result = mysqli_query($mysqli_link, $query);
mysqli_close($mysqli_link);
if(isset($_POST['submit'])) {
while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$flight_image = $row['flight_image'];
$flight_class = $row['flight_class'];
$departure_date = $row['departure_date'];
$departure_time = $row['departure_time'];
$departure_location = $row['departure_location'];
$arrival_date = $row['arrival_date'];
$arrival_time = $row['arrival_time'];
$arrival_location = $row['arrival_location'];
$flight_stops = $row['flight_stops'];
$flight_duration = $row['flight_duration'];
echo "<b>id: $id</b><br> image: $flight_image<br>Class: $flightclass<br>departure_date: $departure_date<br>departure_time: $departure_time<br>departure_location: $departure_location<br>arrival_date: $arrival_date<br>arrival_time: $arrival_time<br>arrival_location: $arrival_location<br>flight_stops: $flight_stops<br>flight_duration: $flight_duration<hr><br>";
}
}
?>
Please could anyone give a hint of what I'm doing wrong.
thanks S.C

Categories