dtv7174 2010-11-11 00:18
浏览 44
已采纳

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

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 ."&nbsp;-&nbsp;". ${'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 ."&nbsp;-&nbsp;". ${'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.

  • 写回答

2条回答 默认 最新

  • dsjj15012 2010-11-11 01:56
    关注

    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'] = 'example@example.com';
    $_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 />';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义