Home > Article > Backend Development > How PHP can handle a large number of form fields conveniently
Sometimes, the form will have many fields that we need to process. Writing this kind of data is very cumbersome. Is there a simpler and more elegant way? This article explains a way to use arrays to quickly and conveniently process large amounts of form data. I hope to be helpful.
About the form batch submission strategy in program development
Many times a form has too many fields. How can we obtain the form fields efficiently? How can we improve the efficiency and uniformity of development?
For example, if there are 26 fields in a system, then I use 26 letters from a to z in the name of the form.
You choose d277d522b7ba037facda75c960f1d2a8,e4e607e40a4090ab921dae8177835388,...,b52115e0c5f60dd3e17ec2da978fcf82?
But in this case, if you do batch data insertion, it will not be so simple,
because the insertion or editing operation will be such a statement: especially such a painfully long SQL string is more Sad.
$sql="INSERT kele_table(a,b,……,z) value(a='$a',b='$b',……,z='$z')";//这样写很长铁牛用省略号标示 $sql="UPDATE SET kele_table(a='$a',b='$b',……,z='$z') where id=$id";
It’s quite troublesome to write like this, the string is too long
It is better to use the following method:
Point 1: Use an array for the entire submitted form field model.
<input type="text" name="setting[a]">,……,<input type="text" name="setting[z]">
Point 2:
PHP background program receives the $setting array through POST
Point 3:
Insert form field display
$fields=array('a','b',……,'z');//这个是特意设置校验字典,校验提交的字段是否存在 foreach($setting as $k=>$v) { if(in_array($k, $fields)) { $sqlk .= ','.$k; $sqlv .= ",'$v'"; } } $sqlk = substr($sqlk, 1); $sqlv = substr($sqlv, 1); $sql="INSERT INTO kele_table ($sqlk) VALUES ($sqlv)";
Update form field display
$sql = ''; foreach($setting as $k=>$v) { if(in_array($k, $fields)) $sql .= ",$k='$v'"; } $sql = substr($sql, 1); $sql="UPDATE kele_table SET $sql WHERE id=$id";
Related recommendations:
php form validation implementation code
PHP Form Validation - Verify E-mail and URL
PHP form method to prevent refresh submission
The above is the detailed content of How PHP can handle a large number of form fields conveniently. For more information, please follow other related articles on the PHP Chinese website!