如何从php表单中保存值到数组

时间:2015-10-29 10:10:25

标签: php arrays forms

这里我有一个表单,其中有一个操作到另一个页面,它将值插入到database.i也有一些相同形式的字段,我只希望那些字段值存储在php数组

代码:

    <form class="form-horizontal" name="product-form" method="post" action="files/insert.php" role="form" enctype="multipart/form-data" onsubmit="return validateForm()">
                  <div class="form-group">
                    <label class="control-label col-sm-2" for="product_name">Product Name</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="product_name" id="product_name" placeholder="Iphone 5c" required>
                    </div>
                  </div>

                  <div class="form-group">
                    <label class="control-label col-sm-2" for="product_price">Product Price</label>
                    <div class="col-sm-10">
                      <input type="number" class="form-control" name="product_price" id="product_price" placeholder="36,000" required>
                    </div>
                  </div>

                  <div class="form-group">
                    <label class="control-label col-sm-2" for="address">Description about product</label>
                    <div class="col-sm-10">          
                       <textarea class="responsive-textarea" rows="2" id="textarea" name="product_description" placeholder="Enter a short synopsis"></textarea>
                    </div>
                  </div>


            <div class="form-group">
                    <label class="control-label col-sm-2" for="product_life">Life of product</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="product_life" id="product_life" placeholder="6years, 5 years, 2 years" required>
                    </div>
            </div>

            <div class="form-group">
                    <label class="control-label col-sm-2" for="refurbishment_factor">Refurbishment Factor</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="refurbishment_factor" id="refurbishment_factor" placeholder="1.1, 2.1, 3.1" required>
                    </div>
            </div>


            <div class="form-group">
                    <label class="control-label col-sm-2" for="insurance_factor">Insurance Factor</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="insurance_factor" id="insurance_factor" placeholder="1.1, 2.1, 3.1" required>
                    </div>
            </div>



                  <div class="form-group">
                    <label class="control-label col-sm-2" for="walden_product_price">Price on walden</label>
                    <div class="col-sm-10">
                      <input type="text" class="form-control" name="walden_product_price" id="walden_product_price" placeholder="36000" required readonly>
                      <span style="color:red;">Walden price to be later calculated by walden as per the logistics</span>
                    </div>
                  </div>


                  <div class="form-group">
                    <label class="control-label col-sm-2" for="pro_url">Reference URL</label>
                    <div class="col-sm-10">
                      <input type="URL" class="form-control" name="pro_url" id="pro_url" placeholder="http://www.amazon.com" required>
                    </div>
                  </div>


                  <div class="form-group">
                    <label class="control-label col-sm-2" for="proposed_by"></label>
                    <div class="col-sm-10">
                    <?php echo '<input type="hidden" class="form-control" name="proposed_by" id="proposed_by" value="'.($_SESSION['user_id']).'">'; ?>
                    </div>
                  </div>

                  <div class="form-group">
                    <label class="control-label col-sm-2" for="proposed_by_user"></label>



                      <div class="user-box">        
                         <div class="form-group"><label class="control-label col-sm-2" for="invite-1">Member 1 email</label><div class="albox col-sm-10"><input type="email" class="form-control" name="invite[]" placeholder="sam@unclejohn.com" required=""></div></div>

                         <div class="form-group"><label class="control-label col-sm-2" for="invite-2">Member 2 email</label><div class="albox col-sm-10"><input type="email" class="form-control" name="invite[]" placeholder="sam@unclejohn.com" required=""></div></div> 

                      </div>              


                  <div class="form-group">        
                    <div class="col-sm-offset-2 col-sm-10">
                      <button type="submit" class="btn btn-success">Add product</button>
                    </div>
                  </div>
                </form>

在这种形式我有这两个字段,我想将这些值存储到php数组,字段可能会有3,5,6。

<div class="user-box">        
       <div class="form-group"><label class="control-label col-sm-2" for="invite-1">Member 1 email</label><div class="albox col-sm-10"><input type="email" class="form-control" name="invite[]" placeholder="sam@unclejohn.com" required=""></div></div>

        <div class="form-group"><label class="control-label col-sm-2" for="invite-2">Member 2 email</label><div class="albox col-sm-10"><input type="email" class="form-control" name="invite[]" placeholder="sam@unclejohn.com" required=""></div></div>              
</div>   

当我单击“提交”按钮时,应插入其他值,并将这些字段的值添加到数组中。  我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

请设置这样的字段名称

    <input type="email" class="form-control" name="invite[]" placeholder="sam@unclejohn.com" required="">

    <input type="email" class="form-control" name="invite[]" placeholder="sam@unclejohn.com" required="">

你可以在PHP中使用这样的值:

    foreach ( $_POST["invite"] as $invite ) {
        echo $invite;
    }

答案 1 :(得分:0)

在php中你可以阅读已发布的数组

$my_array=$_POST["invite"];
for($i=0;$i<count($my_array);$i++){
echo $my_array[$i];
//.. do what you want
}