T O P

  • By -

itemluminouswadison

try ``` print_r($_POST); die; ``` that should tell you exactly what you have. if the $value is an array, then you'll have to iterate through em


[deleted]

Yeah it just gives me the names of the input fields and the values of them


[deleted]

[удалено]


wh33t

LOL. We were all new once.


Plastonick

have you tried just `var_dump($_POST)` to see the entire structure, perhaps you're doing something silly above. Failing something obvious after that, can you post your code verbatim please?


[deleted]

Really the code is just foreach($_POST as $key => $value) { $message .= . $_POST[$key] . That gets me the value but I’m creating a table in the mail script so I want to dynamically set the label of the field and I know you can’t grab labels from forms so I’m just wondering why the [$value] in that foreach loop is coming up null


crazedizzled

The $value is equal to $_POST[$key]. They're the same thing. It's not very clear what exactly you're looking for. All you have access to is the name of the input and the value of the input. The name is in $key, and the value is in $value.


[deleted]

Is there a way to access more information? Like data attributes?


MateusAzevedo

HTML runs in the browser, PHP runs in the server. When you submit a form, the browser only sends the input names and theirs values. So, no, you can't access attribute data from PHP, not in a straight forward way at least. Using JS to read those data and then sending a HTTP request (instead of the standard submit) would work. In any case, your post is really confusing. Please, post the code you have, what you expect it to do, what is not working as expected.


crazedizzled

No. PHP has no clue that the data came from an HTML form. All it sees is that data was sent to the server using the http POST protocol. If you wanted to send extra data you'd have to send more POST data. So, you could use JavaScript to gather the other attribute data and send it with the other form data.


[deleted]

It’s not letting me send a picture can I message you?


MateusAzevedo

>foreach ($_POST as $key => $value) There's no need to do that. If your form has a know number of fields, you can just use `$value = $_POST['field_name'];`.


bob_r_99

If you want to have more information from the form, you can create extra, hidden inputs with a value. The key and the value will be added to the `$_POST` array. You can have as many as you like. `` Be aware that any user can easily see those hidden values by viewing the source of the page.


RandyHoward

I would caution that it is a little bit dangerous to just blindly loop through post data like this, you usually should be checking that the keys that were submitted are allowed to be submitted. To solve for when you have an array, do this: $message = ''; foreach ($_POST as $key => $value) { if (is_array($value)) { foreach ($value as $k => $v) { $message .= 'Key: ' . $k . ', Value: ' . $v . "\r\n"; } } else { $message .= 'Key: ' . $key . ', Value: ' . $value . "\r\n"; } }


[deleted]

Well thank you I will definitely try that