how to fetch data after clicking on dropdown box - php

hi i m new in codeigniter plz help me. In my db there is one table name car details.i am fetching city field in drop down box so its work well.now after clicking on specific city i have to fetch all the data so how its possible.plz help me
model file
function getCity()
{
$this->load->database();
$query = $this->db->query('select DISTINCT mco_registercity from cardetailso');
if($query->num_rows() > 0)
{
return $query->result();
}
return array();
contoller file
$data['cities'] = $this->car_model->get_city();
view file
<?php foreach($city as $each){ ?>
<option value="<?php echo $each->mco_registercity; ?>">
<?php echo $each->mco_registercity; ?></option>';
<?php } ?>

//in view file
<select name="something" id="something" onchange="getCityDetails(this.value);">
<?php foreach($city as $each){ ?>
<option value="<?php echo $each->mco_registercity; ?>">
<?php echo $each->mco_registercity; ?></option>';
<?php } ?>
</select>
//in JAVASCRIPT SECTION
function getCityDetails(city)
{
//make ajax call with the city value
}
Hope it will help you. Please let me know if you need any further help.

Related

Outputting data from database to HTML options and tables

I'm new in PHP.
I have a database with 5 columns. "id" "fldName" "fldPrice" "fldSupplier" and "fldPackage".
I have a HTML table with select options where I output "fldName" where "fldPackage" has a spesific value. I have made this work, but I would also like to output the corresponding "fldPrice" from the selected "fldName" when it's chosen. I'm stuck on how to do this.
This is my php:
<?php
function conTroller(){
include("includes/dbh.php");?>
<?php $sql = "SELECT * FROM tbl_price WHERE fldPackage='controller'";
$result = mysqli_query($conn, $sql);
if($result->num_rows> 0){
$options= mysqli_fetch_all($result, MYSQLI_ASSOC);}
foreach ($options as $option) { ?>
<option><?php echo $option['fldName']; ?> </option><?php }}
?>
and this is my HTML
<td><b>Electrical package</b></td>
<td>
<select id="1" class="custom-select" required onchange="ePkg(event)">
<option selected value="">None</option>
<?php conTroller(); ?>
</select>
</td>
<td>I want "fldPrice" to update here when I choose from the options</td>
<td>istL</td>
<td>Select controller</td>
Can anyone help?
I have been reading, googling and looking for similar problems. The biggest problem for me is that I'm not really sure what to search for.
For the simplicity for only updating price when selectbox changed, I suggest to use DOM Manipulation using Javascript
<?php
function conTroller() {
include("includes/dbh.php");
$sql = "SELECT * FROM tbl_price WHERE fldPackage='controller'";
$result = mysqli_query($conn, $sql);
if($result->num_rows> 0){
$options= mysqli_fetch_all($result, MYSQLI_ASSOC);}
foreach ($options as $option) { ?>
<!-- add attribute "data-price" so javascript can get price value from the selected option -->
<option data-price="<?php echo $option['fldPrice']; ?>"><?php echo $option['fldName']; ?> </option><?php
}
}?>
<td><b>Electrical package</b></td>
<td>
<!-- add a function in onchange to update price whenever selectbox changed -->
<select id="1" class="custom-select" required onchange="ePkg(event); updatePrice(this)">
<option selected value="">None</option>
<?php conTroller(); ?>
</select>
</td>
<!-- add attibute "id" so we can access the element to be updated -->
<td id="price-field">I want "fldPrice" to update here when I choose from the options</td>
<td>istL</td>
<td>Select controller</td>
<script>
// function to update price from selected option
function updatePrice(selectbox) {
var price = selectbox.selectedOptions[0].attributes['data-price'].value || '- no price set -';
document.getElementById('price-field').innerHTML = price;
}
</script>
For more advanced flow (eg: getting more product info other than price like description, etc), you will need a separate php file for displaying product info and fetch it using AJAX call. See some tutorial about AJAX and PHP
Steps I did:
PDO is safer.
The first function returns data.
The other is making a presentation.
An additional div with id='fldPrice' will be the place to display the price.
A significant part of the code was done by javascript
function getPackage() {
$db = conn();
$sql = "SELECT id, fldName, fldPrice, fldSupplier, fldPackage FROM tbl_price WHERE fldPackage='controller'";
$sth = $db->prepare($sql);
$sth->execute();
return $sth->fetchAll(PDO::FETCH_ASSOC);
}
function conTroller() {
$options = getPackage();
foreach ($options as $option) {
echo "<option>" . $option['fldName'] . "</option>";
}
}
function ePkg(ev) {
let prices = JSON.parse('<?php echo json_encode(getPackage()); ?>');
let select = document.getElementById('1');
let fldName = select.options[select.selectedIndex].value;
for (let i = 0; i < prices.length; i++) {
if (prices[i]['fldName'] === fldName) {
let d = document.getElementById('fldPrice');
d.innerHTML = 'Prices: ' + prices[i]['fldPrice'];
}
}
}
<td><b>Electrical package</b></td>
<td>
<select id="1" class="custom-select" required onchange="ePkg(event)">
<option selected value="">None</option>
<?php conTroller(); ?>
</select>
</td>
<td>I want "fldPrice" to update here when I choose from the options</td>
<td>List</td>
<td>Select controller</td>
<div id="fldPrice"></div>

submit two values from two different dropdown list in php

I have a function having two dropdown list and one submit button, the user must choose a value from dropdown list A and a value from dropdown list B so he can submit them.
I knew that I need to have an attribute in the value section as follows
<select name="students" id="students">
<?php
while($data = mysqli_fetch_array($selectstudent1)) {
$name=$data['name'];
$idname =$data['user_id_student'];
?>
<option name="studentdd" value="<?php echo $idname; ?>"><?php
echo $name; ?></option>
<?php } ?>
</select>
and the second dropdown list here
<select name="groups" id="groups">
<?php
while($data1 = mysqli_fetch_array($selectgroup1)) {
$groupschoose = $data1['group_number']; ?>
<option name="nogroup" value="<?php echo $groupschoose; ?>">
<?php echo $groupschoose; ?></option>
<?php }//while ?>
</select>
so, i need to get the "php echo $idname" (student id) from A dropdown list
and "php echo $groupschoose; " (group id) form drop downlist B
so I tried this PHP code as follows:
<?php
if(isset($_POST['addmember'])){
//addmember is id for dropdownlists form
$groupnum= $_POST['nogroup'];
$stuid=$_POST['studentdd'];
echo $groupnum;
echo $stuid ;
$sq="SELECT user_id_student FROM student WHERE
user_id_student=$stuid ";
$sq1 = mysqli_query($con,$sq);
while ($sq2 = mysqli_fetch_array($sq1)) {
$tes = $sq2['user_id_student'];
if( $tes == $stuid){
$sqlmem="UPDATE student
set group_id= $groupnum
WHERE user_id_student=$stuid";
$resultmem = mysqli_query($con,$sqlmem);}
}
}
?>
But it won't work, and I'm not sure where is the missing section
I hope my Q is clear and I'm sorry if it's little bit long, but i'm looking for your help, thank you!!

ERROR Message: Trying to get property of non-object in CODEIGNITER

I'm trying to get data from a certain table where the member id from a dropdown lis value is equal to the id from the other table. And i'm using jquery ajax also.
Here's my controller transactions.php
function getdataload(){
$this->db->where('member',$this->input->post('member'));
echo $this->db->get('loading_service')->row()->member;
}
And here's is my view.php
<label for="member">Member</label>
<select class="form-control" id="member" name="member" required onchange="showCustomer(this.value)">
<option selected="" value="">--select--</option>
<?php foreach ($members as $row): ?>
<option value="<?php echo $row->mem_id; ?>"><?php echo ucwords($row->mem_fname.' '.$row->mem_lname) ?></option>
<?php endforeach ?>
</select>
<p class="help-block text-danger" style="margin-left: 600px; margin-top: -450px; color: red;"><span id="select_member"> </span></p>
<script>
$('#member').on('change',function(){
$.post('<?php echo base_url("transactions/getdataload")?>',
{
mem_id:$(this).val()
}).done(function(res)
{
$('#select_member').text(res);
});
});
</script>
Please help me with this guys:
The first thing to do is make sure you are getting results from your query. Try something like
$query = $this->db->get(...);
if ($query->num_rows() > 0) {
return json_encode($query->result());
} else {
return "No data found";
}
then handle the no data error gracefully in your ajax success code. I sort of guessed that you would want your query result returned as json.
Try:
model
function getData()
{
$this->db->select('field_name');
$this->db->from('loading_service');
$this->db->where('member',$this->input->post('member'));
$query=$this->db->get();
return $query->row();
}
Controller
function getdataload(){
$data['member'] = $this->model_name->getData();
$this->load->view("view_name",$data);
}

Get the value of a text in select tag

I'm having issues getting the user selected text from the select tag. I've looked at the other similar questions but none really helps.
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<select name="Select" id="vals" onchange="this.form.submit()" class="btn btn-warning">
<option value="">-- Select Country --</option>
<?php
$user = new User();
$sql5 = DB::getInstance()->get('country', array('user', '=', $user->data()->username));
if (!$sql5->count()) {
echo 'No data';
} else {
foreach ($sql5->results() as $sql5) {
?>
<option value="country"><?php echo $sql5->name;?></option>'; <?php
}}
?>
</select>
</form>
</div>
The dropdown has a list of countries assigned to the user but when I click on it to output details on the country, nothing shows.
<?php
if (isset($_POST['Select']) && !empty($_POST['Select'])) {
$userSelection = $_POST['Select'];
$sql = DB::getInstance()->get('country',array('country', '=', $userSelection));
if (!$sql->count()) {
echo 'no data';
} else {
foreach ($sql->results() as $sql) {
echo 'data'; }
The query gets the data of the country selected from the country table. When I change the $userSelection in the query to a specific country name, the data for that country shows. I think the problem might be the option value within the select tag but I'm not sure.
The value is the same on all requests. Update to
<option value="<?php echo $sql5->country ?>"><?php echo $sql5->name;?></option>
This is assuming the identifying column is country since that is your where clause in the select query.

Posting php form with variable contents

I have the below code. It's a function creating a from containing a dropdown menu and a selectionbox, and is being called from a different page).
<?php
require_once 'forbindtilDB.php';
function populerDropdownsOpretProgram()
{
global $mysqliOOP;
$stmt = $mysqliOOP->prepare("SELECT DISTINCT primaer FROM oevelser ORDER BY primaer ASC");
?>
<form action="temp.php" method="post">
<select multiple>
<option value="0">Vælg ønskede øvelse(r)</option>
<?php
$stmt->execute();
$stmt->bind_result($primaereMuskelgruppe);
while ($stmt->fetch())
{
?>
<option value = "<?php echo $primaereMuskelgruppe;?>" >
<?php echo $primaereMuskelgruppe;
?>
</option>
<?php
}
$stmt->close();
?>
</select>
<?php
$stmt = $mysqliOOP->prepare("SELECT antalreps FROM antalreps ORDER BY antalreps ASC");
?>
<select>
<option value="0">Vælg ønskede antal gentagelser</option>
<?php
$stmt->execute();
$stmt->bind_result($antalreps);
while ($stmt->fetch())
{
?>
<option value = "<?php echo $antalreps;?>" >
<?php echo $antalreps;
?>
</option>
<?php
}
$stmt->close();
?>
</select>
<input type="submit">
</form>
<?php
}
I want to post the user input on a different page (currently temp.php), but I don't know how to handle the fact that the form contents is variables fetched by a mysqli call.
So far I've tried different versions of the below on the temp.php-page
<?php
echo $_POST[$antalreps];
?>
<?php
echo $_POST[$primaereMuskelgruppe];
?>
But I'm getting errors stating that there is undefined variables (antalreps and primaeremuskelgruppe) and undefined indexes...
Also, there's the added complexity that the selection box may return more than one result.
Pretty sure echo $_POST[$antalreps]; etc. is the wrong way to go about this, but I haven't been able to figure out alternatives...
Any hints?
You should add 'name' to your select and then use it.
For example instead of:
<select>
you should have:
<select name="yourname">
and then in php to display its value use:
echo $_POST['yourname'];
and for select multiple you can use:
<select name="othername[]">
and then in PHP use it as array:
foreach ($_POST['othername'] as $item) {
echo $item;
}
of course where I put yourname and othername you should put descriptive name such as colours for color box and similar

Categories