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">
<?php 
foreach{
echo'
<input id="'.$userid.'" value="'.$userid.'"  name="invite[]" type="checkbox">
<input type="submit">';
}
?>
</form>

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

<?php
    if(isset($_POST['invite'])){
$invite = $_POST['invite'];
echo $invite;
}

 Answers

55

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

<?php
if(isset($_POST['invite'])){
  $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.

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

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
 
axiomer
answered 5 Months ago
72

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
 
Classified
answered 5 Months ago
83

There are some references and pointers in the comments on this page at PHP.net:

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: http://www.w3.org/TR/xhtml1/#C_8 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
 
Yrtymd
answered 5 Months ago
56

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';

console.log(user);
console.log(userid);
Tuesday, September 21, 2021
 
cegfault
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 :
 
Share