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";
?>
Related
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>
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;
};?>
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);
I am using editable select to show drop down list.Depend on search if values is not present in table which initially generates dropdown then I check if values are present in other table and if yes then I want to reload drop down with new values.
Select in view is
<select tabindex="2" id="port_of_loading" name="port_of_loading" onkeyup="port_search()">
<option value=""></option>
<?php if(!empty($port_list))
{
foreach($port_list as $d)
{
?>
<option value="<?php echo $d->port_id; ?>"><?php echo $d->port_name; ?></option>
<?php } } ?>
</select>
and ajax is
var search_val = $("#port_of_loading").val();
$.ajax({
type:'POST',
cache:false,
async:false,
data: { 'search_val' : search_val },
url: 'URL',
success: function(data)
{
$("#port_of_loading").html(data);
}
});
I am refreshing it from controller.
consloe.log(data) output is
<select tabindex="2" id="port_of_loading" name="port_of_loading" onkeyup="port_search()">
<option value=""></option>
<option value="26">value1</option>
<option value="9">value2</option>
</select>
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.