how to multickeck in ajax call - php

multi checkbox not working in ajax calling it will reset,my checkbox like
<form action="#">
<?php
foreach($rimdiameter as $rows) {
if($rows->rimdiameter!='') {
?>
<p>
<label>
<input type="checkbox" name="diametersearchs[]" value="<?php echo $rows->rimdiameter; ?>" class="ads_Checkbox_diameter" id="diametersearch" multiple>
<span><?php echo $rows->rimdiameter; ?></span>
</label>
</p>
<?php } }?>
</form>
click a check box it will run a query and view the result and click next checkbox first will be unchecked ,my ajax code
$('.ads_Checkbox_diameter').change(function(){
$('input[type="checkbox"]').not(this).prop("checked", false);
var final = '';
$('.ads_Checkbox_diameter:checked').each(function(){
var final = $(this).val();
var type = 'diameter';
$("#loadMore").hide();
var siteurl = '<?php echo $root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/'; ?>';
$(".loadingimageproduct").show();
var serachfilterproductbyprice = 'serachfilterproductbyprice';
var centre_bore='<?php echo $centre_bore;?>';
var bolt_pattern='<?php echo $bolt_pattern;?>';
var widtz=sessionStorage.getItem('widthz');
sessionStorage.setItem('diameter',final);
$.ajax({
url:"{{url('/')}}/serachfilterproductbyprice",
method: 'POST',
data: {serachfilterproductbyprice:serachfilterproductbyprice ,width:widtz, siteurl:siteurl, type:type, final:final ,bore:centre_bore ,pattern:bolt_pattern , "_token": "{{ csrf_token() }}"},
success: function(data) {
$('#msgproductfilter').html(data);
$(".loadingimageproduct").hide();
$(".front_product_wheel_show").hide();
$.ajax({
// save another data
});
}
});
});
});
why the checkbox unchecked if another one is clicking time ,how to solve?i wand multi checking property . and it will sore in array

The very first thing you do in your change handler is explicitly un-check all other checkboxes:
$('input[type="checkbox"]').not(this).prop("checked", false);
If you don't want to do that, simply remove that line of code.

Related

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>

Trying to hide specific div in jquery in php while loop

I have this form being outputted from a PHP while loop :
echo '<div id="postCont" class="postCont'.$pid.'" style="block;">
<div id="clLink">
<a id="clLink" href="'.$plink.'" target="_blank"" title="'.$ptitle.'">'.$ptitle.'</a>
</div>
<div id="clDate">Posted on '.$pdate.'</div>
<div id="clDesc">'.$pdesc.'</div>
<form method="post" class="ibadForm">
<input type="hidden" name="id" value="'.$pid.'">
<input type="hidden" name="hiddenBad" value="yes">
<input type="image" src="../img/bad.png" name="subBad" value="Bad" class="bad">
</form>
</div>';
I am trying to remove the individual .postCont when the ibadForm is clicked with jquery.
$(".ibadForm").submit(function(e) {
var url = "add_form.php";
var id = <?php echo $pid?>;
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function(data)
{
$('.postCont' + id).hide();
}
});
e.preventDefault();
});
It submits the form to add_form.php fine but doesn't hide the postCont. If I remove the id from the class then it hides all post Cont's. Can anyone tell me where I am going wrong?
You could use closest() to get the parent div then hide it using hide() method like :
$(".ibadForm").submit(function(e) {
var url = "add_form.php";
var id = <?php echo $pid?>;
var _this = $(this);
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function(data)
{
_this.closest('.postCont').hide();
}
});
e.preventDefault();
});
NOTE : You should store the $(this) object that refer to the clicked form in some variable (_this in my example) then use it inside the success callback since $(this) inside callback doesn't refer no more to the form, e.g :
_this.closest('.postCont').hide();
Hope this helps.

Get key from input id jQuery

I have list with several inputs
<input type="hidden" id="elevens_id_ea" value="<?php echo $_GET['elev_id']; ?>" />
<input type="hidden" id="arskursen_ea" value="<?php echo $arskursen; ?>" />
<?php
if ($extraanpassning_hamta['atgard'] == true){
?>
<input name="extraanpassning" id="knapp[<?php echo $amnets_id['amne_id']; ?>]" type="button" class="btn-u rounded btn-u-red btn-sm" value="Ja">
<?php } else { ?>
<input name="extraanpassning" id="knapp[<?php echo $amnets_id['amne_id']; ?>]" type="button" class="btn-u rounded btn-u-green btn-sm" value="Nej">
<?php } ?>
The main problem is how to "catch" the value in the two latest inputs with:
id="knapp[<?php echo $amnets_id['amne_id']; ?>]"
If knapp[4] -> how do I get the 4?
The code above is a part of a button that changes value when the user presses it (without refreshing the page).
The JS
<script>
$(document).ready(function(){
$('input[type="button"]').click(function(){
var extraanpassningVal = $(this).attr("value");
var amne_id_ea = $(this).attr("id");
var elevens_id_ea = $("#elevens_id_ea").val(); //värdet av elev_id
var arskursen_ea = $("#arskursen_ea").val(); //värdet av elev_id
$.ajax({
type: "POST",
url: "iup_extraanpassning_byta.php",
data: {extraanpassningType: extraanpassningVal, amne_id_ea: amne_id_ea, elevens_id_ea: elevens_id_ea, arskursen_ea: arskursen_ea},
success: function() {
location.reload();
}
})
});
});
</script>
EDIT:
The main problem is how to get the key from a input with an id like knapp[4].
How do get the key within knapp[]?
Update (thanks to user:SpYk3HH)
<script>
$(document).ready(function(){
$('input[type="button"]').click(function(){
var key = this.id.replace(/knapp|\[|\]/g, ''), // <---They key
extraanpassningVal = $(this).attr("value"),
amne_id_ea = $(this).attr("id"),
elevens_id_ea = $("#elevens_id_ea").val(),
arskursen_ea = $("#arskursen_ea").val();
if (this.ajax) this.ajax.abort(); // helps prevent multiple ajaxing (multiclicking)
this.ajax = $.ajax({
type: "POST",
url: "iup_extraanpassning_byta.php",
data: {extraanpassningType: extraanpassningVal, amne_id_ea: amne_id_ea, elevens_id_ea: elevens_id_ea, arskursen_ea: arskursen_ea},
success: function() {
location.reload();
}
})
})
});
</script>
I think I get it? You want the key when calling the button in JS? So like say: key = this.id.replace(/knapp|\[|\]/g, '')
Update, I didn't see the brackets before
$('input[type="button"]').click(function(){
var key = this.id.replace(/knapp|\[|\]/g, ''), // <---They key
extraanpassningVal = $(this).attr("value"),
amne_id_ea = $(this).attr("id"),
elevens_id_ea = $("#elevens_id_ea").val(),
arskursen_ea = $("#arskursen_ea").val();
if (this.ajx) this.ajx.abort(); // helps prevent multiple ajaxing (multiclicking)
this.ajx = $.ajax({/* options */});
})
Does that help?
FYI, $(this).attr("id") and this.id are the same thing
$('[name=test]').each(function(i) { $('#bob').text(this.id.replace(/knapp|\[|\]/g, '')) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input id="knapp4" name="test" value='my id is "knapp[4]"' />
<hr />
key: <span id="bob"></span>
Try this.
var amne_id_ea = "knapp[4]",
value = amne_id_ea.substring(amne_id_ea.lastIndexOf("[")+1,amne_id_ea.lastIndexOf("]"));
alert(value);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
Hope you are expecting this.

JQuery Function not response inside of php while loop

here i want to passing the product_id value into addcart.php file through the jQuery Function.here 'Add Cart' button generated dynamically by php while loop. every button set as a invidual product id, there is fine until this..
what's my problem is All Button pass the same value to addcart.php file when i clicked each one...i want to pass the value of individual one(button)..
Please Give me a Solution... addcart.php just contain code like
"echo $_POST['pid']; only"
<?php
include('includes/db-config.php');
$shop_id=$_GET['sid'];
$get_product_q = mysql_query("select product from shops where sid = $shop_id");
$get_product_f = mysql_fetch_array($get_product_q);
$product_id = explode(',', $get_product_f['product']);
$count_product_id = count($product_id);
for($i=0;$i<$count_product_id;$i++){
$get_product_q = mysql_query("select * from products where pid = $i");
$get_product_f = mysql_fetch_array($get_product_q);
$product_id = $get_product_f['pid'];
?>
<script>
$(document).ready(function(){
$('.but').click(function(){
$.ajax({
type: 'POST',
url:"addcart.php",
data : { pid : '<?php echo $product_id?>'},
success:function(result){
$("#div1").html(result);
}});
});
});
</script>
<?php
$product_name = $get_product_f['name'];
$product_description = $get_product_f['description'];
$product_price = $get_product_f['price'];
echo $product_name;
echo '<br>';
echo $product_description;
echo '<br>';
echo $product_price;
echo '<br>';
echo '<input class="but" type="button" value="Add Cart" />';
echo '<br><br>';
}
?>
<div id="div1"></div>
You are repeating the entire Javascript for every product. Move the Javascript out of the loop and set the product ID as a data-productid attribute on the button:
echo '<input class="but" type="button" value="Add Cart" data-productid="' . htmlspecialchars($product_id) . '" />';
Then when you make the ajax call (on click), read the product ID and set it as the POST data:
$.ajax({
type: 'POST',
url:"addcart.php",
data : { pid : $(this).data('productid') },
success:function(result){
$("#div1").html(result);
}
});
Try this :
replace : echo '<input class="but" type="button" value="Add Cart" />'; by this : echo '<input class="but" type="button" rel="'.htmlspecialchars($product_id).'" value="Add Cart" />';
and your script :
<script>
$(document).ready(function(){
$('.but').click(function(){
$.ajax({
type: 'POST',
url:"addcart.php",
data : { pid : $(this).attr('rel'),
success:function(result){
$("#div1").html(result);
}});
});
});
</script>
The problem is that you're binding it to a click on all elements with the class ".but", if you on the other hand gave the button an id like
echo '<input data-pid="'. sha1($product_id) . '" class="but" type="button" value="Add Cart" />';
and then in your jquery function would do the following (outside the loop):
<script>
$(document).ready(function(){
$('.but').click(function(){
$.ajax({
type: 'POST',
url:"addcart.php",
data : $(this).data(),
success:function(result){
$("#div1").html(result);
}
});
});
});
</script>
Then you would get around the problem, as it then listens for the click event on each element, but the data is varying on each element.
I hope this helps.
in you loop ,.but was bind an function each time,so the last bind one works .try to give the .but elements an unique id such as "id=$product_id";
and you can bind the function once (outside the loop).this simply like:
$(document).ready(function(){
$('.but').click(function(){
var data = $(this).attr("id");
$.ajax({
type: 'POST',
url:"addcart.php",
data : { pid : data},
success:function(result){
$("#div1").html(result);
}});
});
});
be lucky.

input type=text $POST value is null

i'm trying to send the value of the search textbox via jquery on keydown to the php page so that it can display the results containing the search keyword. however, the php page is not reading $_POST of the input textbox [not button].
html:
.keydown(function() {
//create post data
var postData = {
"bsearch" : $(this).val()
};
//make the call
$.ajax({
type: "POST",
url: "quotes_in.php",
data: postData, //send it along with your call
success: function(response){
$("#bquote").html(response);
}
});
})
. . .
<!-- Begin Search -->
<div id="search" style="display:none;">
<input type="text" class="bsearch" name="search" value="Search" />
</div>
<!-- End Search -->
php:
include_once "inc/class.quotes.inc.php";
$quotes = new Quotes($db);
if (isset($_POST['search']))
//$quotes->searchQuotes();
echo 'Searching. . ';
else
$quotes->displayQuotes();
it seems it is not reading the $_POST['search'] because it does not echo the text 'Searching. .' but instead goes to the else part and displays the quotes.
i tried this code to find out what is happening:
echo "<pre>";
print_r($_POST);
echo "</pre>";
this displays an empty array. what am i doing wrong?
either change postData object key to "search" or check for $_POST['bsearch']
var postData = {
"search" : $(this).val()
};

Categories