Get value of hidden inpute to $_POST












-2














I've assigned a hidden input into my form which will store values of checked checkboxes (that part is working fine as can be seen in this picture):



enter image description here



The value of this hidden input is converted to a string By the way, in order to perform explode on it later.



When I try to access it in PHP via var_dump(['distribution']) I get this data: string(0) "".



Code on php side:



if(isset($_POST['distribution'])){
var_dump($_POST['distribution']);
die;
}


just in case, here is my html: <input type="hidden" name="distribution" />



and here is my JS:



function isChecked(){
let distributionEL = document.querySelector("[name='distribution']");
console.log(distributionEL.value);
sendingLists.forEach(function(list) {
let sendSMSArr = distributionEL.value.split(',');
if(list.checked == true){
sendSMSArr.push(list.value);
} else {
let index = sendSMSArr.indexOf(list.value)
if (index > -1) {
sendSMSArr.splice(index, 1);
}
}
distributionEL.value = sendSMSArr.join(',');
});
}


Update:



I've used name='distribution' when creating the inputs and its working, but,
when someone uses the search bar which creates checkboxes according to the query it won't accumulate values from pre search and post search.



my ajax:



const resToHtml = res => {
// create a virtual fragment where we will create all the HTML in
let resultDiv = document.createElement('div');

// iterates on each option
res.forEach(({
sendingListName,
sendingListID
}) => {

// create an <li>
let liEl = document.createElement('li');


// create a <span> and an <input>
let spanEl = document.createElement('span');
let inputEl = document.createElement('input');

// create class attribute and append it to input
let elementClass = document.createAttribute("class");
let nameClass = document.createAttribute("name");
elementClass.value = "sending-lists";
nameClass.value = "distribution";
inputEl.setAttributeNode(elementClass);
inputEl.setAttributeNode(nameClass);

// the <span> will hold the visible text
spanEl.textContent = sendingListName;

// the <input> will be a checkbox with a value
inputEl.type = "checkbox";
inputEl.value = sendingListID;
inputEl.name = 'distribution';



// put the <input> and the <span> inside the <div>
liEl.appendChild(inputEl);
liEl.appendChild(spanEl);

// add the <li> into the <div>
resultDiv.appendChild(liEl);
})

// apply all the elements into the <div>
listREl.innerHTML = '';
listREl.innerHTML = resultDiv.innerHTML;
}









share|improve this question
























  • var_dump(['distribution']) would make an array with a string in it. Not sure how you are getting string 0 from that. 3v4l.org/OaQvc
    – user3783243
    Nov 19 at 2:05


















-2














I've assigned a hidden input into my form which will store values of checked checkboxes (that part is working fine as can be seen in this picture):



enter image description here



The value of this hidden input is converted to a string By the way, in order to perform explode on it later.



When I try to access it in PHP via var_dump(['distribution']) I get this data: string(0) "".



Code on php side:



if(isset($_POST['distribution'])){
var_dump($_POST['distribution']);
die;
}


just in case, here is my html: <input type="hidden" name="distribution" />



and here is my JS:



function isChecked(){
let distributionEL = document.querySelector("[name='distribution']");
console.log(distributionEL.value);
sendingLists.forEach(function(list) {
let sendSMSArr = distributionEL.value.split(',');
if(list.checked == true){
sendSMSArr.push(list.value);
} else {
let index = sendSMSArr.indexOf(list.value)
if (index > -1) {
sendSMSArr.splice(index, 1);
}
}
distributionEL.value = sendSMSArr.join(',');
});
}


Update:



I've used name='distribution' when creating the inputs and its working, but,
when someone uses the search bar which creates checkboxes according to the query it won't accumulate values from pre search and post search.



my ajax:



const resToHtml = res => {
// create a virtual fragment where we will create all the HTML in
let resultDiv = document.createElement('div');

// iterates on each option
res.forEach(({
sendingListName,
sendingListID
}) => {

// create an <li>
let liEl = document.createElement('li');


// create a <span> and an <input>
let spanEl = document.createElement('span');
let inputEl = document.createElement('input');

// create class attribute and append it to input
let elementClass = document.createAttribute("class");
let nameClass = document.createAttribute("name");
elementClass.value = "sending-lists";
nameClass.value = "distribution";
inputEl.setAttributeNode(elementClass);
inputEl.setAttributeNode(nameClass);

// the <span> will hold the visible text
spanEl.textContent = sendingListName;

// the <input> will be a checkbox with a value
inputEl.type = "checkbox";
inputEl.value = sendingListID;
inputEl.name = 'distribution';



// put the <input> and the <span> inside the <div>
liEl.appendChild(inputEl);
liEl.appendChild(spanEl);

// add the <li> into the <div>
resultDiv.appendChild(liEl);
})

// apply all the elements into the <div>
listREl.innerHTML = '';
listREl.innerHTML = resultDiv.innerHTML;
}









share|improve this question
























  • var_dump(['distribution']) would make an array with a string in it. Not sure how you are getting string 0 from that. 3v4l.org/OaQvc
    – user3783243
    Nov 19 at 2:05
















-2












-2








-2







I've assigned a hidden input into my form which will store values of checked checkboxes (that part is working fine as can be seen in this picture):



enter image description here



The value of this hidden input is converted to a string By the way, in order to perform explode on it later.



When I try to access it in PHP via var_dump(['distribution']) I get this data: string(0) "".



Code on php side:



if(isset($_POST['distribution'])){
var_dump($_POST['distribution']);
die;
}


just in case, here is my html: <input type="hidden" name="distribution" />



and here is my JS:



function isChecked(){
let distributionEL = document.querySelector("[name='distribution']");
console.log(distributionEL.value);
sendingLists.forEach(function(list) {
let sendSMSArr = distributionEL.value.split(',');
if(list.checked == true){
sendSMSArr.push(list.value);
} else {
let index = sendSMSArr.indexOf(list.value)
if (index > -1) {
sendSMSArr.splice(index, 1);
}
}
distributionEL.value = sendSMSArr.join(',');
});
}


Update:



I've used name='distribution' when creating the inputs and its working, but,
when someone uses the search bar which creates checkboxes according to the query it won't accumulate values from pre search and post search.



my ajax:



const resToHtml = res => {
// create a virtual fragment where we will create all the HTML in
let resultDiv = document.createElement('div');

// iterates on each option
res.forEach(({
sendingListName,
sendingListID
}) => {

// create an <li>
let liEl = document.createElement('li');


// create a <span> and an <input>
let spanEl = document.createElement('span');
let inputEl = document.createElement('input');

// create class attribute and append it to input
let elementClass = document.createAttribute("class");
let nameClass = document.createAttribute("name");
elementClass.value = "sending-lists";
nameClass.value = "distribution";
inputEl.setAttributeNode(elementClass);
inputEl.setAttributeNode(nameClass);

// the <span> will hold the visible text
spanEl.textContent = sendingListName;

// the <input> will be a checkbox with a value
inputEl.type = "checkbox";
inputEl.value = sendingListID;
inputEl.name = 'distribution';



// put the <input> and the <span> inside the <div>
liEl.appendChild(inputEl);
liEl.appendChild(spanEl);

// add the <li> into the <div>
resultDiv.appendChild(liEl);
})

// apply all the elements into the <div>
listREl.innerHTML = '';
listREl.innerHTML = resultDiv.innerHTML;
}









share|improve this question















I've assigned a hidden input into my form which will store values of checked checkboxes (that part is working fine as can be seen in this picture):



enter image description here



The value of this hidden input is converted to a string By the way, in order to perform explode on it later.



When I try to access it in PHP via var_dump(['distribution']) I get this data: string(0) "".



Code on php side:



if(isset($_POST['distribution'])){
var_dump($_POST['distribution']);
die;
}


just in case, here is my html: <input type="hidden" name="distribution" />



and here is my JS:



function isChecked(){
let distributionEL = document.querySelector("[name='distribution']");
console.log(distributionEL.value);
sendingLists.forEach(function(list) {
let sendSMSArr = distributionEL.value.split(',');
if(list.checked == true){
sendSMSArr.push(list.value);
} else {
let index = sendSMSArr.indexOf(list.value)
if (index > -1) {
sendSMSArr.splice(index, 1);
}
}
distributionEL.value = sendSMSArr.join(',');
});
}


Update:



I've used name='distribution' when creating the inputs and its working, but,
when someone uses the search bar which creates checkboxes according to the query it won't accumulate values from pre search and post search.



my ajax:



const resToHtml = res => {
// create a virtual fragment where we will create all the HTML in
let resultDiv = document.createElement('div');

// iterates on each option
res.forEach(({
sendingListName,
sendingListID
}) => {

// create an <li>
let liEl = document.createElement('li');


// create a <span> and an <input>
let spanEl = document.createElement('span');
let inputEl = document.createElement('input');

// create class attribute and append it to input
let elementClass = document.createAttribute("class");
let nameClass = document.createAttribute("name");
elementClass.value = "sending-lists";
nameClass.value = "distribution";
inputEl.setAttributeNode(elementClass);
inputEl.setAttributeNode(nameClass);

// the <span> will hold the visible text
spanEl.textContent = sendingListName;

// the <input> will be a checkbox with a value
inputEl.type = "checkbox";
inputEl.value = sendingListID;
inputEl.name = 'distribution';



// put the <input> and the <span> inside the <div>
liEl.appendChild(inputEl);
liEl.appendChild(spanEl);

// add the <li> into the <div>
resultDiv.appendChild(liEl);
})

// apply all the elements into the <div>
listREl.innerHTML = '';
listREl.innerHTML = resultDiv.innerHTML;
}






php superglobals






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 11:01

























asked Nov 19 at 2:02









TheProphet

196




196












  • var_dump(['distribution']) would make an array with a string in it. Not sure how you are getting string 0 from that. 3v4l.org/OaQvc
    – user3783243
    Nov 19 at 2:05




















  • var_dump(['distribution']) would make an array with a string in it. Not sure how you are getting string 0 from that. 3v4l.org/OaQvc
    – user3783243
    Nov 19 at 2:05


















var_dump(['distribution']) would make an array with a string in it. Not sure how you are getting string 0 from that. 3v4l.org/OaQvc
– user3783243
Nov 19 at 2:05






var_dump(['distribution']) would make an array with a string in it. Not sure how you are getting string 0 from that. 3v4l.org/OaQvc
– user3783243
Nov 19 at 2:05














1 Answer
1






active

oldest

votes


















1














Why use JS to append values and store as a string when you can just access them as an array in PHP if you use proper naming?



<form name="demo" method="POST" action="<?php print($_SERVER['PHP_SELF']); ?>">
<input type=checkbox name="checkbox" value="val1"> check 1<br />
<input type=checkbox name="checkbox" value="val2"> check 2<br />
<input type=checkbox name="checkbox" value="val3"> check 3<br />
<input type=submit name=submit value="submit">
</form>


Then in the PHP you can access the values :



<?php

print("Selected checkbox values were: ".implode(",",$_POST['checkbox']))."<br />");

?>





share|improve this answer





















  • I've update the question. can you help with that? @ivanivan ?
    – TheProphet
    Nov 19 at 11:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367376%2fget-value-of-hidden-inpute-to-post%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Why use JS to append values and store as a string when you can just access them as an array in PHP if you use proper naming?



<form name="demo" method="POST" action="<?php print($_SERVER['PHP_SELF']); ?>">
<input type=checkbox name="checkbox" value="val1"> check 1<br />
<input type=checkbox name="checkbox" value="val2"> check 2<br />
<input type=checkbox name="checkbox" value="val3"> check 3<br />
<input type=submit name=submit value="submit">
</form>


Then in the PHP you can access the values :



<?php

print("Selected checkbox values were: ".implode(",",$_POST['checkbox']))."<br />");

?>





share|improve this answer





















  • I've update the question. can you help with that? @ivanivan ?
    – TheProphet
    Nov 19 at 11:02
















1














Why use JS to append values and store as a string when you can just access them as an array in PHP if you use proper naming?



<form name="demo" method="POST" action="<?php print($_SERVER['PHP_SELF']); ?>">
<input type=checkbox name="checkbox" value="val1"> check 1<br />
<input type=checkbox name="checkbox" value="val2"> check 2<br />
<input type=checkbox name="checkbox" value="val3"> check 3<br />
<input type=submit name=submit value="submit">
</form>


Then in the PHP you can access the values :



<?php

print("Selected checkbox values were: ".implode(",",$_POST['checkbox']))."<br />");

?>





share|improve this answer





















  • I've update the question. can you help with that? @ivanivan ?
    – TheProphet
    Nov 19 at 11:02














1












1








1






Why use JS to append values and store as a string when you can just access them as an array in PHP if you use proper naming?



<form name="demo" method="POST" action="<?php print($_SERVER['PHP_SELF']); ?>">
<input type=checkbox name="checkbox" value="val1"> check 1<br />
<input type=checkbox name="checkbox" value="val2"> check 2<br />
<input type=checkbox name="checkbox" value="val3"> check 3<br />
<input type=submit name=submit value="submit">
</form>


Then in the PHP you can access the values :



<?php

print("Selected checkbox values were: ".implode(",",$_POST['checkbox']))."<br />");

?>





share|improve this answer












Why use JS to append values and store as a string when you can just access them as an array in PHP if you use proper naming?



<form name="demo" method="POST" action="<?php print($_SERVER['PHP_SELF']); ?>">
<input type=checkbox name="checkbox" value="val1"> check 1<br />
<input type=checkbox name="checkbox" value="val2"> check 2<br />
<input type=checkbox name="checkbox" value="val3"> check 3<br />
<input type=submit name=submit value="submit">
</form>


Then in the PHP you can access the values :



<?php

print("Selected checkbox values were: ".implode(",",$_POST['checkbox']))."<br />");

?>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 2:29









ivanivan

1,609258




1,609258












  • I've update the question. can you help with that? @ivanivan ?
    – TheProphet
    Nov 19 at 11:02


















  • I've update the question. can you help with that? @ivanivan ?
    – TheProphet
    Nov 19 at 11:02
















I've update the question. can you help with that? @ivanivan ?
– TheProphet
Nov 19 at 11:02




I've update the question. can you help with that? @ivanivan ?
– TheProphet
Nov 19 at 11:02


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367376%2fget-value-of-hidden-inpute-to-post%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Volksrepublik China

How to test boost logger output in unit testing?

Write to the output between two pipeline