Skip to content Skip to sidebar Skip to footer

How To Pass The Select Check Box Values Dynamically To The Ajax Call Variables Post To Php Variables Dynamically

I got nice select check box from eric hanes ...site : http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/ I have to pass the values that are selected from the sel

Solution 1:

Okay, here we go. From what i understand you have a multiselect, and want to push all those values that are selected in an array, and send it to a PHP file using ajax. Here's what i came up with:

$('[name=submit]').click(function(e){
    e.preventDefault();
    var array = [];
    $('select :selected').each(function(i,value){
        array[i] = $(this).val();
    });
    //here make your ajax call to a php file
    $.ajax({
        type: "POST",
        url: "path/to/file.php",
        data: { selected_values: array, otherVal: 'something else' }
    });
});

FIDDLE FOR MULTI SELECT

In your PHP page, use:

$array = $_POST['selected_values'];
foreach($arrayas$key => $value){
    //do something with the key/value.
}

Hope it helps you.

Solution 2:

You can get values of all the selected options like the following:

var arr = []; 
$('select[name="example-basic"] :selected').each(function(i, selected){ 
  arr[i] = $(selected).val(); 
});

or Using the .val() function on a multi-select list will return an array of the selected values:

var selectedValues = $('select[name="example-basic"]').val();

By using above jQuery code it will give you the value in array format. Where you can pass that array into php like the following:

$("#fetch").load("select.php",{var1:arr[0],var2:arr[1]......},function(){});

where 'arr[0]' is first selected option value and similarly so on.

Edited Code:

var str = '';
        $('select[name="example-basic"] :selected').each(function(i, selected){ 
           str += 'var'+(i+1) +'='+ $(selected).val()+'&'; 
        });

        $("#fetch").load("select.php",str,function(){});

Above is the code for your javascript and below is defined how you can get the values in server side :

<?php$var1= $_GET['var1'];
$var2 = $_GET['var2'];
.
.
.


echo$var1;
echo$var2;

?>

Hope this will help you. Thanks

Post a Comment for "How To Pass The Select Check Box Values Dynamically To The Ajax Call Variables Post To Php Variables Dynamically"