how to get value form Ajax to php - php

I have a question: How to get value form Ajax and sent value to PHP ? Can someone help me! Value percentold ajax sent to $percent for option value.
<div class="form-group p-1 m-0">
<h6>Percent </h6>
<select class="form-control form-control-lg p-2" name="percent" id="percent" >
<?php
$percent = 'val(data.percent)'; // <<<--- HERE HOW TO GET Value FORM Ajax and sent here
echo '<option class="text-white">'.$percent.'</option>';
for ($i=100; $i>=1; $i--) {
?>
<option value="<?php echo $i;?>"><?php echo $i;?>%</option>
<?php
}
?>
</select>
</div>
//////////////////////////////AJAX///////////////////////////
$("body").on("click",".editBtn", function(e){
e.preventDefault();
edit_skill = $(this).attr('id');
//console.log(edit_skill);
$.ajax({
url: 'assets/php/process.php',
method: 'post',
data: {edit_skill: edit_skill},
success:function(response){
//console.log(response);
data = JSON.parse(response);
//console.log(data);
$("#id").val(data.id);
$("#title").val(data.skill);
$("#percentold").val(data.percent);
$("#sequenceold").val(data.sequence);
$("#percentold").val(data.percent); <------Neet post go to php
}
});
});

what is val(data.percent). As is, its just string.
if you want to add it as option from javascript to select then you need to use
<script>
$('#percent').append(new Option(data.percent, ""));
</script>
in your case :
$('#percent').append(new Option(data.percent, ""));

Related

How to create filter for options value using ajax in php and zf2

How to make filter using ajax i want to get single option value for each selection and when click on filter button it sends data to getproductlistcb controller.
please help
HTML CODE is-
<div class="filter-category">
<form>
<input type="submit" value="filter" class="btn filter-btn">
<div class="user-category-select">
<select required="" name="sort" form="filter-form" class="btn filter-form-select" >
<option value="" disabled="">Sort</option>
<option value="popularity" selected="">Most Popular</option>
<option value="highest-rated">Highest Rated</option>
<option value="newest">Newest</option>
<option id="aesc" value="price-low-to-high">Lowest Price</option>
<option id="desc" value="price-high-to-low">Highest Price</option>
</select>
<label class="filter-category-select"><i class="fa fa-angle-down" aria-hidden="true"></i></label>
</div>
</form>
</div>
AjaX-
<script type="text/javascript">
$(document).ready(function(){
$('.btn filter-form-select').on('change',function(){
var data=$('#search_form');
$.ajax({
type: "POST",
url: "<?php echo $this->url('page', array('controller' => 'page', 'action' => 'getproductlistcb')); ?>",
data:data,
success:function(data){
console.log(data);
alert(data);
$("#").html(data);
}
});
});
});
</script>
Did you try to use :option in selector? For example, in that way: $('select.filter-form-select option:selected').val();?
I think you are looking for DOM-selector like that:
$('.filter-form-select option:selected').
So, in your case, in your code the code below might be helpful:
var $formData = $('#search_form');
var $formFilters = $formData.find('select.filter-form-select');
var $firstFilter = $formFilters.first();
var $option = $firstFilter.find('option:selected');
var optionText = $option.length ? $option.text() : null;
var optionValue = $option.length ? $option.val() : null;
Be aware, that $formFilters is not just one element, it's jQuery object that represents a set of DOM elements.
An additional way, try to debug and check what you have in your this-context and event:
$('.btn filter-form-select').on('change',function(event) {
console.log('clicked: ' + event.target.nodeName);
console.log('Context: ' + this);
});
I hope, something from that will help you a bit.

Jquery Radio button value not changing

Need some help, ive searched google and have no luck.
I am trying to update my radios-button's value in the div class "size-number". That value I want use it within the ajax request below. When i inspect element, the radio-button has the updated value of the button available but when i console.log it in jquery, it gives me the same initial value, despite which i pick. Ive tried setting the radio-buttons to checked="checked". Also i believe its a matter of the default value. Ive also tried input validation within jquery. In addition, ive attempted to reference the input ("input[name='radiobten']:checked") in jquery as well. any suggestions would very appreciated!
$(".size-number").click(function(){
var modelid = $("#modelval").attr('value');
console.log(modelid);
$.ajax({
url: '"Example API"',
data: [],
dataType: 'json',
type: "GET",
success: function(res){
var newarr = (res).filter(function(indx){
return indx.model_cat_id == modelid
});
console.log(newarr)
$('modelselect').slideToggle(200).html(res);
}});
});
<?php foreach ($brands as $brand){ ?>
<div class="search-select">
<i class="fas fa-chevron-down"></i>
<a href="/<?php echo $brand->full_slug.$urlSize; ?>">
<?php echo $brand->name; ?></a>
</div>
<?php
$slugCurrent = $brand->slug;
foreach ($models as $model){
if($model->brand_id == $brand->id){
if(is_null($model->model_cat_id)){?>
<div class="size-number" style="display: none;">
<?php echo $model->name; ?><input type="radio" id="modelval" name="radiobten" value="<?php echo $model->id?>"/>
</div>
<?php
}
}
}
}?>
You cannot have multiple element with same id i.e : modelval. This will always give you value of first radio button no matter which button you choose.Instead use class to get value of radio button.So ,onclick of div you can use $(this).find("yourclassname").attr('value') to get value of that particular button only.
Demo Code :
$(".size-number").click(function() {
//div->find closest modelval->value
var modelid = $(this).find(".modelval").attr('value');
console.log(modelid);
$.ajax({
url: '"Example API"',
data: [],
dataType: 'json',
type: "GET",
success: function(res) {
var newarr = (res).filter(function(indx) {
return indx.model_cat_id == modelid
});
console.log(newarr)
$('modelselect').slideToggle(200).html(res);
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="size-number" >
Abc<input type="radio" class="modelval" name="radiobten" value="1"/>
</div>
<div class="size-number" >
Xyz<input type="radio" class="modelval" name="radiobten" value="2"/>
</div>

POST variable is null when using AJAX call

I am trying to POST the value selected in a dropdown list using AJAX call. But the POST variable remains blank when the value is selected from the dropdown.
I have used the ECHO method to check whether it is returning the POST value. But it is empty.
Here is the code for reference:
Javascript (Jquery and Ajax):
<script>
$(document).ready(function(){
$("#booking_date").change(function(){ //listen when the option change
var optionValue = $("#booking_date").val(); //get the new value
$.ajax({
url: "doctordetails.php", //php file which recive the new value and save it to the database
data: { optionValue: optionValue }, //send the new value
type: "POST" , //use POST method
success: function(data) {
console.log(optionValue);
}
});
});
});
</script>
HTML:
<div class="row">
<div class="col-6">
<div class="form-group">
<input class="form-control" type="text" id="booking_date" name="booking_date" data-lang="en" data-min-year="2017" data-max-year="2020" data-disabled-days="10/17/2017,11/18/2017">
</div>
</div>
<div class="col-6">
<div class="form-group">
<select class="form-control">
<option value="">Select slot</option>>
<?php
require_once("dblogin.php");
?>
<option value="<?php echo $_POST[" optionValue "]; ?>">
<?php echo $_POST["optionValue"]; ?>
</option>
</select>
</div>
</div>
</div>
Expected result:
$_POST should return the selected value from booking_date and the $_POST value will be used in a SQL Query.
If this is dropdown list, then id must be on select box. You set id booking_date on textbox other than select box. write as:
<select id="booking_date" class="form-control">
</select>
If you want to value from select(dropdown) so you have to set id on <select> rather than textbox <input>
<select id="booking_date" class="form-control">
<option value="">Select slot</option>>
<?php require_once("dblogin.php"); ?>
<option value="<?php echo $_POST["optionValue"]; ?>">
<?php echo $_POST["optionValue"]; ?>
</option>
</select>
On Script:
<script>
$(document).ready(function(){
$("#booking_date").change(function(){ //listen when the option change
var optionValue = $(this).val(); //get the new value
$.ajax({
url: "doctordetails.php", //php file which recive the new value and save it to the database
data: { getValue: optionValue }, //send the new value
type: "POST" , //use POST method
success: function(data) {
console.log(data);
}
});
});
});
</script>

how to post select option value into jQuery ajax with name

I want to send section option value with ajax but only selection
and checkbox
the problem is that section and checkbox post undifined
thanx advanced
php code
<?php
$paramateres = new ParametresApp();
$paramateres->getparametres();
$max_pages_nbr = $paramateres->max_pages_nbr;
$idle_deconnexion = $paramateres->idle_deconnexion;
$show_chat = $paramateres->show_chat;
?>
html code
<form id="formparametres" class="form-row row shadow bg-white border row p-5 "
method="post">
<div class="form-check col-3 col-lg-3 ">
<input name="show_chat" id="show_chat" type="checkbox"
class="form-check-input" value="<?php echo $show_chat; ?>"
<?php if ($show_chat == 1) {
echo 'checked="true" ';
}
?>
<label class="form-check-label"
for="show_chat"> Autoriser messenger </label>
</div>
<div class="col-3 col-lg-3">
<label class="form-control-label">idle deconexion</label>
<input name="idle_deconnexion" class="form-control"
value="<?php echo $idle_deconnexion; ?>" required>
</div>
<div class="input-wrapper col-4 col-lg-4">
<label class="form-control-label">max_pages_nbr</label>
<select name="max_pages_nbr" class="form-control" required>
<?php echo '<option value="'.$max_pages_nbr.'">'.$max_pages_nbr.'</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>';?>
</select>
</div>
</form>
jquery code
$("form").submit(function (event) {
event.preventDefault();
var formData =
'show_chat=' + $('input[name=show_chat]').val() +
'&idle_deconnexion=' + $('input[name=idle_deconnexion]').val() +
'&max_pages_nbr=' + $('section[name=max_pages_nbr]').val() ;
$.ajax({
url: "src/admins/inc/save_parametres.inc.php",
method: "POST",
data: formData, // serializes the form's elements.
cache: false,
success: function (data) {
alert(data);
}
});
Try this
$("form").submit(function (event) {
event.preventDefault();
var formData = {'show_chat' : $('input[name=show_chat]').val(), 'idle_deconnexion' : $('input[name=idle_deconnexion]').val(), 'max_pages_nbr' : $('section[name=max_pages_nbr]').val() };
$.ajax({
url: "src/admins/inc/save_parametres.inc.php",
method: "POST",
dataType: 'json',
data: formData, // serializes the form's elements.
cache: false,
success: function (data) {
alert(data);
}
});
Hope this will help you.
i have solveur thanx for all
jquery
$(document).ready(function () {
$(':checkbox').change(function () {
if ($(this).attr("value") == 1) {
$(this).attr("value", 0)
} else {
$(this).attr("value", 1)
}
});
$("form").submit(function (event) {
event.preventDefault();
var formData =
'show_chat=' + $('#show_chat').attr("value") +
'&idle_deconnexion=' + $('input[name=idle_deconnexion]').val() +
'&max_pages_nbr=' + $('#max_pages_nbr option:selected').text() ;
$.ajax({
url: "src/admins/inc/save_parametres.inc.php",
method: "POST",
data: formData, // serializes the form's elements.
cache: false,
success: function (data) {
alert(data);
}
});
});
});
in may page src/admins/inc/save_parametres.inc.php
<?php
if (isset($_POST)){
$show_chat= $_POST['show_chat'];
$idle_deconnexion= $_POST['idle_deconnexion'];
$max_pages_nbr= $_POST['max_pages_nbr'];
$sql = " update dms_parametres set parm_value=".$show_chat ." where parm_name='show_chat';
update dms_parametres set parm_value='" . $idle_deconnexion . "' where parm_name='idle_deconnexion';
update dms_parametres set parm_value='" . $max_pages_nbr . "' where parm_name='max_pages_nbr';
";
if (!$parametres->connect()->multi_query($sql)) {
echo $parametres->connect()->error;
} else {
echo 'sucsess';
}
}
?>

POST the value of select option to another page PHP

I need to POST the value of the selected option to use in a query on another page.
This is my form:
<form method="post" data-remote="true">
<select class="selectpicker" data-live-search="true" name="switch-area">
<?php while($test3 = sqlsrv_fetch_array($test2, SQLSRV_FETCH_ASSOC)) : ?>
<option data-tokens="<?php echo $test3['Name'] ?>" value="<?php echo $test3['Name'] ?>"><?php echo $test3['Name'] ?></option>
<?php endwhile; ?>
</select>
<button class="btn btn-info">View Area</button>
</form>
I am using ajax and this is my code:
$(document).ready(function() {
$('select[name="switch-area"]').change(function(){
var status = $(this).val();
$.ajax({
type: 'POST',
url: 'sim-area.php',
data: {changeStatus: status}
});
});
});
Then on sim-area.php this is my code to grab the data:
$selectedArea = $_POST['switch-area'];
But I keep getting an undefined index error, am I posting this in the correct way?
You're sending changeStatus but not switch-area in your data, you want
$selectedArea = $_POST['changeStatus'];

Categories