How to set the input disable with condition? - php

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');
}
});
});

Related

How to make the option value include more than 1 value?

I have a problem to show the 4 different data in the input fields. I have create the onchange function to get the 1 value in the input field, but how to get other different data to show in the other fields:
Below is my coding:
<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 onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option>Please Select</option>
<option value="New Category_value">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'];
$function_code_select = $rs_incharge['function_code'];
$function_name_select = $rs_incharge['function_name']
$activity_code_select = $rs_incharge['activity_code']
$activity_name_select = $rs_incharge['activity_name']
echo '<option value="' . $rs_incharge['function_code'] . '">' . $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">Function Code:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="function_code" name="function_code" title="function_code" value="">
</div>
</div>
<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Function Name:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="function_name" name="function_name" title="function_name">
</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>
<script>
function getComboA(selectObject) {
var value = selectObject.value;
document.getElementById("function_code").value =value;
document.getElementById("function_name").value =value;
document.getElementById("activity_code").value =value;
document.getElementById("activity_name").value =value;
}
<script>
Now my output show me the 4 same data in the input fields, like below the picture:
Output 1
Actually I want to show the 4 different data, now I just grab the first data using this code $rs_incharge['function_code'], how let these $rs_incharge['function_name'], $rs_incharge['activity_code']$rs_incharge['activity_name'] can show the different field, hope someone can guide me solve this problem. Thanks.
You should store the data in additional attributes in the <option> itself then get the values onchange and then perform your operation. This should help you.
<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option selected disabled>Please Select</option>
<!--Make this selected by default-->
<option value="New Category_value" data-act_code="Your-default-act-code" data-act_name="Your-default-act-name">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'];
$function_code_select = $rs_incharge['function_code'];
$function_name_select = $rs_incharge['function_name']
$activity_code_select = $rs_incharge['activity_code']
$activity_name_select = $rs_incharge['activity_name']
// save the data here↓↓ in data attributes
echo "<option value='{$function_code_select}' data-act_code='{$activity_code_select}' data-act_name='{$activity_name_select}'>{$function_name_select}</option>";
}
?>
</select>
Then in your script
<script>
function getComboA(selectObject) {
let func_code = selectObject.value; // get the value of selected option
let func_name = selectObject.selectedOptions[0].textContent; // get the text of selected option
let act_code = selectObject.selectedOptions[0].getAttribute("data-act_code"); // get attribute of selected option
let act_name = selectObject.selectedOptions[0].getAttribute("data-act_name"); // get attribute of selected option
document.getElementById("function_code").value = func_code;
document.getElementById("function_name").value = func_name;
document.getElementById("activity_code").value = act_code;
document.getElementById("activity_name").value = act_name;
}
</script>
Below is the quick demo of what I am talking about-
function getComboA(selectObject) {
let func_code = selectObject.value; // get the value of selected option
let func_name = selectObject.selectedOptions[0].textContent; // get the text of selected option
let act_code = selectObject.selectedOptions[0].getAttribute("data-act_code"); // get attribute of selected option
let act_name = selectObject.selectedOptions[0].getAttribute("data-act_name"); // get attribute of selected option
document.getElementById("function_code").value = func_code;
document.getElementById("function_name").value = func_name;
document.getElementById("activity_code").value = act_code;
document.getElementById("activity_name").value = act_name;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option selected disabled>Please Select</option>
<!--Make this selected by default-->
<option value="New Category_value" data-act_code="Your-default-act-code" data-act_name="Your-default-act-name">New Category</option>
<!-- Provide default values here -->
<!-- After giving values via PHP your HTML code should look like this-->
<option value='Function Code 1' data-act_code='Activity Code 1' data-act_name='Activity Name 1'>Function Name 1</option>
<option value='Function Code 2' data-act_code='Activity Code 2' data-act_name='Activity Name 2'>Function Name 2</option>
<option value='Function Code 3' data-act_code='Activity Code 3' data-act_name='Activity Name 3'>Function Name 3</option>
</select>
Function Code: <input type="text" class="form-control" id="function_code" name="function_code" title="function_code" /><br>
Function Name: <input type="text" class="form-control" id="function_name" name="function_name" title="function_name" /><br>
Activity Code: <input type="text" class="form-control" id="activity_code" name="activity_code" title="activity_code" /><br>
Activity Name: <input type="text" class="form-control" id="activity_name" name="activity_name" title="activity_name" />

Onchange echo value probelm

I have a problem to create the select box then get the value to show in the other input field.
Below is what I have tried the code:
<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'];
$function_code_select = $rs_incharge['function_code'];
echo '<option value="' . $rs_incharge['category_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">Function Code:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="function_code" name="function_code" title="function_code" value="<?php echo $function_code_select;?>">
</div>
</div>
I have define $function_code_select = $rs_incharge['function_code']; because I need to get the function code number to show in the second input field, so that the second input field I have written echo $function_code_select; to show the value, but it cannot work. Hope someone can guide me how to solve it. Thanks.
Below is the sample what I need to show the output. If work it can show me correct the function_code in the second input field:
You can't do that with PHP, it's a server-side script lang.
Add some javascript to make the magic happen:
<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 onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
<option>Please Select</option>
<option value="New Category_value">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'];
$function_code_select = $rs_incharge['function_code'];
echo '<option value="' . $rs_incharge['category_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">Function Code:</label>
<div class="col-lg-3">
<input type="text" class="form-control" id="function_code" name="function_code" title="function_code" value="<?php echo $function_code_select;?>">
</div>
</div>
<script>
function getComboA(selectObject) {
var value = selectObject.value;
document.getElementById("function_code").value =value;
}
</script>
If you need to keep value="", you can add an extra attribute like data-function-code=""
<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 onchange="getComboA()"
class="form-control blank"
id="parentid"
name="parentid"
title="parentid">
<option>Please Select</option>
<option value="New Category_value"
data-function-code="' . $rs_incharge['function_code'] . '">
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'];
$function_code_select = $rs_incharge['function_code'];
echo '<option value="' . $rs_incharge['category_id'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="cp1"
class="control-label col-lg-4">Function Code:</label>
<div class="col-lg-3">
<input type="text"
class="form-control"
id="function_code"
name="function_code"
title="function_code"
value="<?php echo $function_code_select;?>">
</div>
</div>
<script>
function getComboA() {
var sel = document.getElementById('parentid');
var selected = sel.options[sel.selectedIndex];
var data = selected.getAttribute('data-function-code');
document.getElementById("function_code").value =data;
}
</script>

Update Table Row Value with Post Button

I have some data in my mysql database which I am displaying in table using PHP like below
$requirement_qry="SELECT t1.*, t2.id as unit_id, t2.name as unit_name FROM `tbl_requirements`
t1 INNER JOIN tbl_units AS t2 on unit_type = t2.id AND project_id='".$_GET['project_id']."' AND user_id='".$userid."'";
$requirement_result=mysqli_query($mysqli,$requirement_qry);
<form action="" name="addeditcategory" method="post" class="form form-horizontal" enctype="multipart/form-data">
<input type="hidden" name="project_id" value="<?php echo $_GET['project_id'];?>" />
<div class="section">
<div class="section-body">
<div class="form-group">
<label class="col-md-3 control-label" >Project Name :-</label>
<div class="col-md-6">
<input type="text" name="name" id="name" value="<?php if(isset($_GET['project_id'])){echo $row['name'];}?> " class="form-control" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" >Location :-</label>
<div class="col-md-6">
<input type="text" name="location" id="location" value="<?php if(isset($_GET['project_id'])){echo $row['location'];}?> " class="form-control" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Project Status :-</label>
<div class="col-md-6">
<select name="status" id="status" class="select2" disabled>
<?php if (!isset($_GET['project_id'])) { ?>
<option value="1">--Project Status--</option>
<?php } ?>
<option value="1" <?php echo $row['status'] == '1' ? 'selected' : ''; ?> >Open</option>
<option value="2" <?php echo $row['status'] == '2' ? 'selected' : ''; ?> >Closed</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label class="control-label">Project Details :-</label>
</div>
<div class="col-md-6">
<textarea name="details" id="details" rows="4" class="form-control" disabled><?php echo stripslashes($row['details']);?></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label class="control-label">Project Requirements :-</label>
</div>
<div class="col-md-6">
<table id="t01">
<thead>
<tr>
<th>#</th>
<th>Requirements</th>
<th>Required</th>
<th>Sent</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
while ($row1 = mysqli_fetch_array($requirement_result))
{
$id = $row1['id'];
$unit_name = $row1['unit_name'];
echo '<input type="hidden" name="reqId" id= "reqId" value="'.$id.'" />';
echo '<tr>
<td>'.$no.'</td>
<td>'.$row1['name'].'</td>
<td>'.$row1['unit_required']." ".$unit_name.'</td>
<td><input type="number" id = "received" name = "received" value ="'.$row1['unit_received'].'"/></td>
<td><button type="submit" name="submit" class="btn btn-primary" style="padding:5px 10px;">Submit</button></td>
</tr>';
$no++;
}?>
</tbody>
</table>
</div>
</div>
</div>
</form>
Above code is properly displaying my data. I have one field value from row need to update using submit button. Its working fine if there only one row...if there multiple row, its not updating data of it except last row.
My submit code is like below
if(isset($_POST['submit']) and isset($_POST['project_id']))
{
$projectId = $_GET['project_id'];
$data = array(
'unit_received' => $_POST['received']
);
$unit_edit=Update('tbl_requirements', $data, " WHERE id = '".$_POST['reqId']."'");
print_r($unit_edit);
echo $unit_edit;
if ($unit_edit > 0)
{
$_SESSION['msg']="11";
header( "Location:view_open_project.php?project_id=".$_POST['project_id']);
exit;
}
}
I am little new in PHP, Let me know if someone can help me for solve the bug.
Thanks a lot :)
As #Saji has already stated, you are replicating your name through the loop.You should rather use
echo '<input type="hidden" name="reqId[]" id= "reqId" value="'.$id.'" />';
Note the [] brackets that were appended so that you create an array of names.
To update, you need a loop like
for($i=0;$i<count($_POST['reqId']);$i++){
$unit_edit=Update('tbl_requirements', $data, " WHERE id = '".$_POST['reqId'][$i]."'");
}
What you can do is just create a hidden form where you will keep original elements. When the save button click you just find the actual value and set this to the elements inside hidden form, then trigger the submit button inside hidden form.
Note the changes I have done on your code.
1.Removed the hidden element reqId from inside loop.
2.Given a class name to the button inside loop and changed the button type from submit to button. Added data attribute data-id="'.$id.'".
3.The input received inside the loop has given unique id and name by concatenated the $id.
4.Created hidden form with hidden input in it. It has your actual input names.
5.Created a jquery function to attach click event to the button inside loop.
6.Moved the hidden element project_id to inside hidden form.
Now see the below code for more details. Hope this will help you..
<form action="" name="addeditcategory" method="post" class="form form-horizontal" enctype="multipart/form-data">
<input type="hidden" name="project_id" value="<?php echo $_GET['project_id']; ?>"/>
<div class="section">
<div class="section-body">
<div class="form-group">
<label class="col-md-3 control-label">Project Name :-</label>
<div class="col-md-6">
<input type="text" name="name" id="name" value="<?php if (isset($_GET['project_id'])) {
echo $row['name'];
} ?> " class="form-control" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Location :-</label>
<div class="col-md-6">
<input type="text" name="location" id="location" value="<?php if (isset($_GET['project_id'])) {
echo $row['location'];
} ?> " class="form-control" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Project Status :-</label>
<div class="col-md-6">
<select name="status" id="status" class="select2" disabled>
<?php if (!isset($_GET['project_id'])) { ?>
<option value="1">--Project Status--</option>
<?php } ?>
<option value="1" <?php echo $row['status'] == '1' ? 'selected' : ''; ?> >Open</option>
<option value="2" <?php echo $row['status'] == '2' ? 'selected' : ''; ?> >Closed</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label class="control-label">Project Details :-</label>
</div>
<div class="col-md-6">
<textarea name="details" id="details" rows="4" class="form-control"
disabled><?php echo stripslashes($row['details']); ?></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label class="control-label">Project Requirements :-</label>
</div>
<div class="col-md-6">
<table id="t01">
<thead>
<tr>
<th>#</th>
<th>Requirements</th>
<th>Required</th>
<th>Sent</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
while ($row1 = mysqli_fetch_array($requirement_result)) {
$id = $row1['id'];
$unit_name = $row1['unit_name'];
echo '<tr>
<td>' . $no . '</td>
<td>' . $row1['name'] . '</td>
<td>' . $row1['unit_required'] . " " . $unit_name . '</td>
<td><input type="number" id = "received' . $id . '" name = "received' . $id . '" value ="' . $row1['unit_received'] . '"/></td>
<td><button type="button" name="submit" class="btn btn-primary submit-click" data-id="' . $id . '" style="padding:5px 10px;">Submit</button></td>
</tr>';
$no++;
} ?>
</tbody>
</table>
</div>
</div>
</div>
</form>
<form action="" id="addeditcategory_temp" name="addeditcategory_temp" method="post" class="form form-horizontal" enctype="multipart/form-data" style="display:none;>
<input type="hidden" name="project_id" value="<?php echo $_GET['project_id']; ?>"/>
<input type="hidden" name="reqId" id= "reqId" value="" />
<input type="number" id = "received" name = "received" value ="">
<button id="submitButton" type="submit" name="submit" class="btn btn-primary" style="padding:5px 10px;">
</form>
<script>
$(document).ready(function(e){
$('.submit-click').off('click').on('click', function(e){
var reqId = $(this).data('id');
var received = $('#received'+reqId).val();
var form = $('#addeditcategory_temp');
form.find('#reqId').val(reqId);
form.find('#received').val(received);
form.find('#submitButton').trigger('click');
})
});
</script>

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>

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