Passing javascript/Jquery array to php through a form -


So I have a jquery function that checks how the checkboxes are checked and puts the ID in an array. Now that array is sent to the value of a hidden input field and later it is sent to the PHP script. What happens like this, it seems that it puts each value in the first index instead of the individual index.

This is the task of obtaining the checkboxes of checkboxes checked in the array. I read about Push and by now I understood that Push actually pushes a price for a new index?

  $ (". -Check-checkbox: check"). Each (function () {multipleCheckboxes push ($ (this) .val ());});   

My form:

  & lt; Form id = "publish-form" method = "post" action = "publisharticle.php" onsubmit = "getMultipleCheckboxValues ​​()" & gt; & Lt; Input type = "hidden" value = "" name = "checkbox-value2 []" id = "checkbox-value2" & gt; Li & gt; & Lt; Input type = "submit" value = "publish" & gt; & Lt; / Li & gt; & Lt; / Form & gt;   

Function onsubmit:

  function getMultipleCheckboxValues ​​() {getValueUsingClass (); . $ ("# Checkbox-value 2") Values ​​(multipleCheckboxes); } Function getValueUsingClass () {/ * Declare a checkbox array / var chkArray = []; Var i = 0; / * Search for all checkboxes that have the class 'chk' attached and check whether it was checked / / $ (". -lid-checkbox: check"). Each (function () {chkArray.push ($ (this) .val ()); Multiple checkboxes ($ (this) .val ()); i ++;}); / * We are included in a separate array from commas * / var selectedOne = chkArray [0]; Var selected; Selected = chkArray.join (',') + ","; / * Check whether the selected checkboxes are selected, length is 1 by default because it has a single comma * / if (selected.length & gt; 1) {oneCheckbox = selectedOne; // Alert ("Please check at least one of the" checkboxes selected); } And {warnings ("Please at least one of the checkboxes"); }}   

and php:

  $ check = $ _POST ['checkbox-value2']; Resonance $ check [0];   

Now in PHP I want to redo a string instead of an array. Let's say I check the checkbox one and two above the phcho code echo 1, even though I chose the index number 0. I also tried:

  var i = 0; $ (". -lid-checkbox: check"). Each (function () {chkArray.push ($ (this) .val ()); Multiple checkboxes [i] .push ($ (this) .val ()); i ++;});    

A better way to submit a form data as a key = & gt; Value in PHP script

HTML

  & lt; Form id = "publish-form" method = "post" & gt; & Lt; Input type = "checkbox" id = "checkbox 1" & gt; Checkbox 1 & lt; Input type = "checkbox" id = "checkbox 2" & gt; Checkbox 2 & lt; Input type = "checkbox" id = "checkbox 3" & gt; Checkbox 3 & lt; Button & gt; Submit & lt; / Button & gt; & Lt; / Form & gt;   

javascript

  var items = {}; Click $ ('button') (function () {$ ('# Public-Form Input'). Each (function () {if ($ (this) .prop ('checked')) {items [$ (this ) .id] = $ (this) .val ();}}; $ .post ('/ publisharticle.php', item) .done (data) {// response from php script console.log ;});});   

  foreach ($ _ POST => $ value as the $ key) {echo $ key: $. $ Value "\ N";}    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -