UI Dev
UI Dev

Reputation: 699

Get the check box value after submitting a form

Here is my form & my current URL is .../pg/members/all

<form id="simplesearch" name="simplesearch" action="<?php echo $vars['url'];?>pg/members/searchuser" method="post">
<table class="people_search" style="border:none;">

       <div id="toggle_profile_type">
       <input type="hidden" value="0" name="meta_data_array_search_criteria[custom_profile_type]">
       <p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39242" class="messageCheckbox"><span>Job Seeker</span></p>
       <p class="skills_odd1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39243" class="messageCheckbox"><span>Employer</span></p>
       <p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39449" class="messageCheckbox"><span>college</span></p>
       </div>

</table>
</form>

In script i have written

$(document).ready(function(){
  $(":checkbox").click(function(){
    simplesearchsubmitform();
  }); 
});

function simplesearchsubmitform()
{
  if(document.simplesearchonsubmit && !document.simplesearch.onsubmit()) {
    return;
  }
  document.simplesearch.submit();
}

I am submitting the form then the URL changes ..../pg/members/searchuser. Now i need to get the selected checkbox values. So that i can append that value to in a div & the checkbox will remain selected.

Any idea guys how do it ? I searched a lot but i didn't found the solution. plz help me guys..

Upvotes: 0

Views: 1657

Answers (4)

Beardminator
Beardminator

Reputation: 774

At "pg/members/searchuser"

Update 2.0 You use this code:

<?php

if(isset($_POST['meta_data_array_search_criteria']))
{  

   foreach($_POST['meta_data_array_search_criteria'] as $val)
   {
        foreach($val as $checkbox_data)
        {
            echo $checkbox_data."<br/>";
        }
   }
 } else 
 {
     echo "No checkbox checked";
 }
?>

How to select the checkboxes: There are 2 methods, with JS or with PHP. The jQuery snippet:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<script type="text/javascript">
$(document).ready(function()
{


<?php

if(isset($_POST['meta_data_array_search_criteria']))
{  

   foreach($_POST['meta_data_array_search_criteria'] as $val)
   {
        foreach($val as $checkbox_data)
        {

            ?>
            $('input:checkbox[value="<?php echo $checkbox_data;?>"]').attr('checked','checked');
            <?php
        }
   }
 } else 
 {
     echo "No checkbox checked";
 }
?>
})
</script>

What it does? It will select those checkboxes,which values we got from $_POST.

Upvotes: 5

Chris Lim
Chris Lim

Reputation: 424

You can check this sample, it will display the checked value based on the post

    <?php
    if (isset($_POST['meta_data_array_search_criteria'])){
        $data = $_POST['meta_data_array_search_criteria'];

        if (is_array($data['custom_profile_type'])){
            //check and display checked value
            if (in_array('39242', $data['custom_profile_type']))
                echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39242" class="messageCheckbox" checked><span>Job Seeker</span></p>';
            else
                echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39242" class="messageCheckbox"><span>Job Seeker</span></p>';

            if (in_array('39243', $data['custom_profile_type']))
                    echo '<p class="skills_odd1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39243" class="messageCheckbox" checked><span>Employer</span></p>';
                else
                    echo '<p class="skills_odd1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39243" class="messageCheckbox"><span>Employer</span></p>';

            if (in_array('39449', $data['custom_profile_type']))
                    echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39449" class="messageCheckbox" checked><span>college</span></p>';
                else
                    echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39449" class="messageCheckbox"><span>college</span></p>';

        }
        else{
            //display all unchecked
            echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39242" class="messageCheckbox"><span>Job Seeker</span></p>';
            echo '<p class="skills_odd1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39243" class="messageCheckbox"><span>Employer</span></p>';
            echo '<p class="skills_even1"><input type="checkbox" name="meta_data_array_search_criteria[custom_profile_type][]" value="39449" class="messageCheckbox"><span>college</span></p>';
        }

    }   
?>

Upvotes: 0

Timo.Klement
Timo.Klement

Reputation: 637

You are sending that form with POST to a new URL. So you need to catch all values with PHP and not with JavaScript. At ".../pg/members/searchuser" you just ask for all $_POST and then select your checkboxes with these values.

Upvotes: 0

bosnjak
bosnjak

Reputation: 8614

Try getting the values into an array like this:

var values = new Array();
$.each($("input[name='meta_data_array_search_criteria[custom_profile_type][]']:checked"), function() {
  values.push($(this).val());
});

Upvotes: 0

Related Questions