Actually, I have a field name (industry) the problem is that I want to get two value from the database change of industry by jquery but how do I do this can't understand. Please help-
Here is my code-
<label>Select Type of Industry</label>
<select class="form-control" name="industry" id="industry" onchange="industry_id(this.value);" required="">
<option value="">------------------Select Your Industry----------------</option>
<?php
foreach ($industry as $key => $value) { ?>
<option value="<?= $value['id']; ?> "> <?= $value['industory']; ?></option>
<?php } ?>
</select>
</div>
<script>
function industry_id(value) {
$.get("<?php echo base_url()?>dive/getebitvalue/"+value,function (data) {
$("#ebit_name").html(data);
// alert(data);
});
}
</script>
one value I get easily but how to get second from the same id?
Here is my model---
function getebit()
{
$industry=$this->uri->segment(3);
$sql = $this->db->query('SELECT * FROM industry WHERE id='.$industry);
$result = $sql->result();
$html='';
foreach ($result as $data) {
$html.= "<option value='" . $data->ebit . "' >" . $data->ebit . "</option>";
}
echo $html;
}
}
and here is my controller--
function getebitvalue(){
$data['ebit']= $this->site_model->getebit();
}
Method getebitvalue in controller should be like this :
function getebitvalue()
{
$industry_id = $this->uri->segment(3);
$result = $this->site_model->getebit($industry_id);
$html='';
if ( !empty($result)){
foreach ($result as $item)
{
$edita[$key] = $item->edita;
$html.= "<option value='" . $item->ebit . "' >" . $item->ebit . "</option>";
}
$data['edita'] = $edita;
$data['html'] = $html;
print_r($data);
exit;
}
}
Here is your model---
function getebit($industry_id)
{
$sql = $this->db->query('SELECT * FROM industry WHERE id='.$industry);
$result = $sql->result();
return $result;
}
You can also do this-
view page-
<div class="form-group">
<label>Select Type of Industry</label>
<select class="form-control industryData" name="industry" id="industry" required="">
<option value="">------------------Select Your Industry----------------</option>
<?php
foreach ($industry as $value) { ?>
<option data1="<?php echo $value['ebitda']; ?>" data2="<?php echo $value['ebit']; ?>" value="<?= $value['id']; ?>"> <?= $value['industory']; ?></option>
<?php } ?>
</select>
</div>
model page-
function select($tbl,$con=''){
$this->db->select("*");
$this->db->from($tbl);
if(!empty($con))
$this->db->where($con);
$query = $this->db->get();
return $query->result();
}
controller page-
public function dive(){
$this->load->view('common/header');
$data['industry'] = $this->site_model->select('industry');
$this->load->view('dive/dive',$data);
$this->load->view('common/footer');
}
Now use jquery on your view page--
<script>
$(document).on("change", ".industryData", function () {
var value = $(this).val();
var data1 = $('.industryData option:selected ').attr('data1');
var data2 = $('.industryData option:selected ').attr('data2');
alert(data1);
});
</script>
Related
Hi I need help with my filtering system. when a user selects a filter it loads the information but doesn't refresh the page with the results. the pictures below are what im looking to do.
Heres some images of what the page will look like
Heres my code
JS
code
(function($) {
"use strict";
var ProposalServerParams = {
"status_filter": "[name='change_status[]']",
"trade_filter": "[name='reade_filter[]']",
"City_filter": "[name='city_filter']",
"post_code_filter_: "[name='post_code_filter[]']",
};
var table_rec_candidate = $('.table-table_rec_candidate');
initDataTable('.table-table_rec_candidate', admin_url+'recruitment/table_candidates', [0], [0], ProposalServerParams, [0, 'desc']);
//hide first column
var hidden_columns = [0];
$('.table-table_rec_candidate').DataTable().columns(hidden_columns).visible(false, false);
$.each(ProposalServerParams, function(i, obj) {**strong text**
$('select' + obj).on('change', function() {
table_rec_candidate.DataTable().ajax.reload();
});
});
code
Controller
code
public function candidate_profile() {
if ($this->input->get('kanban')) {
$this->switch_kanban(0, true);
}
$data['switch_kanban'] = false;
$data['bodyclass'] = 'tasks-page';
if ($this->session->userdata('candidate_profile_kanban_view') == 'true') {
$data['switch_kanban'] = true;
$data['bodyclass'] = 'tasks-page kan-ban-body';
}
$data['rec_campaigns'] = $this->recruitment_model->get_rec_campaign();
$data['candidates'] = $this->recruitment_model->get_candidates();
$data['skills'] = $this->recruitment_model->get_skill();
$data['job_titles'] = $this->recruitment_model->get_job_position();
$data['company_list'] = $this->recruitment_model->get_company();
$data['title'] = _l('candidate_profile');
$this->load->view('candidate_profile/candidate_profile', $data);
}
code
View code
code
<div class="row">
<div class="col-lg-2 ">
<div class="form-group">
<select name="trade_filter" id="trade_filter" class="selectpicker" data-live-search="true" data-width="100%" data-none-selected-text="<?php echo _l('trade'); ?>">
<option value="" ><?php echo _l('trade'); ?></option>
<?php foreach($trade as $trade_key => $trade){ ?>
<option value="<?php echo html_entity_decode($trade['id']); ?>" > <?php echo html_entity_decode($trade['trade']); ?></option>
<?php }?>
</select>
</div>
</div>
View for data tables
$trade_filter = $this->ci->input->post('trade_filter');
if(isset($trade_filter)&&($trade_filter!='')){
$trade_where = '';
foreach ($trade_filter as $tarde_id) {
if ($trade_id != '') {
if ($trade_where == '') {
$trade_where .= 'AND (find_in_set(' . $trade_id . ', ' . db_prefix() . 'rec_candidate.trade) ';
} else {
$rade_where .= ' OR find_in_set(' . $trade_id . ', ' . db_prefix() . 'rec_candidate.trade) ';
}
}
}
if ($trade_where != '') {
$trade_where .= ')';
$where[] = $trade_where;
}
}
How can I make my queries short?
I have the same query multiple times on my page on different sections.
Here's the code to see what I mean:
//sql for dropdown contact type
$sql_getContactType1 = $db->query('SELECT * FROM tb_phone_contact_type');
$sql_getContactType2 = $db->query('SELECT * FROM tb_phone_contact_type');
$sql_getContactType3 = $db->query('SELECT * FROM tb_phone_contact_type');
Output in dropdown:
<select name="contact_type1" class="form-control">
<?php
while($row = $sql_getContactType1->fetch_assoc())
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
while($row = $sql_getContactType2->fetch_assoc())
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
<select name="contact_type3" class="form-control">
<?php
while($row = $sql_getContactType3->fetch_assoc())
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
How can I achieve it in just one query statement on my output?
Do the query once, put the results in an array, and then use that array in each <select>.
<?php
$sql_getContactType = $db->query('SELECT id, name FROM tb_phone_contact_type');
$contact_types = array();
while ($row = $sql_getContactType->fetch_assoc()) {
$contact_types[] = $row;
}
?>
<select name="contact_type1" class="form-control">
<?php
foreach($contact_types as $row)
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
foreach($contact_types as $row)
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
<select name="contact_type3" class="form-control">
<?php
foreach($contact_types as $row)
{
echo '<option value="'.$row[id].'">'.$row[d_name] . '</option>';
}
?>
</select>
Why using 3 select? u can use one they do same job.
For selects you must use foreach.
First, get all data in a single query. Then you can add if inside your loop to determine the contact type to show. This example code may help you:
$contacts = $db->query('SELECT * FROM tb_phone_contact_type')->fetch_assoc();
<select name="contact_type1" class="form-control">
<?php
foreach ($contacts as $contact) {
// fit this code by the `type` you want to show
if ($contact['type'] === 'CONTACT_TYPE_1') {
echo '<option value="'.$contact['id'].'">'.$contact['d_name'].'</option>';
}
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
foreach ($contacts as $contact) {
// fit this code by the `type` you want to show
if ($contact['type'] === 'CONTACT_TYPE_2') {
echo '<option value="'.$contact['id'].'">'.$contact['d_name'].'</option>';
}
}
?>
<select name="contact_type3" class="form-control">
<?php
foreach ($contacts as $contact) {
// fit this code by the `type` you want to show
if ($contact['type'] === 'CONTACT_TYPE_3') {
echo '<option value="'.$contact['id'].'">'.$contact['d_name'].'</option>';
}
}
?>
</select>
Execute query and store data into array... You can use array multiple times..
$sql_getContactType1 = $db->query('SELECT * FROM tb_phone_contact_type');
while($row = $sql_getContactType1->fetch_assoc())
{
$row_info[] = $row;
}
foreach ($row_info as $info) {
echo "Id: {$info[id]}<br />"
. "Name: {$info[name]}<br />"
. "Code: {$info[code]}<br /><br />";
}
SQL for dropdown contact type
<?php
$sql_getContactType1 = $db->query('SELECT * FROM tb_phone_contact_type');
while($row = $sql_getContactType1->fetch_assoc())
{
$row_data[] = $row;
}
?>
Output in dropdown :
<select name="contact_type1" class="form-control">
<?php
foreach($row_data as $row_val)
{
echo '<option value="'.$row_val[id].'">'.$row_val[d_name] . '</option>';
}
?>
</select>
<select name="contact_type2" class="form-control">
<?php
foreach($row_data as $row_val)
{
echo '<option value="'.$row_val[id].'">'.$row_val[d_name] . '</option>';
}
?>
<select name="contact_type3" class="form-control">
<?php
foreach($row_data as $row_val)
{
echo '<option value="'.$row_val[id].'">'.$row_val[d_name] . '</option>';
}
?>
</select>
Use above the code if you need 3 dropdown on a page.
I have two fail for my script.
1.publicAdvart.php
<script type="text/javascript">
$(document).ready(function () {
$('#select_marka').change(function () {
$.post(
'modeli.php',
{id: $('#select_marka').val()},
function (res) {
$('#model').html(res);
}
);
});
});
</script>
<select class="form-control" name="marka" id="select_marka">
<option></option>
<?php
$sel = "SELECT * FROM marka";
$query = mysqli_query($conn, $sel);
while ($row = mysqli_fetch_array($query)) {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['marka']; ?></option>
<?php
}
?>
</select>
2.modeli.php
<?php
if (isset($_POST['id']) && $_POST['id'] != '') {
$marka_id = $_POST['id'];
?>
<select id="select_marka" class="form-control">
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
$queryModel = mysqli_query($conn, $selModel);
while ($rows = mysqli_fetch_array($queryModel)) {
?>
<option value="<?php echo $rows['model']; ?>"><?php echo $rows['model']; ?></option>
<?php
}
?>
</select>
<?php
}
?>
Where is a problem? These two script half work.
This is OK click "Opel" -> "meriva". http://i.stack.imgur.com/ma5XZ.jpg
Next step 2 is a problem, i want "name" marka and "name" model.
Problem this is here.I don't get "marka - id", i want "name" http://i.stack.imgur.com/Nm1Yk.jpg
Update:
step2.php
<?php
if (isset($_POST['submit'])) {
$marka = $_POST['marka'];
$model = $_POST['model'];
$_SESSION['marka'] = $marka;
$_SESSION['model'] = $model;
}
?>
<div class="col-md-12 well advertTittle">
<h4 class="well">
<?php echo $_SESSION['marka'] . ' ' . $_SESSION['model'] . ' ' . stripslashes($_SESSION['modify']); ?>
</h4>
<span><?php echo $_SESSION['price'] . ' лв.'; ?></span>
</div>
You forget to give the "name" attribute for yours "marka" select field:
<?php
...
?>
<select id="select_marka" class="form-control"> // <- here
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
....
Should be something like this:
<?php
...
?>
<select id="select_model" class="form-control" name="model"> // <- here!!!
// "id" attribute also should be changed
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
....
Also, yours step2.php should be like this:
<?php
if (isset($_POST['submit'])) {
$_SESSION['marka'] = $_POST['marka'];
$_SESSION['model'] = $_POST['model'];
}
function validData($data) {
return
isset($data['marka'])
&& isset($data['model'])
&& isset($data['price'])
;
}
if (validData($_SESSION)) {
$marka_id = intval($_SESSION['marka']);
$model_id = intval($_SESSION['model']);
$marka = mysqli_fetch_array(mysqli_query($conn, "SELECT name FROM marka where id = $marka_id"));
$model = mysqli_fetch_array(mysqli_query($conn, "SELECT name FROM marka_model WHERE id = $model_id"));
?>
<div class="col-md-12 well advertTittle">
<h4 class="well">
<?php echo $marka['name'] . ' ' . $model['name'] . ' ' . (isset($_SESSION['modify']) ? stripslashes($_SESSION['modify']) : ''); ?>***
</h4>
<span>
<?php echo $_SESSION['price'] . ' лв.'; ?>
</span>
</div>
<?php } else { ?>
Not all fields filled.
<?php } ?>
I made a form with some dynamic/concatenated select in this way.
FORM HTML (index.php)
<form id="src_form" action="index.php" method="post">
<p>
<select name="catid_1" id="catid_1" class="fieldmax">
<?php echo $cs->show_cat_1(''); ?>
</select>
</p>
<p>
<select name="catid_2" id="catid_2" class="fieldmax">
<option value=""> </option>
</select>
</p>
<p>
<select name="catid_3" id="catid_3" class="fieldmax">
<option value=""> </option>
</select>
</p>
<p>
<input type="submit" name="submit" value="SRC" id="btn_src">
</p>
</form>
AJAX JS
$(function() {
// Ajax post Page
var urlpage = 'ajax-php/con-select.php';
// Vars
var wait = '<option value="">wait</option>';
var all = '<option value="">all</option>';
// Default
$("select#catid_2").prop("disabled", true);
$("select#catid_3").prop("disabled", true);
// CATID_1 Change
$("select#catid_1").change(function(){
// set var
var catid_1 = $("select#catid_1 option:selected").prop("value");
// laod
$("select#catid_2").html(wait);
$.post(urlpage,
{
catid_1:catid_1
},
function(data){
switch (data) {
case all:
$("select#catid_2").html("");
return false;
default:
$("select#catid_2").prop("disabled", false);
$("select#catid_2").html(data);
return false;
}
});
});
// CATID_2 Change ... etc
});
CON-SELECT.PHP
if(isset($_POST['catid_1']))
{
echo $cs->show_cat_2();
die;
}
if(isset($_POST['catid_2']));
{
echo $cs->show_cat_3();
die;
}
CLASS.PHP
public function show_cat_1()
{
$result = $this->db->mysqli->query
("SELECT * FROM cat_1 ORDER BY idcat_1 ASC");
$cat_1 = '<option value="">all</option>';
while($row = $result->fetch_assoc())
{
$cat_1 .= '<option value="' . $row['idcat_1'] . '"';
$cat_1 .= '>' . $row['idcat_1'] . '</option>';
}
return $cat_1;
}
public function show_cat_2()
{
$result = $this->db->mysqli->query
("SELECT * FROM cat_2 ORDER BY idcat_2 ASC");
$cat_2 = '<option value="">all</option>';
while($row = $result->fetch_assoc())
{
$cat_1 .= '<option value="' . $row['idcat_2'] . '"';
$cat_1 .= '>' . $row['idcat_2'] . '</option>';
}
return $cat_2;
}
etc..
my goal is to keep the choices (select value) in fields after submit the form (reload page) while maintaining their functionality. you can do it? how could I do that? thanks
I am currently working with a dynamic dropdown menu(dependable select boxes). I am pulling the values straight from MySQL DB(if your curious here is how i have the DB SETUP). I am able to get the values of each table and display them accordingly. The problem I am having is echoing the SELECTED value of each selct box. I have a created JS function that will request postfile.php which will then echo the SELECTED value of each box. I am not getting anything echoed. I have checked with firebug but nothing is being posted.
How can I make this work? or Am I approaching this wrong? or Is there a better way? EXAMPLE
Working HTML/PHP
<?php
include ('includes/dbConnect.php');
try {
$pdo = get_database_connection();
$sql = "SELECT *
FROM `categories`
WHERE `master_id` = 0";
$statement = $pdo->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo 'There was a problem';
}
?>
<select name="main" id="main" size="7" class="update">
<option value="">Select one</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="update" disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
JS
<script type="text/javascript">
$(document).ready(function () {
$('#main).change(function() {
if ($(this).val()!='
') {
$("#subc1").load("postfile.php",{main_id: $(this).val()});
//$("#subc1").removeAttr('
disabled hidden ');
}
});
//code on change of sel_source
$('#subc1 ').change(function() {
if ($(this).val()!='
') {
$("#subc2").load("postfile.php",{subc1_id: $(this).val()});
//$("#colour").removeAttr('
disabled ');
}
});
$('#subc2 ').change(function() {
if ($(this).val()!='
') {
$("#subc3").load("postfile.php",{subc2_id: $(this).val()});
//$("#colour").removeAttr('
disabled ');
}
});
});
</script>
PHP- postfile.php
if(isset($_REQUEST['main_id']) && !empty($_REQUEST['main_id'])) {
try {
include ('../includes/dbConnect.php');
$pdo = get_database_connection();
$sql = ("select * from `categories` where id='".$_REQUEST['main_id']."' ");
$result = $con->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
}catch(PDOException $e) {
echo 'There was a problem';
}
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
if(isset($_REQUEST['subc1_id']) && !empty($_REQUEST['subc1_id'])) {
$result = mysql_query("select * from table where id='".$_REQUEST['subc1_id']."' ");
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
if(isset($_REQUEST['subc2_id']) && !empty($_REQUEST['subc2_id'])) {
$result = mysql_query("select * from table where id='".$_REQUEST['subc2_id']."' ");
if($number_of_rows > 0) {
$output = '<option value="">Select</option>';
while($row = mysql_fetch_assoc($result)) {
$output .= '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
} else {
$output = '<option value="">Select</option>';
}
echo $output;
}
Maybe you would like to use jQuery UI auto-complete. It is easier to use and less code. It has a remote data source also. Try this one, maybe this can solve your problem. http://jqueryui.com/demos/autocomplete/
Add on submit action to the form. In the function use the following:
var Myvar = $('#subc3 :selected').text();