skip to Main Content

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:

enter image description here

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


  1. 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 call

    all_focused = [];
    $('.project-option-boxes.box_focused').each(function(){
        all_focused.push($(this).text());
    });
    
    //Append to 'data'
    $.ajax({
        ...
        data: {
            ...
            "project_scope" : JSON.stringify(all_focused),
            ...
    
    all_focused = [];
    $('.box_focused').each(function(){
      all_focused.push($(this).text());
    });
    
    console.log(all_focused);
    .box_focused {
      font-weight: bold;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div id="project-scope-container">
        <div id="project-scope-title">PROJECT SCOPE</div>
        <div class="project-option-boxes box_focused">BRANDING & IDENTITY</div>
        <div class="project-option-boxes">WEB DESIGN</div>
        <div class="project-option-boxes box_focused">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>
    Login or Signup to reply.
  2. Just filter the active divs using .project-option-boxes.box_focused. Then using .map() you can build an array to send-

    var activeDivs = $('.project-option-boxes.box_focused').map(function(i, el){
                return $(this).html();
    });
    

    Then add that activeDivs to your ajax –

        $.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  //add the array to your data
            },
            ...
    

    see this jsFiddle example – https://jsfiddle.net/kdghucwn/

    Login or Signup to reply.
  3. I’d add an id to each element:

    <div id="project-scope-container">
        <div id="project-scope-title">PROJECT SCOPE</div>
        <div class="project-option-boxes" id="brandingAndIdentity">BRANDING & IDENTITY</div>
        <div class="project-option-boxes" id="webDesign">WEB DESIGN</div>
        <div class="project-option-boxes" id="responsive">RESPONSIVE/MOBILE</div>
        <div class="project-option-boxes" id="marketingAssets">MARKETING ASSETS</div>
        <div class="project-option-boxes" id="html5Animation">HTML5 ANIMATION</div>
        <div class="project-option-boxes" id="seoOptimization">SEO OPTIMIZATION</div>
        <div class="project-option-boxes" id="monthlySupport">MONTHLY SUPPORT</div>
        <div class="project-option-boxes" id="webDevelopment">WEB DEVELOPMENT</div>
        <div class="project-option-boxes" id="ecommerce">ECOMMERCE</div>
    </div>
    

    Then I’d just set an array to and add or remove the selected options to it like so:

    var selectedProjectOptions = [];
    
    $('.project-option-boxes').click(function() {
        $(this).hide().toggleClass('box_focused').fadeIn('slow');
    
        //get the option's id
        var selectedOption = $(this).attr('id');
        //check to see if it's in the selectedProjectOptions array
        var optionIndex = selectedProjectOptions.indexOf(selectedOption);
        //If it has an index in the array already then it's being toggled off -- so we remove it.
        //If it isn't, we're adding to it.
        if(optionIndex > -1){
            selectedProjectOptions.splice(optionIndex, 1);
        } else {
            selectedProjectOptions.push(selectedOption);
        }
    });
    

    Then it’s just a matter of adding that variable to your ajax data:

    data: {
        ...
        "project_options": selectedProjectOptions,
    }
    

    EDIT: I made a fiddle for you to see how it works https://jsfiddle.net/2xxnxrdL/

    Login or Signup to reply.
  4. You can pass all active divs in comma separated string to apply in IN Clause of SQL as —

    Var actives="";
    $('.project-option-   boxes  .box_focused').each(function() { 
    If (actives != "")
     avtives += ","+$(this).text();
    Else
    actives = $(this).text();
     });
    

    Pass actives in ajax parameters or use it in SQL directly in IN Clause like —-

    Where project_scope IN(actives);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search