I am trying to figure out how I can pass an active class/toggle class to AJAX to send to php. I have a section where divs look like buttons allowing the user to click on one or more and then that will change to show it is active. I am wanting to send the ones that were clicked on through AJAX to my php file. I was successfully able to send my inputs through AJAX, but I am stumped on this one.
The buttons look like this:
So the ones that are active, I want to pass to AJAX.
I get my buttons to look active by doing this:
$('.project-option-boxes').click(function() {
$(this).hide().toggleClass('box_focused').fadeIn('slow');
});
My HTML
<div class="project-container">
<div class="project-input-container">
<form action="" autocomplete="on" method="POST" id="project-information-form">
<input type="text" class="input-borderless" id="project-name" placeholder="Your Name">
<input type="text" class="input-borderless" id="title-roll" placeholder="Title/Role">
<input type="email" class="input-borderless" id="project-email" placeholder="Email Address">
<input type="text" class="input-borderless" id="project-number" placeholder="Phone Number">
<input type="text" class="input-borderless" id="project-company" placeholder="Company/URL">
</div>
<div id="project-scope-container">
<div id="project-scope-title">PROJECT SCOPE</div>
<div class="project-option-boxes">BRANDING & IDENTITY</div>
<div class="project-option-boxes">WEB DESIGN</div>
<div class="project-option-boxes">RESPONSIVE/MOBILE</div>
<div class="project-option-boxes">MARKETING ASSETS</div>
<div class="project-option-boxes">HTML5 ANIMATION</div>
<div class="project-option-boxes">SEO OPTIMIZATION</div>
<div class="project-option-boxes">MONTHLY SUPPORT</div>
<div class="project-option-boxes">WEB DEVELOPMENT</div>
<div class="project-option-boxes">ECOMMERCE</div>
</div>
<input type="submit" id="submit-project" value="Send Project Inquiry">
</form>
</div>
So far for my AJAX (for the inputs) I have this:
$(document).ready(function(){
$("#submit-project").on("click", function (event) {
event.preventDefault();
var project_name = $("#project-name").val();
var title_roll = $("#title-roll").val();
var project_email = $("#project-email").val();
var project_number = $("#project-number").val();
var project_company = $("#project-company").val();
var project_description = $("#project-description").val();
var project_source = $("#project-source").val();
var project_socialMedia = $("#project-socialMedia").val();
var project_humanTest = $("#project-humanTest").val();
$.ajax({
url: "email-project.php",
type: "POST",
data: {
"project_name": project_name,
"title_roll": title_roll,
"project_email": project_email,
"project_number": project_number,
"project_description": project_description,
"project_source": project_source,
"project_socialMedia": project_socialMedia,
"project_humanTest": project_humanTest
},
success: function (data) {
//console.log(data); // data object will return the response when status code is 200
if (data == "Error!") {
alert("Unable to send email!");
alert(data);
} else {
$(".project-container").addClass("removeClass");
$(".email-success").show();
$(".announcement_success").fadeIn();
$(".announcement_success").show();
$(".light-gray-container").hide();
// $('.announcement_success').html('Email Successfully sent!');
//$('.announcement_success').delay(5000).fadeOut(400);
}
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus + "|" + errorThrown);
//console.log("error"); //otherwise error if status code is other than 200.
}
});
});
});
Any help would be appreciated.
UPDATE: What I tried.
var project_company = $("#project-company").val();
var project_description = $("#project-description").val();
var project_source = $("#project-source").val();
var project_socialMedia = $("#project-socialMedia").val();
var project_humanTest = $("#project-humanTest").val();
var activeDivs = $('.project-option-boxes.box_focused').map(function(i, el){
return $(this).html();
});
$.ajax({
url: "email-project.php",
type: "POST",
data: {
"project_name": project_name,
"title_roll": title_roll,
"project_email": project_email,
"project_number": project_number,
"project_description": project_description,
"project_source": project_source,
"project_socialMedia": project_socialMedia,
"project_humanTest": project_humanTest,
"activeDivs": activeDivs
},
4
Answers
Well, you can simply use the
.each()
and then fetch the text value of the div and push it into an array. Then pass it along with the data to the AJAX callJust filter the active divs using
.project-option-boxes.box_focused
. Then using.map()
you can build an array to send-Then add that
activeDivs
to your ajax –see this jsFiddle example – https://jsfiddle.net/kdghucwn/
I’d add an id to each element:
Then I’d just set an array to and add or remove the selected options to it like so:
Then it’s just a matter of adding that variable to your ajax data:
EDIT: I made a fiddle for you to see how it works https://jsfiddle.net/2xxnxrdL/
You can pass all active divs in comma separated string to apply in IN Clause of SQL as —
Pass actives in ajax parameters or use it in SQL directly in IN Clause like —-