Get the selected dropdown related value into the text box - php

I want to generate the salary after user selects the designation dropdown value from the database.
my db strucuture is as follows:
employee:
emp_id dep_id
1 1
2 1
designation:
dep_id des_name salary
1 accountant 20000
This is my html code:
<div class="form-group">
<label class="control-label col-lg-2" for="initial">Designation :</label>
<div class="col-lg-3">
<select class="form-control input-sm" name="classification" id="classification" onchange="changeValue();">
<?php while($row3 = mysqli_fetch_array($result3)):; ?>
<option><?php echo $row3[2]; ?></option>
<?php endwhile; ?>
</select>
<span class="error_form" id="classification_error_message"></span>
</div>
<label class="control-label col-lg-2" for="basicSalary">Basic Salary :</label>
<div class="col-lg-3">
<input type="text" class="form-control input-sm" name="basicSalary" id="basicSalary" placeholder="0.00">
</div>
</div>

You can do this by using this code butt this code from php codeigniter. Now u can changing it according to your requirements.
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">select Categories <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select class="form-control" name="cat" id="catogires">
<!-- <option value="">Select categories</option>-->
<?php
$data=$this->Product_Model->cat_select();
foreach($data as $row)
{ ?>
<option <?php if ($product->cat_id==$row->cat_id) echo "selected='selected'"; ?> value="<?php echo $row->cat_id?>"><?php echo $row->cat_name;?></option>
<?php
}
?>
</select>
</div>
</div>

Related

CSS not working when I add PHP code in a form

Screenshot of the form without PHP:
When I add following PHP code:
<div class="col-lg-4 col-md-6 col-sm-12">
<label for="">Distributor Price</label>
<input type="text" name="DisPrice" placeholder="Distributor Price" class="form-control" required>
</div>
<div class="col-lg-4 col-md-12 col-sm-12">
<label for="">Brand Name</label>
<select name="car_id" id="car_id" class="form-control-label" >
<?php
$list = mysqli_query($conn, "SELECT * FROM `vehicle_registration` where `status`='0'");
while ($row_ah = mysqli_fetch_assoc($list)) {
?>
<option value="<?php echo $row_ah['id']; ?>"><?php echo $row_ah['car_no']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
<label for="">Description (use for ||| new Line)</label>
<textarea name="Description" class="form-control" id="" cols="30" rows="10"></textarea>
</div>
After that my form looks like this:
Some CSS Logics still works but still show me the same issue.
You should inspect the HTML page using the inspect tools.
The error might be displayed there..
It could be from $conn variable.

How to select a query response before POST Submit

I wanted that when the user will choose which brand the model choices will also show by brand. My question is how can I get the model name base on the chosen brand when It won't show because I needed the response from the first one.
<div class="col-sm">
<form class="needs-validation" method="post" novalidate>
<div class="form-row">
<div class="col-md-6 mb-10">
<label for="validationCustom03">Select Brand</label>
<select class="form-control custom-select" name="brandname" required>
<option value="">Select Brand</option>
<?php
$ret=mysqli_query($con,"select brandName from tbl_brand");
while($row=mysqli_fetch_array($ret))
{?>
<option value="<?php echo $row['brandName'];?>" ><?php echo $brandnameSelect = $row['brandName'];?></option>
<?php } ?>
</select>
<div class="invalid-feedback">Please select Item.</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-10">
<label for="validationCustom03">Model</label>
<select class="form-control custom-select" name="modelname" required>
<option value="">Select Model</option>
<?php
$ret=mysqli_query($con,"select modelName from tbl_model WHERE brandID='$brandnameSelect'");
while($row=mysqli_fetch_array($ret))
{?>
<option value="<?php echo $row['modelName'];?>" ><?php echo $row['modelName'];?></option>
<?php } ?>
</select>
<div class="invalid-feedback">Please select Item.</div>
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-10">
<label for="validationCustom03">Item Name</label>
<input type="text" class="form-control" id="validationCustom03" placeholder="Brand Name" name="brandname" required>
<div class="invalid-feedback">Please provide a valid item name.</div>
</div>
</div>
<button class="btn btn-primary" type="submit" name="submit">Submit</button>
</form>
</div>
</div> ```
you must use javascript and you have just 2 choices :
fetch all models when you are loading the form page, then handle their corresponding select boxes options by js based on the selected model.
use ajax on brands select boxes changes event, fetch related model and add them as options to models select box.
you cannot do anything more to achieve this.

Foreach loop inside for loop runs only once

I have got a foreach loop iterating over metalname array, which is nested inside a for loop, the foreach loop is coming from a seperate array, the issue is, during first iteration of for loop, the for each loop prints the values, but during next iteration of for loop, the foreach prints nothing.
whereas, for rest of the fields, there is no problem, they get printed on all iteratins.
$product = GetProduct();
$metal_name = get_metal();
$count = count($product);
<?php for($i=0;$i<$count;$i++) { ?>
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2">Choose Metal</label>
<div class="col-lg-10">
<select class="form-control input-lg" name="metal_name[]">
<option value=''>Select Metal Name</option>
<?php
foreach ($metal_name as $r) { ?>
<option value="<?php echo $r['metal_id'];?>" <?php if($r['metal_id'] == $product[$i]['metal']){echo "selected='selected'";}?>><?php echo ucfirst($r['metal_name']);?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Product ID</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="product_id[]" value="<?php echo $product[$i]['id'];?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Charges Per Gram</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="gram_price[]" value="<?php echo $product[$i]['per_gram_price'];?>">
</div>
</div>
<?php } ?>
I found one thing on your forth line:
<?php for($i=0;$i<$count;$i++) { ?>
You didn't close out of PHP in your code above when you are getting your product and such. So this will give you some error.
In terms of your loop, a while loop is better for database work.
Here, I fixed it up for you. There were a few typos:
<?php
$product = GetProduct();
$metal_name = get_metal();
$count = count($product);
or($i=0;$i<$count;$i++) { ?>
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2">Choose Metal</label>
<div class="col-lg-10">
<select class="form-control input-lg" name="metal_name[]">
<option value=''>Select Metal Name</option>
<?php
foreach ($metal_name as $r) {
?>
<option value="
<?php
echo $r['metal_id'];
if($r['metal_id'] == $product[$i]['metal']){
echo "selected='selected'";
echo ucfirst($r['metal_name']);
}
?>
</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Product ID</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="product_id[]" value="<?php echo $product[$i]['id'];?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Charges Per Gram</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="gram_price[]" value="<?php echo $product[$i]['per_gram_price']; ?>">
</div>
</div>
I fixed up your code a bit for you, try this out.

Insert Data to database not working

i have this html form
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Please Input Below Information</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form method="POST" action="model.php" class="form-horizontal">
<div class="box-body">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Model Code *</label>
<div class="col-sm-10">
<input type="text" name="ModelCode" class="form-control" id="inputEmail3" placeholder="e.g KTI">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Model Name *</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="ModelName" id="inputPassword3" placeholder="e.g 0002">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Model Units *</label>
<div class="col-sm-10">
<select class="form-control" name="ModelUnit" style="width: 100%;">
<option value="" selected disabled>Choose Model Unit</option>
<?php
$sql = mysqli_query($con, "select departmentname from department");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['departmentname']; ?>"><?php echo $row['departmentname']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Model Hour Theroy *</label>
<div class="col-sm-10">
<select class="form-control" name="ModelHourT" style="width: 100%;">
<option value="" selected disabled>Choose Model Hour</option>
<?php
$sql = mysqli_query($con, "select GroupTeory from grouptheory");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['GroupTeory']; ?>"><?php echo $row['GroupTeory']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Model Hour Practice *</label>
<div class="col-sm-10">
<select class="form-control" name="ModelHourP" style="width: 100%;">
<option value="" selected disabled>Choose Model Practice</option>
<?php
$sql = mysqli_query($con, "select GroupPractical from grouppractical");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['GroupPractical']; ?>"><?php echo $row['GroupPractical']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Model Type *</label>
<div class="col-sm-10">
<input type="text" name="ModelType" class="form-control" id="inputEmail3" placeholder="e.g KTI">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Semister *</label>
<div class="col-sm-10">
<select class="form-control" style="width: 100%;" name="SemsterID">
<option value="" selected disabled>Choose Model Type</option>
<?php
$sql = mysqli_query($con, "select Semester from semester");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['Semester']; ?>"><?php echo $row['Semester']; ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<input type="submit" name="submit" class="btn btn-info pull-right" value="Submit" />
</div>
<!-- /.box-footer -->
</form>
</div>
and this is my php insert function
include("Connection.php");
if(isset($_POST['submit']))
{
echo "<script>alert($query1)</script>";
$ModelCode = mysqli_real_escape_string($con,$_POST['ModelCode']);
$ModelName = mysqli_real_escape_string($con,$_POST['ModelName']);
$ModelUnit = mysqli_real_escape_string($con,$_POST['ModelUnit']);
$ModelHourT =mysqli_real_escape_string($con,$_POST['ModelHourT']);
$ModelHourP = mysqli_real_escape_string($con,$_POST['ModelHourP']);
$ModelType = mysqli_real_escape_string($con,$_POST['ModelType']);
$SemsterID = mysqli_real_escape_string($con,$_POST['SemsterID']);
$query1 = mysqli_query($con, "INSERT INTO 'model' (ModelCode,ModelName,ModelUnit,ModelHourTheory,ModelHourPractical,ModelType,StageID) VALUES ('$ModelCode','$ModelName','$ModelUnit','$ModelHourT','$ModelHourP','$ModelType' ,'$SemsterID')");
echo "<script>alert($query1)</script>";
}
and it doesn't work .... i didn't no what's happen !! even i have been compare fields of the table with the name of the database ... but it still doesn't work ... anyone knows what is problem ?
You don't should use single quote for table name (in this case single quote is for literal value )
Assuming that you table is named model you should use
$query1 = mysqli_query($con,
"INSERT INTO model (ModelCode, ModelName,ModelUnit,
ModelHourTheory,ModelHourPractical,ModelType,StageID)
VALUES ('$ModelCode','$ModelName','$ModelUnit',
'$ModelHourT','$ModelHourP','$ModelType','$SemsterID')");

Update Database tables with values input by user in CodeIgniter using PHP

I'm trying to update one of my user tables in the Database with values taken from the the user. But for some reason it's not updating anything.
HTML Form
<form class="form-horizontal" method = "post">
<fieldset>
<!-- Form Name -->
<legend>User Details</legend>
<div>
<?php echo $this->session->flashdata('msg'); ?>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Full Name</label>
<div class="col-md-8">
<input id="name" name="name" type="text" placeholder="something" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="dob">Date of Birth</label>
<div class="col-md-4">
<input id="dob" name="dob" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Multiple Radios -->
<div class="form-group">
<label class="col-md-4 control-label" for="gender">Gender</label>
<div class="col-md-2">
<select id="gender" name="gender" class="form-control">
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="degree">Degree</label>
<div class="col-md-8">
<input id="degree" name="degree" type="text" placeholder="degree" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="specialization">Specialization</label>
<div class="col-md-8">
<input id="specialization" name="specialization" type="text" placeholder="specialization" class="form-control input-md">
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="year">Degree Year</label>
<div class="col-md-2">
<select id="year" name="year" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="semester">Semester</label>
<div class="col-md-2">
<select id="semester" name="semester" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="filebutton">Upload Profile Picture</label>
<div class="col-md-4">
<input id="filebutton" name="filebutton" class="input-file" type="file">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<a href="<?php echo base_url("/index.php/studentDashboardController/saveUserDetails"); ?>" >
<button id="submit" name="submit" class="btn btn-primary">Save Changes</button>
</a>
</div>
</div>
</fieldset>
studentDashboardController
public function saveUserDetails()
{
$this->load->model('userModel');
if (isset($this->session->userdata['logged_in']))
{
$username = ($this->session->userdata['logged_in']['username']);
}
$user = $this-> userModel-> getUserUid($username); //Gets the uid of the current user
$this->userModel->saveUserDetails($user);
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Successfully Inserted Data</div>');
$this->load->view('studentDashboard/common',$data);
redirect(base_url('index.php/studentDashboard/editProfile',$data1));
}
userModel
public function saveUserDetails($uid)
{
$data = array(
'name' => $this->input->post('name')
);
$this->db->where("uid",$uid);
$this->db->update("sysuser",$data);
}
The sysuser has the uid and name fields.I' not sure what I'm doing wrong here. Any help in this regard will be appreciated
You dont send your post data to controller. Change $this->userModel->saveUserDetails($user); as
$this->userModel->saveUserDetails($user,$this->input->post('name'));
Now Model should be like
public function saveUserDetails($uid,$name)
{
$data = array(
'name' => $name
);
$this->db->where("uid",$uid);
$this->db->update("sysuser",$data);
}
and make sure you have set correctly form action like
<form class="form-horizontal" method="post" action="<?php echo site_url('studentDashboardController/saveUserDetails');?>">

Categories