0

Here i have a form with action to another page which inserts the values to database.i also have some fields in the same form which i want only those field value to be stored in php array

code:

    <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="[email protected]" 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="[email protected]" 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>

in this form i have these 2 fields where i want to store these values to php array, the fields may vary like 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="[email protected]" 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="[email protected]" required=""></div></div>              
</div>   

when i click submit button the other values should be inserted and values of these fields should be added to array. how can i do this?

2 Answers 2

2

Please set the field names like this

    <input type="email" class="form-control" name="invite[]" placeholder="[email protected]" required="">

    <input type="email" class="form-control" name="invite[]" placeholder="[email protected]" required="">

and you can have values in PHP like this:

    foreach ( $_POST["invite"] as $invite ) {
        echo $invite;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

... and then access the values via $_POST["invite"]
for ( $i = 0; $i < count($_POST["invite"]); $i++ ) { echo $_POST["invite"][$i]; }
0

In php you can read posted array

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

1 Comment

A "for each" loop would be a much cleaner approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.