Foreach value from POST from form

前端 未结 4 1219
野性不改
野性不改 2020-12-01 23:32

I post some data over to another page from a form. It\'s a shopping cart, and the form that\'s being submitted is being generated on the page before depending on how many it

相关标签:
4条回答
  • 2020-12-01 23:57

    i wouldn't do it this way

    I'd use name arrays in the form elements

    so i'd get the layout

    $_POST['field'][0]['name'] = 'value';
    $_POST['field'][0]['price'] = 'value';
    $_POST['field'][1]['name'] = 'value';
    $_POST['field'][1]['price'] = 'value';
    

    then you could do an array slice to get the amount you need

    0 讨论(0)
  • 2020-12-02 00:04

    Use array-like fields:

    <input name="name_for_the_items[]"/>
    

    You can loop through the fields:

    foreach($_POST['name_for_the_items'] as $item)
    {
      //do something with $item
    }
    
    0 讨论(0)
  • 2020-12-02 00:12

    If your post keys have to be parsed and the keys are sequences with data, you can try this:

    Post data example: Storeitem|14=data14

    foreach($_POST as $key => $value){
        $key=Filterdata($key); $value=Filterdata($value);
        echo($key."=".$value."<br>");
    }
    

    then you can use strpos to isolate the end of the key separating the number from the key.

    0 讨论(0)
  • 2020-12-02 00:14

    First, please do not use extract(), it can be a security problem because it is easy to manipulate POST parameters

    In addition, you don't have to use variable variable names (that sounds odd), instead:

    foreach($_POST as $key => $value) {
      echo "POST parameter '$key' has '$value'";
    }
    

    To ensure that you have only parameters beginning with 'item_name' you can check it like so:

    $param_name = 'item_name';
    if(substr($key, 0, strlen($param_name)) == $param_name) {
      // do something
    }
    
    0 讨论(0)
提交回复
热议问题