Auto submit selection upon onChange - javascript

I have a selection form which supposed to updated database on change by using jQuery, but no effect at all, can someone help with this?
<SELECT name='status' id='status'>
<option value="<?php echo $status ?>"><?php echo $status ?></option>
<option value="Inquiry">Inquiry</option>
<option value="Confirmed">Confirmed</option>
<option value="Departured">Departured</option>
<option value="Cancelled">Cancelled</option>
</select>
<input type="hidden" name="gp_name" id="gp_name" value="<?php echo $gp_name;?>" />
<div id="autosavenotify"></div>
<script>
$(document).ready(function(){
$('select').live('change',function () {
var statusVal = $(this).val();
var gp_name = $('#gp_name').val();
alert(statusVal);
$.ajax({
type: "POST",
url: "lotus_savestatus.php",
data: {statusType : statusVal, gp_name:gp_name},
success: function(msg) {
$('#autosavenotify').text(msg);
}
})
});
});
</script>
statussave.php
<?php
$gp_name=$_POST['gp_name'];
$st=$_POST['statusType'];
$qry =" UPDATE lotus_gp SET `status`=$st where gp_name='".$_POST["gp_name"]."'";
$done = mysql_query($qry);
if($done)
{
echo "Saved Successfully";
}
?>

Just need to use "on" instead of "live" method in Jquery. because it has been deprecated.
$('select').live('change',function () {

It's loud and clear !
jQuery .live()
Note: This API has been removed in jQuery 1.9; please use on() instead.
jQuery.Deferred exception: $(...).live is not a function TypeError:
$(...).live is not a function
Instead of .live() use .on()
$(document).ready(function() {
$('select').on('change', function() {
var statusVal = $(this).val();
var gp_name = $('#gp_name').val();
alert(statusVal);
$.ajax({
type: "POST",
url: "lotus_savestatus.php",
data: {
statusType: statusVal,
gp_name: gp_name
},
success: function(msg) {
$('#autosavenotify').text(msg);
}
})
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name='status' id='status'>
<option value="<?php echo $status ?>">
<?php echo $status ?>
</option>
<option value="Inquiry">Inquiry</option>
<option value="Confirmed">Confirmed</option>
<option value="Departured">Departured</option>
<option value="Cancelled">Cancelled</option>
</select>
<input type="hidden" name="gp_name" id="gp_name" value="<?php echo $gp_name;?>" />
<div id="autosavenotify"></div>

Related

Splice method in codeigniter drop down

I have some locations that are stored in my database separated by a comma and I have a dropdown that gets that information. The user selects a location to populate another drop down based on this chosen location.
Here is my php code:
<label for="select-service">
<strong>Enter a Location:</strong>
</label>
<select class="form-control" id="select-location" class="col-xs-12 col-sm-4 form-control" required>
<option value="">Select Location</option>
<?php
foreach($appointment_locations as $location) {
?>
<option value="<?php echo $location->notes ?>"><?php echo $location->notes ?></option>
<?php
}
?>
</select>
Here is my javascript code:
$(document).ready(function() {
FrontendBook.initialize(true, GlobalVariables.manageMode);
GeneralFunctions.enableLanguageSelection($('#select-language'));
$('#select-provider').html('');
$('#select-location').change(function() {
$('#select-provider').html('');
var selected_location = $(this).val();
$.ajax({
url: '<?php echo site_url('appointments/getProviderByLocation'); ?>',
type: 'POST',
data: {
csrfToken: GlobalVariables.csrfToken,
'selected_location': selected_location,
},
dataType: 'json',
success: function(data) {
var options = '';
$.each(data, function(key,val) {
console.log(val.id);
options += '<option value="'+val.id+'">'+val.first_name+" " +val.last_name +'</option>'
});
$('#select-provider').html(options);
}
});
});
and here is a screenshot of the location how it is currently:
So what i want to achieve is to have Randburg as one option, Greenside as another option and Rosebank as another option.
You have different type string in your array so for this particular problem in your loop you should explode your string like this and another loop to print separated string
<select class="form-control" id="select-location" class="col-xs-12 col-sm-4 form-control" required>
<option value="">Select Location</option>
<?php
foreach($appointment_locations as $location) {
$LocationsArray = explode(",", $location->notes);
foreach($LocationsArray as $singleLocation):
?>
<option value="<?=$singleLocation ?>"><?=$singleLocation ?></option>
<? endforeach;
};?>

how to Load Dependent Dropdown on Multi-Select in laravel

I want to load dependent dropdwon on multiselect in laravel.how can i load ?now i have sigle select to load dropdown .below is my code i want to load dependent dropdwon on multiselect in laravel.how can i load ?now i have sigle select to load dropdown .below is my code
<select class="form-control" id="region_country_id" name="region_country_id" >
<?php foreach ($countries as $key => $value) { ?>
<option value="<?php echo $value->id; ?>"<?php if($product->region_country_id == $value->id){ echo "selected";} ?>>
<?php echo $value->name;?>
</option>
<?php } ?>
</select>
<select class="form-control" id="region_id" name="region_id" style="margin-top: 10px;" >
<?php if(Session::get('branch_access') != 1)
{?>
<option value="">All region</option>
<?php } ?>
<?php foreach ($regions as $key => $value) { ?>
<option value="<?php echo $value->id; ?>" <?php if($value->id == $product->region_id) { echo "selected";} ?>><?php echo $value->region; ?>
</option>
<?php } ?>
</select>
below is script for above
$('body').on('change', '#region_country_id', function() {
loadOptionRegion($(this).val());
});
function loadOptionRegion(countryID) {
$.ajax({
method: "POST",
url: "/region/country",
dataType: 'json',
data: {
'country_id': countryID
},
beforeSend: function() {},
success: function(data) {
console.log(data.data.region);
var regionList = data.data.region;
var str = '<option value="0">All Region</option>';
$.each(regionList, function(index, value) {
str = str + "<option value='" + value.id + "'>" + value.region + "</option>";
console.log(str);
});
$("#region_id").html(str);
}
})
}
Very first add the multiple in the select box
<select class="form-control" id="region_country_id" name="region_country_id" multiple>
Now in your controller change the query from where to whereIn country ID
From:
->where('country_id',$countryId);
To:
->whereIn('country_id',$arrOfCountryID);

Get Javascript Variable value in PHP variable

var val;
$('select').on('change', function() {
alert( this.value );
val = this.value;
})
<?php
echo $variable = "<script>document.write(val)</script>";
?>
<select>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">four</option>
</select>
I want to get the value of the selected box and save it in a PHP variable. I want to save and echo val variable. Please help
use this code for use variable
<?php
session_start();
echo $_SESSION['php_value'];
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function getValue(obj){
var value = obj.value;
$.ajax({
type:"POST",
url: 'edit.php',
data: "val="+ value,
dataType: 'text',
async: false,
cache: false,
success: function ( result ) {
window.location.reload();
}
});
}
</script>
<select onchange="getValue(this)">
<option value="1" <?php if($_SESSION['php_value'] == 1) echo 'selected';?>>One</option>
<option value="2" <?php if($_SESSION['php_value'] == 2) echo 'selected';?>>Two</option>
<option value="3" <?php if($_SESSION['php_value'] == 3) echo 'selected';?>>Three</option>
<option value="4" <?php if($_SESSION['php_value'] == 4) echo 'selected';?>>four</option>
</select>
then create edit.php file
<?php
session_start();
$_SESSION['php_value'] = $_REQUEST['val'];
?>
Ajax can do this. Google it, and check out api.jquery.com and look at the ajax functions, .ajax(), .post(), .get(), .load(), etc.
As for your specific question, here is what you would do:
//Javascript file
$.post('my_ajax_receiver.php', 'val=' + $(this).val(), function(response) {
alert(response);
});
});
//PHP file my_ajax_receiver.php
<?php
$value = $_POST['val'];
echo "$value";
?>

javascript and ajax to update the second drop dowm and the function undefined error

This is my php file
<select name="scity" id="city_id" class="myinput" required onChange="getMalls()">
<option value="<?php echo $_POST['scity'] ;?>"><?php echo $_POST['scity'] ;? ></option>
<option value="">Please Select</option>
<?php
include 'include/allcity.php';
?>
</select>
<select name="mallname" required class="myinput" id="malls-list">
<option value="">Please Select</option>
</select>
<script>
function getMalls() {
document.getElementById("city_id").value;
alert(hi);
$.ajax({
type: "POST",
url: "include/allmall.php";
data:'city_id='+x,
success: function(data){
$("#malls-list").html(data);
}});
}
</script>
<?php
alert(hi);
include 'mydb.php';
if(!$con) {
echo 'Could not connect to the database.';
} else {
if(!empty($_POST['scity'])) {
$queryString = $con->real_escape_string($_POST['scity']);
if(strlen($queryString) > 0) {
$querys = $con->query("SELECT mallname FROM mallname WHERE cityname = '%$queryString%' order by mallname");
if($querys) {
while ($results = $querys -> fetch_object()) {
<option value="<?php echo ($results -> $results["id"]; ?>"><?php echo ($results -> $results["mallname"]; ?></option>
}
} else {
echo 'OOPS we had a problem :(';
}
} else {
// do nothing
}
} else {
echo 'There should be no direct access to this script!';
}
}
?>
There is another php file after the script tag where it is referring to.
First of all I am getting an error that function is not defined. Actually this is for activating second dropdown on the value of first dropdown. Please help me. Thanks in advance.

onchange function when selecting drop down menu will populate a textbox

this is my table in the database
tbl_job_title
ID | Job_title | description |
1 | Accountant | Handle Accounts |
2 | Dev. Support | Support Centre |
when selecting the job title from the table, i'm using a drop down menu and here is my code.
<select id="job_title" >
<option value=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
and I have a textbox that will populate if i select a job_title
<input type="text" id="catch_value">
How do I populate the textbox when I select example is "Accountant", the textbox value must be "Handle Accounts".
Here is the short & sweet code I have made just for you, feel free to use it!
Not required Ajax...
CODE -
<select id="job_title" >
<option value="" class="job" role=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>" class="job<?php echo $row['id']; ?>" role="<?php echo $row['description']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
Jquery -
$('#job_title').on('change',function(){
var m = this.value;
var n = $('.job'+m).attr('role');
$('#catch_value').val(n);
return false;
});
eg - fiddle
<select id="job_title" onchange="javascript:callajaxvalue(this.value);">
<option value=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
<script>
function callajaxvalue(id)
{
$.ajax({
type: "POST",
url: "some.php",
data: { id: id}
success: function(data, textStatus ){
if(data)
{ $("#catch_value").val(data);}
},
})
}
</script>
some.php
$_POST['id'];
$query = "get data from table";
execute query.
echo $row['description'];
For codeigniter
function callajaxvalue(id)
{
$.ajax({
type: "POST",
url: "my_controller/update/"+id,
data: { id: id}
success: function(data, textStatus ){
if(data)
{ $("#catch_value").val(data);}
},
})
}
my_Controller.php
public function update()
{
$id = $this->input->post('id');
$data['discription'] = call model function for get data form database
echo json_encode($data);
}
$('#job_title').change(function(){
$('#catch_value').val('Handle Accounts');
});
see fiddle:fiddle
or you can add if statments
$('#job_title').change(function(){
var jt = $('#job_title').val();
if(jt == "Something")
{
$('#catch_value').val('Handle Accounts');
}
if (jt == "SomethingElse")
{
$('#catch_value').val('somethingelse');
}
});
This is the right answer :
<select id="job_title" onchange='populate()'>
<option value=""> </option>
<?php foreach ($job_title as $row): ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?>
</option>
<?php endforeach?>
</select>
<script>
function populate(){
var jobttl= document.getElementById('job_title').value();
if(jobttl){
document.getElementById('catch_value').value;
}
}
</script>

Categories