Asked  7 Months ago    Answers:  5   Viewed   36 times

i am posting an array of checkboxes. and i cant get it to work. i didnt include the proper syntax in the foreach loop to keep it simple. but it is working. i tested in by trying to do the same thing with a text field instead of a checkbox and it worked with the textfield.

<form method="post">
<input id="'.$userid.'" value="'.$userid.'"  name="invite[]" type="checkbox">
<input type="submit">';

here is the part that is not working. it is echoing 'invite' instead of array.

$invite = $_POST['invite'];
echo $invite;



Your $_POST array contains the invite array, so reading it out as

  $invite = $_POST['invite'];
  echo $invite;

won't work since it's an array. You have to loop through the array to get all of the values.

  if (is_array($_POST['invite'])) {
    foreach($_POST['invite'] as $value){
      echo $value;
  } else {
    $value = $_POST['invite'];
    echo $value;
Wednesday, March 31, 2021
answered 7 Months ago

Instead of checking !isset(), use empty(). If the form posts an empty string, it will still show up in the $_POST as an empty string, and isset() would return TRUE.

I've replaced your incremental for loop with a foreach loop, which is almost always used in PHP for iterating an array.

$out_data = array();
foreach ($form_data as $key) {
    if(empty($_POST[$key])) {
        $out_data[$key] = "NO_DATA";
    else {
        $out_data[$key] = $_POST[$key];
Saturday, May 29, 2021
answered 5 Months ago

Assuming every dict has a value key, you can write (assuming your list is named l)

[d['value'] for d in l]

If value might be missing, you can use

[d['value'] for d in l if 'value' in d]
Tuesday, June 1, 2021
answered 5 Months ago

There are some references and pointers in the comments on this page at

Torsten says

"Section C.8 of the XHTML spec's compatability guidelines apply to the use of the name attribute as a fragment identifier. If you check the DTD you'll find that the 'name' attribute is still defined as CDATA for form elements."

Jetboy says

"according to this: the type of the name attribute has been changed in XHTML 1.0, meaning that square brackets in XHTML's name attribute are not valid.

Regardless, at the time of writing, the W3C's validator doesn't pick this up on a XHTML document."

Tuesday, June 1, 2021
answered 5 Months ago

You can use: Array.prototype.find():

const data = {
  "Customers": [{
      "id": 24,
      "userid": 73063,
      "username": "BOB",
      "firstname": "BOB",
      "lastname": "LASTNAME"
      "id": 25,
      "userid": 73139,
      "username": "Billy",
      "firstname": "Billy",
      "lastname": "lasty"

const user = data.Customers.find(u => u.username === 'Billy');
const userid = user ? user.userid : 'not found';

Tuesday, September 21, 2021
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :