Remove submit button value from $_POST form data P

2020-03-31 05:05发布

I'm using this cycle to print all form $_POST data:

foreach($_POST as $name => $value) {
print "$name : $value<br>";
}

And at the end of result is submit button value (submit : Edit) and this cause error for me, because with this foreac cycle I'm adding data to excel document $name is cell, $value is cell value so how to remove button value from the list?

标签: php forms post
5条回答
兄弟一词,经得起流年.
2楼-- · 2020-03-31 05:17

Just skip it with continue;

foreach($_POST as $name => $value) {
    if($name == "submit") continue;
    print "$name : $value<br>";
}
查看更多
啃猪蹄的小仙女
3楼-- · 2020-03-31 05:31

Your submit button does not need to have a name attribute in your HTML. If it has no name, it will not be present in the POST data.

查看更多
可以哭但决不认输i
4楼-- · 2020-03-31 05:34

What you are doing is not good practice. In this case, passing the data into an Excel spreadsheet, you are unlikely to have problems, nevertheless, this is a dangerous habit to get into.

You have designed your forms and given names to each of your inputs, so you know what indexes your $_POST array will contain ahead of time. You should explicitly reference only those values in your $_POST array and validate each one as required.

Do not forget that $_POST comes from the user and is, therefore, untrustworthy. Extra fields can be added to the $_POST array and, as it stands, your code will happily process them.

This may or may not be an issue in your code, but you should at least think about it.

查看更多
Root(大扎)
5楼-- · 2020-03-31 05:35

What about

foreach($_POST as $name => $value) {
    if($name != "submit"){
        print "$name : $value<br>";
    }
}
查看更多
The star\"
6楼-- · 2020-03-31 05:41

you can just unset it before you do your foreach:

unset($_POST['submit']);
查看更多
登录 后发表回答