使 POST 值在循环中动态存储为数组?

发布于 2024-10-01 11:55:23 字数 1026 浏览 0 评论 0原文

我一直在尝试编写一个函数,该函数将获取任何给定表单提交的 POST 值,将它们弹出到数组中,使用修剪、addslashes 等循环遍历数组,然后将该值传递回变量传递到数据库。

现在我遇到的障碍是在提交表单时将所有输入、文本区域、选择元素数据放入数组中。我的代码如下

$fields = array($_POST['1'], $_POST['2']);

    $i = 0;
    foreach ($fields as $field) {
        $i++;
        ${'field'.$i } = trim(addslashes(strip_tags($field)));
        echo "POST field info #". $i ."&nbsp;-&nbsp;". ${'field'.$i }."<br />";
    }

正如您所看到的,这里一切都很好,只是 POST 值名称仍然静态输入,我需要的是一种将 POST 数据输入循环的方法,该循环使用增量动态调用 POST 名称变量,然后将所有数据弹出到同一个数组中。我尝试过的代码如下。

for ($ii=0;$ii++;) {
    foreach($_POST['$ii'] as $field) {
        $fields = array($field);
    }
}

    $i = 0;
    foreach ($fields as $field) {
        $i++;
        ${'field'.$i } = trim(addslashes(strip_tags($field)));
        echo "POST field info #". $i ."&nbsp;-&nbsp;". ${'field'.$i }."<br />";
    }

现在我知道这行不通,但我可以感觉到我已经比较接近了,所以我想知道是否有聪明的人可以帮助我解决最后一部分?遗憾的是,我现在要去睡觉了,至少 9 个小时不会看这篇文章,抱歉。

提前致谢。

担。

I've been working on trying to write a function that will grab the POST values of any given form submission, pop them into an array, loop through the array using trim, addslashes etcetera pass that value back to a variable where it can then be passed to a database.

Now the hurdle I have atm is getting all the input,textarea,select element data into an array upon form submission. code I have follows

$fields = array($_POST['1'], $_POST['2']);

    $i = 0;
    foreach ($fields as $field) {
        $i++;
        ${'field'.$i } = trim(addslashes(strip_tags($field)));
        echo "POST field info #". $i ." - ". ${'field'.$i }."<br />";
    }

As you can see everything is fine here baring that the POST value names are still being in-putted statically, what I need is a way to get that POST data fed into a loop which dynamically calls the POST name using an increment variable and then pop all that data into the same array. Code I have tried follows.

for ($ii=0;$ii++;) {
    foreach($_POST['$ii'] as $field) {
        $fields = array($field);
    }
}

    $i = 0;
    foreach ($fields as $field) {
        $i++;
        ${'field'.$i } = trim(addslashes(strip_tags($field)));
        echo "POST field info #". $i ." - ". ${'field'.$i }."<br />";
    }

Now I know this wont work but I can sense I am relatively close, so I am wondering if any clever person can help me sort the last part out? I sadly am now going to sleep and wont be viewing this post for at least 9 hours, apologies.

Thanks in advance.

Dan.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉 2024-10-08 11:55:23
$arrayOfPostValues = $_POST;  // it already is an array
$arrayOfPostValues = array_map('strip_tags', $arrayOfPostValues);
$arrayOfPostValues = array_map('trim', $arrayOfPostValues);

或者,如果您真的非常想使用循环:

foreach ($arrayOfPostValues as &$value) {
   $value = trim(striptags($value));
}

我绝对建议不要使用addslashes,它的作用很小。使用 mysql_real_escape_string准备好的语句 相反。

我还建议不要将数组中的值分解为单独的变量,这只会导致问题。如果您确实想这样做,可以使用 extract 函数,正是这样做的。但是,再次强调,不要这样做。数组是处理此类数据的完美方法。

$arrayOfPostValues = $_POST;  // it already is an array
$arrayOfPostValues = array_map('strip_tags', $arrayOfPostValues);
$arrayOfPostValues = array_map('trim', $arrayOfPostValues);

Or, if you really, really want to use a loop:

foreach ($arrayOfPostValues as &$value) {
   $value = trim(striptags($value));
}

I'd absolutely advise against the use of addslashes, it serves very little purpose. Use mysql_real_escape_string or prepared statements instead.

I'd also advise against breaking the vales out of the array into separate variables, it can only cause problems. If you really want to do it, there's the extract function, which does exactly that. But, again, don't do it. Arrays are the perfect way to handle this kind of data.

失而复得 2024-10-08 11:55:23

首先,您需要为 $_POST[1] 和 $_POST[2] 分配值,我已经为您完成了此操作,但通常它们会从我假设的表单中填充?

我不确定你为什么想做这种事情:${'field'.$key},但我保留了该部分,因为我认为你一定有一个原因。

不管怎样,我对你的代码做了一些修改,见下文。

$_POST['1'] = '<h1>variable 1</h1>';
$_POST['2'] = '<h2>variable 2</h2>';

foreach($_POST as $key => $value){
    ${'field'.$key} = trim(addslashes(strip_tags($value)));
    echo "POST field info #". $key ." = ". ${'field'.$key}."<br />";
}

上面的代码输出:
POST 字段信息 #1 = 变量 1
POST 字段信息 #2 = 变量 2

顺便说一句,使用“1”和“2”等字段名称并不是很好。尝试使用更具描述性的内容,但正如我上面所说,我认为您这样做是有原因的。


更新:
即使您对表单元素使用特定名称,您仍然可以使其适用于任何表单。我在下面添加了几行作为示例。

$_POST['email'] = '[email protected]';
$_POST['password'] = 'hgbks78db';
$_POST['name'] = '';

foreach($_POST as $key => $value){
    if($value==''){
        echo 'POST field "'.$key . '" is empty<br />';
        /* I added the name of the field that is empty to an error array 
        so you have a way of storing all blank fields */
        $a_error[] = $key;
    }
    else{
        echo 'POST field "'.$key . '" is not empty<br />';
    }
}

You need to assign values to $_POST[1] and $_POST[2] to begin with, I've done this for you but normally they would be populated from a form I assume?

I'm not sure why you want to do this sort of thing: ${'field'.$key}, but I've left that part as is as I assume you must have a reason.

Anyway I've modified your code a bit, see below.

$_POST['1'] = '<h1>variable 1</h1>';
$_POST['2'] = '<h2>variable 2</h2>';

foreach($_POST as $key => $value){
    ${'field'.$key} = trim(addslashes(strip_tags($value)));
    echo "POST field info #". $key ." = ". ${'field'.$key}."<br />";
}

The above code outputs:
POST field info #1 = variable 1
POST field info #2 = variable 2

On a side note, using field names such as '1' and '2' is not very good. Try using something more descriptive but as I said above I assume you have a reason for doing this.


UPDATE:
You can still get this to work for any form even if you are using specific names for the form elements. I have added a few lines below as an example for you.

$_POST['email'] = '[email protected]';
$_POST['password'] = 'hgbks78db';
$_POST['name'] = '';

foreach($_POST as $key => $value){
    if($value==''){
        echo 'POST field "'.$key . '" is empty<br />';
        /* I added the name of the field that is empty to an error array 
        so you have a way of storing all blank fields */
        $a_error[] = $key;
    }
    else{
        echo 'POST field "'.$key . '" is not empty<br />';
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文