php - Get $_POST from multiple checkboxes without name key including '[]' -
php - Get $_POST from multiple checkboxes without name key including '[]' -
what best solution $_post info multiple checkboxes have same name attribute, without using this;
<input type="checkbox" name="some_value[]"> <input type="checkbox" name="some_value[]">
i'm using unbounce create landing page, , don't offer way of setting name attribute custom including '[]' denote php set values in array.
you can read post info using like:
$formdata = file_get_contents('php://input');
however, parsing "application/x-www-form-urlencoded", you'll want find third-party library somewhere, of native php options exhibit same behaviour (later keys override before ones) find normal $_post structure.
here's "toy" implementation of user-land version of parse_str(), added bonus 'duplicate' values turned array. create no claims quality of code handle possible edge-cases of "application/x-www-form-urlencoded" data:
<?php $form = file_get_contents('php://input'); $arg_sep = ini_get('arg_separator.input'); $max = ini_get('max_input_vars'); $token = strtok($form, $arg_sep); $data = []; while (false !== $token && $processed < $max) { if (false !== ($pos = strpos($token, '='))) { list($key, $value) = explode('=', $token); $value = urldecode($value); if (strlen($key)) { if (isset($data[$key])) { if (is_array($data[$key])) { array_push($data[$key], $value); } else { $data[$key] = [$data[$key], $value]; } } else { $data[$key] = $value; } } } $token = strtok($arg_sep); ++$processed; } var_dump($data);
for comparison, here guts of php's internal implementation - note there much more this, heart of key/value parsing logic:
switch (arg) { case parse_get: case parse_string: separator = (char *) estrdup(pg(arg_separator).input); break; case parse_cookie: separator = ";\0"; break; } var = php_strtok_r(res, separator, &strtok_buf); while (var) { val = strchr(var, '='); if (arg == parse_cookie) { /* remove leading spaces cookie names, needed multi-cookie header ; can followed space */ while (isspace(*var)) { var++; } if (var == val || *var == '\0') { goto next_cookie; } } if (++count > pg(max_input_vars)) { php_error_docref(null tsrmls_cc, e_warning, "input variables exceeded " zend_long_fmt ". increment limit alter max_input_vars in php.ini.", pg(max_input_vars)); break; } if (val) { /* have value */ size_t val_len; size_t new_val_len; *val++ = '\0'; php_url_decode(var, strlen(var)); val_len = php_url_decode(val, strlen(val)); val = estrndup(val, val_len); if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len tsrmls_cc)) { php_register_variable_safe(var, val, new_val_len, &array tsrmls_cc); } efree(val); } else { size_t val_len; size_t new_val_len; php_url_decode(var, strlen(var)); val_len = 0; val = estrndup("", val_len); if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len tsrmls_cc)) { php_register_variable_safe(var, val, new_val_len, &array tsrmls_cc); } efree(val); } next_cookie: var = php_strtok_r(null, separator, &strtok_buf); }
php post
Comments
Post a Comment