I previously had the form set up with just radio input so the value was only one or the other. The client now wants it to be a check box so you could search by multiple variables at once.
I setup a test jQuery method to making sure it was at least making the correct string to be submitted with the follow:
function showValues() {
var datastr = $(".formC").find("input[type='checkbox']").serialize();
$( "#bodyA" ).text( datastr );
}
$(".localSearch").on('click', showValues);
Here is the result:
expertise%5B%5D=Ancillary&expertise%5B%5D=LargeGroup&expertise%5B%5D=IndividualPlans
I am very new to AJAX, jQuery, and PHP but this seems like the correct string to be submitting.
Now I am using jQuery AJAX to submit the values over to my PHP page.
$('.localSearch').on('click', function() { //Pulls data based on radial input
var dataStr = $(".formC").find("input[type='checkbox']").serialize();
$.ajax({
type: "POST",
datatype: "html",
data: {
expertise: dataStr
},
url: "expertise.php",
success: function (data) {
$("#bodyA").html(data);
}
});
});
Here is what the form looks like: (The form contains more but these are the only elements for expertise.php)
<label for="agent">Agent Services:</label><br />
<label for="ancillary"><input type="checkbox" value="Ancillary" name="expertise[]" id="ancillary" />Ancillary</label><br />
<label for="smallgroup"><input type="checkbox" value="SmallGroup" name="expertise[]" id="smallgroup" />Small Group</label><br />
<label for="largegroup"><input type="checkbox" value="LargeGroup" name="expertise[]" id="largegroup" />Large Group</label><br />
<label for="medicare"><input type="checkbox" value="Medicare" name="expertise[]" id="medicare" />Medicare</label><br />
<label for="longterm"><input type="checkbox" value="LongTermCare" name="expertise[]" id="longterm" />Long Term Care</label><br />
<label for="individual"><input type="checkbox" value="IndividualPlans" name="expertise[]" id="individual" />Individual Plan</label><br />
<label for="tpa"><input type="checkbox" value="TPASelfInsured" name="expertise[]" id="tpa" />TPA Self Insured</label><br />
<label for="ppaca"><input type="checkbox" value="CertifiedForPPACA" name="expertise[]" id="ppaca" />Certified for PPACA</label><br />
<label for="acaind"><input type="checkbox" value="ACA_Ind" name="expertise[]" id="acaind" />Individual Marketplace Certified</label><br />
<label for="acashop"><input type="checkbox" value="ACA_Shop" name="expertise[]" id="acashop" />Shop Marketplace Certified <br />(small group)</label><br />
<span class="localSearch">Submit</span>
I had it working when it was only dealing with one value but the string it creates seems to be the correct string. Any ideas on this? If you need anymore code or anything then just let me know!
Live site if needed
You're serializing everything in the checkbox element set. I suspect you just need the value of a given attribute (id or value). And only items that are checked. Here's an example of dumping selected checkboxes into an array:
$(".formC input:checkbox:checked").each(function(){ myArray.push($(this).val()); })
jQuery get values of checked checkboxes into array
Once you have an array, you can loop through it to put together your sql WHERE predicates.
Related
my Code not working Exactly as I need, 2 pages php and 1 jquery file to get the data by jquery Ajax.
First Page name catfd.php :
<input type="checkbox" value="2" class="catcf"> catfd2 <br />
<input type="checkbox" value="35" class="catcf"> catfd35 <br />
<input type="checkbox" value="22" class="catcf"> catfd22 <br />
<input type="checkbox" value="133" class="catcf"> catfd133 <br />
<input type="checkbox" value="28" class="catcf"> catfd28 <br />
<input type="checkbox" value="33" class="catcf"> catfd33 <br />
<input type="checkbox" value="55" class="catcf"> catfd55 <br />
<input type="checkbox" value="44" class="catcf"> catfd44 <br />
and the second page name getvalue.php
this page it's given me the right answer if var '$catcfid' is Exactly one I choose from input filed in catfd.php ( i think my problem with Jquery ajax
if(isset($_POST['catcf'])){
$catcfid= $_POST['catcf'];
$sql = 'SELECT * FROM tablename where cat_id = '.$catcfid.' order by p_id asc';
$catandproid = $wpdb->get_results($sql);
foreach($catandproid as $key){
echo $key->title;
}
}
this is the jquery ajax file
$(document).ready(function(){
$(".catcf").on('click', function() {
if ($('input.catcf').is(':checked')) {
var catcf = Number($(this).val());
$(".catcf").val(catcf);
$.ajax({
url: 'getvalue.php',
type: 'post',
data: {catcf:catcf},
beforeSend:function(){
$(".main-area-courses-continer").html("Looding....");
},
success: function(response){
// Setting little delay while displaying new content
setTimeout(function() {
// appending posts after last post with class="post"
$(".main-area-courses-continer:last").after(response).show().fadeIn("slow");
}, 2000);
}
});
}
});
});
the code is working but the result gives to me is repeating the first checkbox value I choose its mean jquery didn't change the value of class="catcf" because there unic value for each class. and I will use multiple choices from the checkbox.
i want use this checkbox to get different result from database .
Your problem is the following line:
$(".catcf").val(catcf);
What's happening here is that you select all of your checkboxes ($(".catcf")) and then change their value attribute to the value of the clicked checkbox. Remove this line and it should work as expected.
In this code jquery get the data back from php and i would like to get jquery explode one of the variable where multiple auto are stored and finally get the specific checkbox checked
json_reponse_from_PHP: "auto1; auto3; auto4".
$(document).on("click", "#update", function(e) {
e.preventDefault();
var datas = 'pg/post.php';
$.getJSON(datas, function(data) {
$.each(data, function( key, value ) {
$('input[name=' + key + '], select[name=' + key + '], textarea[name=' + key + ']').val(value);
});
});
});
<div>
<input type="text" name="name" value="" />
<input type="text" name="name" value="" />
</div>
See example:
<input type="checkbox" name="auto[]" checked="checked" value="auto1" />
<input type="checkbox" name="auto[]" value="auto2" />
<input type="checkbox" name="auto[]" checked="checked" value="auto3" />
<input type="checkbox" name="auto[]" checked="checked" value="auto4" />
<input type="checkbox" name="auto[]" value="auto5" />
<input type="checkbox" name="auto[]" value="auto6" />
Can you help me to find
If you want to check a checkbox you don't use val() you use prop('checked', true)
Since you have array naming on elements you can't match the specific name so you will need to use the returned value to help target the checkboxes.
Also what you are sending from server doesn't match the names anyway.
$.each(data.split(';'), function(_, val ) {
$(':input[value=' + val +']').prop('checked',true);
});
It makes no sense either not to send array from server instead of json_encoding a delimited string in your php
So i have this line of code that will repeat different times in a form.
<input type="checkbox" name="checkbox[]" /> !checked
<input type="checkbox" name="checkbox[]" /> !unchecked
<input type="checkbox" name="checkbox[]" /> !checked
<input type="checkbox" name="checkbox[]" /> !unchecked
The !checked show that the checkbox was checked and the !unchecked shows that the checkbox was not checked.
How can i create a php array to get values of checked and unchecked checkboxes in order like this :
array( 0 => checked, 1 => unchecked, 2 => checked, 3 => unchecked );
Momentarily i can get just the checked value with $_POST["checkbox"] but i cannot get the unchecked value.
First of all you need to put a value to your checkboxes:
<input type="checkbox" name="checkbox[]" value="checkboxNchecked" /> !checked
You can't really distinguish your checkboxes otherwise.
Then: Your checkboxes will either return a value if they are checked or will be ignored when they are unchecked. You will not get a NULL, FALSE or other value. It will simply not be transfered via POST/GET to your php script as if it wasn't in yout HTML code. This covers the topic: Does <input type="checkbox" /> only post data if it's checked?
If you know how many checkboxes are around and what they are called - no problemo señor - but if you don't, you'll need to find a way around. If you tell us what the nature of your checkboxes are, we can help you find a tailored solution.
you can use jquery and ajax. In your submit event get all values from the form and submit it by ajax. you can get unchecked value in jquery like this:
$("input:checkbox:not(:checked)")
or
if ($('#idOfYourCheckBox:checked').length > 0) {
//its checked
}
else {
//not checked
}
This will print only checked fields, because unchecked ones are not sent to server.
You will have to do some javascript and hidden field tricks.
Take a look here
Post the checkboxes that are unchecked
<input type="checkbox" name="checkbox[n1]" /> !checked
<input type="checkbox" name="checkbox[n2]" /> !unchecked
<input type="checkbox" name="checkbox[n3]" /> !checked
<input type="checkbox" name="checkbox[n4]" /> !sdsk
foreach($_POST['checkbox'] as $key => $value){
$checkbox[$key] = 'checked';
}
print_r($checkbox); // your new array
Solved:
Declaration of form...
<form id="form1" name="form1" method="post" action="xx.php" onSubmit="set_hidden_value()">
<input name="arrayofchecks" type="hidden" value="toset" />
...
OnSubmit:
function set_hidden_value()
{
var checkstring = "";
for (var i=0; i < $('#checkbox').length; i++)
{
if ($('#checkbox')[i].checked)
{
checkstring = checkstring + "1";
}
else
{
checkstring = checkstring + "0";
}
}
$('#arrayofchecks').val(checkstring);
And the result is a string with values checked and unchecked (1 and 0)...
In my case, i use ajax for intercept submit and do set_hidden_value here...
If you are using a server side language like PHP, there is an easier method than using hidden fields to supply default or writing javascript (both may fail if the user's device/browser doesn't support that method).
<input type="checkbox" value="choice1" name="checkbox[]" />
<input type="checkbox" value="choice2" name="checkbox[]" />
<input type="checkbox" value="choice3" name="checkbox[]" />
This method doesn't return unchecked items, but it specifically identifies which items were checked. Otherwise, if the checkboxes all have the same value, all you get is one, two or 3 values repeated with no idea which item was checked. However, assuming choice2 was checked with the above method, it's pretty easy then to figure out that item 1 and 3 therefore were not checked.
Please check below code.
$("button").click(function () {
var i=0;
var cbox=[];
$("input[name='cbox']").each(function () {
if($(this).is(':checked')) {
cbox[i++] = $(this).val();
}else{
cbox[i++] = "unchecked";
}
});
console.log(cbox);
i = 0;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" value="checked" name="cbox">
<input type="checkbox" value="checked" name="cbox">
<input type="checkbox" value="checked" name="cbox">
<input type="checkbox" value="checked" name="cbox">
<button>Click</button>
I'm trying to learn jQuery and have a question which may be pretty simple to someone familiar with it already.
Application:
Using PHP with jQuery and Bootstrap Toggle Buttons (http://www.larentis.eu/bootstrap_toggle_buttons/) to create a dynamic link address depending on which toggle buttons the user has turned "on".
Right now i'm using form to POST the toggle button states which once the page reloads it then pulls those POST variables and attaches them to the link.
What I would like to do is to have jQuery automatically change the link on the page when the user toggles the button instead of having to use form post.
What I would like to be able to do is in layman's terms explanation each time the toggle button is toggled:
$link = "http://www.mydomain.com/students.php?view="
if jQuery toggle-button-1 = on then add "name"
if jQuery toggle-button-2 = on then add "id"
if jQuery toggle-button-3 = on then add "address"
// If toggle-button-1 and toggle-button-2 were on and toggle-button-3 was off
// then $views would equal
$views = "name,id"
// On the webpage it would then display
http://www.mydomain.com/students.php?view=name,id
// If the person toggled toggle-button-1 to off, the link would display:
http://www.mydomain.com/students.php?view=id
I hope i've explained this so you can understand it...it's really simple what i'm trying to do and I know how to do this in PHP but i feel like i've been searching around online for something that should be simple and I just can't seem to wrap my head around it.
If anybody could please help me out or point me in the right direction I would greatly appreciate it!
Thanks!!
Here is my example on jsFiddle
HMTL
<input type="checkbox" name="name" value="name" id="name" /> name<br />
<input type="checkbox" name="id" value="id" id="id" /> id<br />
<input type="checkbox" name="address" value="address" id="address" /> address<br />
Link: <input type="text" name="url" value="" id="url" />
JavaScript
$(function() {
var link = "http://jsfiddle.net/mouse0270/chnyy/"
$('input[type=checkbox]').change(function () {
var viewItems = "";
$('input[type=checkbox]:checked').each(function () {
viewItems += ','+$(this).val();
});
$('#url').val(link+'?view='+viewItems.substring(1));
});
});
http://jsfiddle.net/CvsbD/
HTML
<input type="checkbox" class="items" id="one" data-view="name" />
<input type="checkbox" class="items" id="two" data-view="id" />
<input type="checkbox" class="items" id="three" data-view="address" />
<input type="text" id="output" />
JS
$("body").on('change', '.items', function(){
// filter down to items that are checked
var items = $('.items').filter(function(){
return this.checked;
});
// map data values to an array
items = items.map(function (){
return $(this).data('view');
}).get();
// join the array with a comma separator
$("#output").val(items.join(','));
});
Do note that binding a delegate to the document body is not generally the best way to go. You'll want to use a more direct ancestor of your checkboxes, or you can bind the change event to the checkboxes themselves.
This can be further simplified (forgot about :checked) like so:
$("body").on('change', '.items', function(){
// filter down to items that are checked, and get their data-view value.
var items = $('.items:checked').map(function(){
return $(this).data('view');
}).get();
// join the array with a comma separator
$("#output").val(items.join(','));
});
I have some radio buttons in a form:
<div id="data_id-block">
<dt id="data_id-label"><label class="required">Data</label></dt>
<dd id="data_id-element">
<label for="data_id-1">
<input type="radio" name="data_id" id="data_id-1" value="1" />test 1
</label><br />
<label for="data_id-2">
<input type="radio" name="data_id" id="data_id-2" value="2" />test 2
</label><br />
<label for="data_id-4">
<input type="radio" name="data_id" id="data_id-4" value="4" /> Test Data
</label><br />
<label for="data_id-5">
<input type="radio" name="data_id" id="data_id-5" value="5" /> Second Test Data
</label><br />
<label for="data_id-6">
<input type="radio" name="data_id" id="data_id-6" value="6" />Unassigned
</label>
</dd>
I'm trying to display a tooltip when a user hovers over the label of the radio button. I can do this but I also want to get whatever is in the 'value' property of the radio button. My attempts to this resulted in only the 'value' of the radio button being returned regardless of which radio button was hovered over.
Appreciate the help.
//this is just one way to register to the window.onLoad event in jQuery, i prefer it's readability for what is going on
$(document).ready(function(){
//$("something", "something else") - 'Something Else' limits where jQuery searches for 'something'
// .hover( function A , function B ) - function A is the mouseover event, function B is the mouseleave event
// function(event){} for All events, jQuery will always pass an "event" parameter if you ask for it on the window .event() registration. cool stuff you can do with jQuery's "event" parameter can be found here in the references below.
$("label", "#data_id-element").hover(function(event){
// $(this) jQuery'itize the html Element the user hovered into
// .attr("for") - get the value of the 'for' attribute
// $("#" + whatever) - get whatever this is by its id='' attribute
// .val() - get the value of whatever this is
var radioValue = $("#"+$(this).attr("for")).val();
},
function(event){
// $(this) jQuery'itize the html Element the user hovered out of
// .attr("for") - get the value of the 'for' attribute
// $("#" + whatever) - get whatever this is by its id='' attribute
// .val() - get the value of whatever this is
var radioValue = $("#"+$(this).attr("for")).val();
});
});
If I've understood you correctly, you want to get the value of the radio button associated with a label, when you hover over the label. If that's the case, try something like this:
$("label").mouseover(function() {
var radioButtonValue = $(this).find("input").val();
});
Here's a working example.