0

Hello everyone. I want to build a special form. It has different kind of input types text, radio,select and combo boxes. Specility of my need is this.
I need to insert one element value as one record to database. Let's assume there are two textboxes and radio button group. It means three input types. So I want to input these values as 3 records.
I want to keep 3 input values as array. So I named input elements as array.

Now I will show my code of one textbox and radio button group below. So you could get idea easily later.

<div class="form-group">
                            <input type="hidden" value="10" name="attribute_ID[]">
                            <label for="Model Year">Model Year :</label>
                            <input type="number" class="form-control" name="attribute_value[]" required="true">
                            <div class="invalid-feedback" style="text-shadow:none;">Please type Model Year </div>
                        </div>

                              <div class="form-group">
                              <fieldset id="Condition">
                              <input type="hidden" value="4" name="attribute_ID[]">
                              <label for="Condition">Condition :</label>
                              <div class="form-check-inline">
                                        <div class="custom-control custom-radio">
                    <input type="radio" class="custom-control-input" id="New" name="attribute_value[2]" required value="New">

                    <label class="custom-control-label" for="New"> New</label>
                </div>

                                        <div class="custom-control custom-radio">
                    <input type="radio" class="custom-control-input" id="Reconditioned" name="attribute_value[2]" required value="Reconditioned">

                    <label class="custom-control-label" for="Reconditioned"> Reconditioned</label>
                </div>

                                        <div class="custom-control custom-radio">
                    <input type="radio" class="custom-control-input" id="Used" name="attribute_value[2]" required value="Used">

                    <label class="custom-control-label" for="Used"> Used</label>
                </div>

                              </div>
                              </fieldset>
                              </div>

My data table structure is like this,
REC_ID|ATTRIBUTE_ID|VALUE

As I mentioned above I used hidden type element to keep attribute ID value. Without any conflict I could keep attribute ID in hidden element as array. Later only without strugling I could access it using for each loop using PHP. So when I research on web I saw something like this, as example
<input type="text" name="array[attribute_name][attribute_value]" value="">
So I think for my problem it is the soulution. I want clear demonstartion/ description on name="array[attribute_name][attribute_value]". Furthermore I need to help with accessing those using php.
Currently I'm accessing using following php code

<?php
                        if(isset($_POST['finishmyad']))
                        {
                            if ($noe>0)
                            {

                            $cleanValues = [];
                            foreach( $_POST['attribute_value'] as $val )
                                $cleanValues[] = mysqli_real_escape_string( $con, $val );
                            //check
                            print_r($cleanValues);
                            echo '<br>';
                            }
                            else
                            {
                                echo '404 ! not Found';
                            }
                        }
                        ?>

I'm again mentioning I need help with array[][] kind of situations. Thank You Guys :)

2
Contributors
1
Reply
21
Views
3 Weeks
Discussion Span
Last Post by kinhkha1984
0

You try:

<div class="form-group">
    <input type="hidden" value="10" name="attribute[id][]">
    <label for="Model Year">Model Year :</label>
    <input type="number" class="form-control" name="attribute_model" required="true">
    <div class="invalid-feedback" style="text-shadow:none;">Please type Model Year </div>
</div>
<div class="form-group">
    <fieldset id="Condition">
        <input type="hidden" value="4" name="attribute[id][]">
        <label for="Condition">Condition :</label>
        <div class="form-check-inline">
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="New" name="attribute_value[]" required value="New">
                <label class="custom-control-label" for="New"> New</label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="Reconditioned" name="attribute_value[]" required value="Reconditioned">
                <label class="custom-control-label" for="Reconditioned"> Reconditioned</label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="Used" name="attribute_value[]" required value="Used">
                <label class="custom-control-label" for="Used"> Used</label>
            </div>
        </div>
    </fieldset>
</div>

OR:

<div class="form-group">
    <input type="hidden" value="10" name="attribute[id][]">
    <label for="Model Year">Model Year :</label>
    <input type="number" class="form-control" name="attribute[model]" required="true">
    <div class="invalid-feedback" style="text-shadow:none;">Please type Model Year </div>
</div>
<div class="form-group">
    <fieldset id="Condition">
        <input type="hidden" value="4" name="attribute[id][]">
        <label for="Condition">Condition :</label>
        <div class="form-check-inline">
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="New" name="attribute[value][]" required value="New">
                <label class="custom-control-label" for="New"> New</label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="Reconditioned" name="attribute[value][]" required value="Reconditioned">
                <label class="custom-control-label" for="Reconditioned"> Reconditioned</label>
            </div>
            <div class="custom-control custom-radio">
                <input type="radio" class="custom-control-input" id="Used" name="attribute[value][]" required value="Used">
                <label class="custom-control-label" for="Used"> Used</label>
            </div>
        </div>
    </fieldset>
</div>

GET POST:

echo '<pre>';print_r($_POST);

Result:

Array
(
    [attribute] => Array
        (
            [id] => Array
                (
                    [0] => 10
                    [1] => 4
                )

            [model] => 1
            [value] => Array
                (
                    [0] => New
                )

        )

)

Hope to be helpful to you.

Edited by happygeek: spam removed

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.