PHP Multiple input search to query mysql database - php

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

Related

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

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....

Is there a best way i can select all the topics and questions on each topics and display them onec in php

I have two tables "setTearmlyExam.php" and "setTopics.php", what i want to do is this:
in the "setTearmlyExam.php" i have a form with two select options, a radio button and a start button. each select option has an option from the database.
what am trying to do here is: when the start button is cliked, its will redirect to the "setTopics.php" and there display all the topics and question using the selected information on the "setTearmlyExam.php".
I have two table where am selecting the data that will be displayed in the "setTopics.php"
The olevelguestion:
Topics:
"setTearmlyExam.php"
<form action="setTopics.php " method="POST" class="col s12" center>
<!-- setTopics.php -->
<div>
<label>Select Term</label>
<select name="terms" class="browser-default" required>
<option value="" disabled selected>Select Term</option>
<?php
// Selecting terms from database
$select_terms = $db->query("SELECT * FROM term");
if (!$select_terms) {
die('QUERY FAILED' .mysqli_error($db));
}
// Ends
// Using while loop tp fatch and loop term titles
while ($row = $select_terms->fetch_assoc()) {
$termId = $row['termId'];
$terms = $row['terms'];
echo "<option value='{$termId}'>{$terms}</option>";
}
?>
</select>
<label>Select Subject</label>
<select name="subjects" class="browser-default" required>
<option value="" disabled selected>Select Subject</option>
<?php
// Selecting subjects from database......
$Select_subjects = $db->query("SELECT *
FROM subjects
ORDER BY subj ASC");
if (!$Select_subjects) {
die("QUERY FAILED" . mysqli_error($db));
}
// Ends..
// Using while loop to fetch the subjects title
while ($rw = $Select_subjects->fetch_assoc()) {
$subjId = $rw['subjId'];
$subj = $rw['subj'];
echo "<option value='{$subjId}'>{$subj}</option>";
}
?>
</select>
</div>
<div class="ST">
<div class="col 6">
<label>
<input class="with-gap" name="Examtype" value="O" type="radio" required/>
<span>Objective</span>
</label>
</div>
<div class="col 12">
<label>
<input class="with-gap" name="Examtype" value="T" type="radio" />
<span>Theory</span>
</label>
</div>
</div>
</div>
</div>
<div class="card-action centerTxt">
<button class="btn waves-effect waves-light " type="submit" name="start">Start
<i class="material-icons right">send</i>
</button>
</div>
</div>
</form>
"setTopics.php"
<form action="">
<div class="card-content black-text">
<span class="card-title centerTxt">
<h4>Subject Title</h4>
</span>
<div>
<!-- Dropdown Trigger -->
<label for="">
<h6 class="" style="font-size: 15px; color:black;">CLICK ON EACH TOPIC TO SELECT QUESTIONS. <span style="color:red; font-size: 13px;">Don't Select more than 50 Questions.</span></h6>
<?php
$terms = 'terms';
$subjects = 'subjects';
// $Examtype = $_GET['Examtype'];
if ($Examtype = 'O') {
$topics = $db->query('SELECT * FROM topics WHERE ');
}
?>
<h5 class='dropdown-trigger ' style="color:#149fd5;" data-target='dropdown1'>Topics Here</h5>
</label>
<!-- Dropdown Structure -->
<ul id='dropdown1' class='dropdown-content'>
<p>
<label>
<input type="checkbox" value="" />
<span>Yellow</span>
</label>
</p>
</ul>
</div>
</div>
<div class="card-action centerTxt">
<button class="btn waves-effect waves-light " type="submit" name="start">Next
<i class="material-icons right">send</i>
</button>
</div>
</form>

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");
?>

How to set the input disable with condition?

I have a problem to set the input box become disable with condition. My condition is if the value is 0, then the input box will become disable. I have add readonly in the html box, but it also can't work.
Below is what I am tried the coding, Hope anyone can guide me to check which part I am getting wrong.:
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;"> *</span></label>
<div class="col-lg-3">
<select class="form-control blank" id="parentid" name="parentid" title="parentid">
<option>Please Select</option>
<option value="0">New Category</option>
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
echo '<option value="' . $rs_incharge['id'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select>
<!--<input type="text" class="form-control blank" id="parentid" name="parentid" title="parentid" onblur="capitalize(this.id, this.value);">-->
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Code:</label>
<div class="col-lg-3">
<?php
if($rs_incharge['id'] != '0'){
<input type='text' class='form-control' id='activity_code' name='activity_code' title='activity_code'>
}else($rs_incharge['id'] = '0'){
<input type='text' class='form-control' id='activity_code' name='activity_code' title='activity_code' readonly>
}
?>
</div>
</div>
Actually I want the output like below the picture:
Your code contains many errors.
And your question is not clear.
If you want to make input boxes as readonly when you select 'New Category' ( value is 0), and otherwise make input boxes editable, you need to use javascript or jQuery.
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;"> *</span></label>
<div class="col-lg-3">
<select class="form-control blank" id="parentid" name="parentid" title="parentid">
<option value="0">New Category</option>
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
echo '<option value="' . $rs_incharge['id'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Code:</label>
<div class="col-lg-3">
<input type='text' class='form-control' id='activity_code' name='activity_code' title='activity_code'>
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Name:</label>
<div class="col-lg-3">
<input type='text' class='form-control' id='activity_name' name='activity_name' title='activity_name'>
</div>
</div>
jQuery code
$(document).ready(function(){
$('#parentid').change(function(){
if ( $(this).val() == '0' ){
$('#activity_code, #activity_name').attr('readonly', 'readonly');
}else{
$('#activity_code, #activity_name').removeAttr('readonly');
}
});
});

getting total value after entering the values in the form and inserting into database using codeigniter

If i select Local Sales from dorpdown and enter DEF, GHI values then the sum of DEF,GHI should be displayed in total value or if i select Inter State,Stock Transfers from dropdown then if we enter ABC value that value should be displayed in total value or else if we select JOB WORK,EXEMPTED SALES from dropdown then the total value should be displayed as zero. The total value which ever we are getting that should be inserted into database.
Controller:
function addinvoice()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('user','User');
$this->form_validation->set_rules('freight_charges');
$this->form_validation->set_rules('abc');
$this->form_validation->set_rules('def');
$this->form_validation->set_rules('ghi');
$this->form_validation->set_rules('total');
if($this->form_validation->run()== FALSE)
{
$data['mainpage']='invoice';
$data['mode']='add';
$this->load->view('templates/template',$data);
}
else
{
$this -> invoice_model -> insert();
$this->flash->success('<h2> Details added Successfully!</h2>');
redirect('invoice');
}
}
Model:
function insert()
{
$data['total']=0;
$data['user'] = $this->input->post('user');
$data['ghi'] = ($this->input->post('ghi'))?$this->input->post('ghi'):0;
$data['abc'] = ($this->input->post('abc'))?$this->input->post('abc'):0;
$data['def'] = ($this->input->post('def'))?$this->input->post('def'):0;
$data['total'] = $data['ghi'] + $data['abc'] + $data['def'];
$data['freight_charges'] = $this->input->post('freight_charges');
$this->db->insert('invoice',$data);
}
View:
<script>
function showRequiredOption(cval)
{
if((cval=='interstate') || (cval == "stocktransfers"))
{
$('#ghi').hide();
$('#def').hide();
$('#abc').show();
}
else if ((cval=='exemptedsales') || (cval=="zeroratedsales") ||(cval=="jobwork"))
{
$('#ghi').hide();
$('#def').hide();
$('#abc').hide();
}
else
{
$('#abc').hide();
$('#ghi').show();
$('#def').show();
}
}
</script>
<div class="col-md-9 col-md-offset-2">
<div id="legend">
<legend class="">Profile Information</legend>
</div>
<form role="form" action="<?php echo site_url();?>invoice/addinvoice" method="post" class="form-horizontal" id="location" method="post" accept-charset="utf-8">
<div class="form-group">
<label class="control-label col-sm-2 " for="user">User</label>
<div class="col-sm-4 col-sm-offset-1">
<select id="user" name="user" onchange="showRequiredOption(this.value)">
<option value="employee">Local Sales</option>
<option value="interstate">Inter state</option>
<option value="stocktransfers">Stock transfers</option>
<option value="exemptedsales">Exempted Sales</option>
<option value="zeroratedcompany">Zero Rated Sales</option>
<option value="jobwork">Job Work</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="freight_charges">Freight Charges</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="freight_charges" name="freight_charges" value="<?php echo set_value('freight_charges');?>" />
</div>
</div>
<div class="form-group" id="abc" style="display:none;">
<label class="control-label col-sm-2 " for="abc">ABC</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="abc" name="abc" value="<?php echo set_value('abc');?>"/ >
</div>
</div>
<div class="form-group" id="def">
<label class="control-label col-sm-2 " for="def">DEF </label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="def" name="def" value="<?php echo set_value('def');?>"/ >
</div>
</div>
<div class="form-group" id="ghi">
<label class="control-label col-sm-2 " for="ghi">GHI</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="ghi" name="ghi" value="<?php echo set_value('ghi');?>"/ >
</div>
</div>
<div class="form-group" id="cgst">
<label class="control-label col-sm-2 " for="total">Total</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" name="total" >
</div>
</div>
<button id="submit" type="submit" class="btn" name="submit">Submit</button>
</form>
</div>
Whatever values i have selected from dropdown only those values to be inserted into database and the rest of the values should be inserted as zero in the database.
Actually i am not getting how to do these can anyone check this.Thanks in Advance.
you should check for posted values in your php code as:
$total = 0;
if($this->input->post('this')){
$total = $this->input->post('this');//value in total
}
if($this->input->post('this2')){
$total += $this->input->post('this2');//value in total
}
and at the end send $total value in db as well.
in short php if else tags you can set variables like;
$this = ($this->input->post('this'))?$this->input->post('this'):0;
$this2 = ($this->input->post('this2'))?$this->input->post('this2'):0;
and then at the end you can make total of them and save them to database. OR as suggested above in comments that make your columns as DEFAULT 0 in your table.
------- IN YOUR CASE------
function insert()
{
$data['total']=0;
$data['user'] = $this->input->post('user');
$data['ghi'] = ($this->input->post('ghi'))?$this->input->post('ghi'):0;
$data['abc'] = ($this->input->post('abc'))?$this->input->post('abc'):0;
$data['def'] = ($this->input->post('def'))?$this->input->post('def'):0;
$data['total'] = $data['ghi'] + $data['abc'] + $data['def'];
$data['freight_charges'] = $this->input->post('freight_charges');
$this->db->insert('invoice',$data);
}
---------------IN JavaScript------------
on your event handler you can sum these by their IDs.
var total = parseInt($('#ghi').val())+parseInt($('#def').val());
and then show this total in your total div
$('#yourTotalDiv').text(total);
Displaying total amount on enter the details.
<script>
function showRequiredOption(cval)
{
if((cval=='interstate') || (cval == "stocktransfers"))
{
$('#ghi').hide();
$('#def').hide();
$('#abc').show();
}
else if ((cval=='exemptedsales') || (cval=="zeroratedsales") || (cval=="jobwork"))
{
$('#ghi').hide();
$('#def').hide();
$('#abc').hide();
}
else
{
$('#abc').hide();
$('#ghi').show();
$('#def').show();
}
}
</script>
<script>
$(document).ready(function(){
//iterate through each textboxes and add keyup
//handler to trigger sum event
$(".txt").each(function() {
$(this).keyup(function(){
calculateSum();
});
});
});
function calculateSum() {
var sum = 0;
//iterate through each textboxes and add the values
$(".txt").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
sum += parseFloat(this.value);
}
});
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#sum").html(sum.toFixed(2));
}
</script>
<div class="col-md-9 col-md-offset-2">
<div id="legend">
<legend class="">Profile Information</legend>
</div>
<form role="form" action="<?php echo site_url();?>invoice/addinvoice" method="post" class="form-horizontal" id="location" method="post" accept-charset="utf-8">
<div class="form-group">
<label class="control-label col-sm-2 " for="user">User</label>
<div class="col-sm-4 col-sm-offset-1">
<select id="user" name="user" onchange="showRequiredOption(this.value)">
<option value="employee">Local Sales</option>
<option value="interstate">Inter state</option>
<option value="stocktransfers">Stock transfers</option>
<option value="exemptedsales">Exempted Sales</option>
<option value="zeroratedcompany">Zero Rated Sales</option>
<option value="jobwork">Job Work</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="freight_charges">Freight Charges</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control txt" id="freight_charges" name="freight_charges" value="<?php echo set_value('freight_charges');?>" />
</div>
</div>
<div class="form-group" id="abc" style="display:none;">
<label class="control-label col-sm-2 " for="abc">ABC</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control txt" id="abc" name="abc" value="<?php echo set_value('abc');?>"/ >
</div>
</div>
<div class="form-group" id="def">
<label class="control-label col-sm-2 " for="def">DEF </label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control txt" id="def" name="def" value="<?php echo set_value('def');?>"/ >
</div>
</div>
<div class="form-group" id="ghi">
<label class="control-label col-sm-2 " for="ghi">GHI</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control txt" id="ghi" name="ghi" value="<?php echo set_value('ghi');?>"/ >
</div>
</div>
<div class="form-group" id="summation">
<label class="control-label col-sm-2 " for="total">Total</label>
<div class="col-sm-4 col-sm-offset-1">
<span id="sum" class="form-control" type="text" name="total">0</span>
</div>
</div>
<button id="submit" type="submit" class="btn" name="submit">Submit</button>
</form>
</div>

Categories