简体   繁体   English

通过表单将javascript / jquery数组传递给php

[英]Passing javascript/Jquery array to php through a form

So I have a jquery function that checks what checkboxes are checked and puts the id's in an array. 因此,我有一个jquery函数,用于检查选中了哪些复选框,并将ID放入数组中。 Now that array gets passed to the value of a hidden input field and later submitted to the php script. 现在,该数组将传递给隐藏输入字段的值,然后提交给php脚本。 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. 这是获取数组中选中复选框的ID的功能。 I read about push and so far I understood that push actually pushes a value to a new index? 我读到有关push的信息,到目前为止,我了解push实际上将值推入了新索引吗?

$(".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: 函数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: 和PHP:

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

echo $checks[0];

Now in PHP I want to iterate through an array instead of a string. 现在在PHP中,我想遍历数组而不是字符串。 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: 可以说,即使我选择了索引0,我也选中了复选框1和2上面echo 1,2的php代码。我也尝试了:

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

a better way of submitting a form data as key => value to a PHP script. 将表单数据作为键=>值提交到PHP脚本的一种更好的方法。

HTML 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 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 PHP

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM