Passing javascript/Jquery array to php through a form

DaViDa

So I have a jquery function that checks what checkboxes are checked and puts the id's in an array. Now that array gets passed to the value of a hidden input field and later submitted to the php script. What happens is, it looks like it puts every value in the first index instead of individual indexes.

This is the function for getting the id's of the checked checkboxes in the array. I read about push and so far I understood that push actually pushes a value to a new index?

$(".article-checkbox:checked").each(function() {
            multipleCheckboxes.push($(this).val());
        });

My form:

<form id="publish-form" method="post" action="publisharticle.php" onsubmit="getMultipleCheckboxValues()">
    <input type="hidden" value="" name="checkbox-value2[]" id="checkbox-value2">
    li><input type="submit" value="Publish"></li>
</form>

the function onsubmit:

function getMultipleCheckboxValues(){
    getValueUsingClass();
    $("#checkbox-value2").val(multipleCheckboxes);
}

function getValueUsingClass(){
        /* declare an checkbox array */
        var chkArray = [];
        var i = 0;

        /* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
        $(".article-checkbox:checked").each(function() {
            chkArray.push($(this).val());
            multipleCheckboxes.push($(this).val());
            i++;
        });

        /* we join the array separated by the comma */
        var selectedOne = chkArray[0];
        var selected;
        selected = chkArray.join(',') + ",";

        /* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
        if(selected.length > 1){
            oneCheckbox = selectedOne;
        //alert("Please at least one of the checkbox" + selectedCheckboxes); 
    }else{
        alert("Please at least one of the checkbox");   
    }
}

and php:

$checks =  $_POST['checkbox-value2'];

echo $checks[0];

Now in PHP I want to iterate through an array instead of a string. Lets say I check checkbox one and two the php code above echo's 1,2 even though I chose index 0. I also tried:

var i = 0;
$(".article-checkbox:checked").each(function() {
            chkArray.push($(this).val());
            multipleCheckboxes[i].push($(this).val());
            i++;
        });
Mody

a better way of submitting a form data as key => value to a PHP script.

HTML

<form id="publish-form" method="post">
    <input type="checkbox" id="checkbox1"> Checkbox1
    <input type="checkbox" id="checkbox2"> Checkbox2
     <input type="checkbox" id="checkbox3"> Checkbox3
    <button>Submit</button>
</form>

JavaScript

var items = {};

$('button').click(function () {
    $('#public-form input').each(function () {
        if ($(this).prop('checked')) {
            items[$(this).id] = $(this).val();
        }
    });

    $.post('/publisharticle.php', items).done(function (data) {
        // response from php script
        console.log(data);
    });

});

PHP

foreach($_POST as $key => $value){
    echo $key . ": " . $value . "\n";
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related