Home > Backend Development > PHP Tutorial > How PHP can handle a large number of form fields conveniently

How PHP can handle a large number of form fields conveniently

*文
Release: 2023-03-18 12:08:02
Original
1580 people have browsed it

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 ,,...,?

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";
Copy after login


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]">
Copy after login

Point 2:

PHP background program receives the $setting array through POST

Point 3:

Insert form field display

$fields=array(&#39;a&#39;,&#39;b&#39;,……,&#39;z&#39;);//这个是特意设置校验字典,校验提交的字段是否存在
foreach($setting as $k=>$v) {
            if(in_array($k, $fields)) { $sqlk .= &#39;,&#39;.$k; $sqlv .= ",&#39;$v&#39;"; }
        }
        $sqlk = substr($sqlk, 1);
        $sqlv = substr($sqlv, 1);
    $sql="INSERT INTO kele_table ($sqlk) VALUES ($sqlv)";
Copy after login

Update form field display

$sql = &#39;&#39;;
        foreach($setting as $k=>$v) {
            if(in_array($k, $fields)) $sql .= ",$k=&#39;$v&#39;";
        }
 $sql = substr($sql, 1);
 $sql="UPDATE kele_table SET $sql WHERE id=$id";
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template