Posting array from form

前端 未结 7 742
刺人心
刺人心 2020-11-27 05:45

I have a form on my page with a bunch of inputs and some hidden fields, I\'ve been asked to pass this data through a \"post array\" only im unsure on how to do this,

<
相关标签:
7条回答
  • 2020-11-27 06:03

    Give each input a name in array format:

    <input type="hidden" name="data[EstPriceInput]" value="" />
    

    Then the in PHP $_POST['data']; will be an array:

      print_r($_POST);         // print out the whole post
      print_r($_POST['data']); // print out only the data array
    
    0 讨论(0)
  • 2020-11-27 06:05

    If you want everything in your post to be as $Variables you can use something like this:

    foreach($_POST as $key => $value) { eval("$" . $key . " = " . $value"); }

    0 讨论(0)
  • 2020-11-27 06:08

    When you post that data, it is stored as an array in $_POST.

    You could optionally do something like:

    <input name="arrayname[item1]">
    <input name="arrayname[item2]">
    <input name="arrayname[item3]">
    

    Then:

    $item1 = $_POST['arrayname']['item1'];
    $item2 = $_POST['arrayname']['item2'];
    $item3 = $_POST['arrayname']['item3'];
    

    But I fail to see the point.

    0 讨论(0)
  • 2020-11-27 06:08

    You're already doing that, as a matter of fact. When the form is submitted, the data is passed through a post array ($_POST). Your process.php is receiving that array and redistributing its values as individual variables.

    0 讨论(0)
  • 2020-11-27 06:19

    What you are doing is not necessarily bad practice but it does however require an extraordinary amount of typing. I would accomplish what you are trying to do like this.

    foreach($_POST as $var => $val){
        $$var = $val;
    }
    

    This will take all the POST variables and put them in their own individual variables. So if you have a input field named email and the luser puts in Someone@example.com you will have a var named $email with a value of "Someone@example.com".

    0 讨论(0)
  • 2020-11-27 06:23

    You can use the built-in function:

    extract($_POST);
    

    it will create a variable for each entry in $_POST.

    0 讨论(0)
提交回复
热议问题