1

I have a form which looks like this:

<form method='POST' name='form2' action='./index.php' class='form-horizontal'>
<input type='hidden' name='savedata' value='true'>

    <div class='row'>
        $numOfCols = 4;
        $rowCount = 0;
        $bootstrapColWidth = 12 / $numOfCols;
        $i=1;
        for ($x=1;$x<=8;$x++) {

            <div class='col-md-".$bootstrapColWidth ." col-xs-12 margin-bottom-30'>
                <strong><p>".$i.". Data</p></strong>
                <div class='col-sm-12'>
                    <div class='form-group' id='form_firstname_".$i."'>
                        <label for='name'>Firstname:<span class='red'>*</span></label>
                        <input class='form-control' id='firstname_".$i."' name='firstname_".$i."' type='text'>
                    </div>
                </div>

                <div class='col-sm-12'>
                    <div class='form-group'>
                        <label for='name'>Lastname:<span class='red'>*</span></label>
                        <input class='form-control' id='lastname_".$i."' name='lastname_".$i."' type='text'>
                    </div>
                </div>


                <div class='col-sm-12'>";
                    <div class='form-group' id='form_praefix_".$i."'>
                        <label for='name'>Field A:<span class='red'>*</span></label>
                        <input class='form-control' id='field_a_".$i."' name='field_a_".$i."' type='text'>
                    </div>
                </div>

                <div class='col-sm-12' id='form_postfix_".$i."'>
                    <div class='form-group'>";
                        <label for='name'>Field B:<span class='red'>*</span></label>
                        <input class='form-control' id='field_b_".$i."' name='field_b_".$i."' type='text'>
                    </div>
                </div>
            </div>

            $rowCount++;
            $i++;
            if($rowCount % $numOfCols == 0) {

            </div> <div class='row'>
            }

        }

    </div>

<input type='submit' id='btn_submit' class='btn btn-primary' value='Save data'>
<form>

So as you can see I have a for-loop which shows 8x times the same fields. It is up to the user if he enters only one person or up to eight persons.

The form will be submitted this way:

if ($savedata=="true") {

    /* create db connection */
    $link = mysqli_connect("localhost", "USER", "PASSWORD", "DATABASE");

    // check connection
    if($link === false){
        $error_db_connection=1;
    }

    // insert entries into database
    for ($run=1;$run<=8;$run++) {

        $var_firstname="firstname_".$run;
        $var_lastname="lastname_".$run;
        $var_field_a="field_a_".$run;
        $var_field_b="field_b_".$run;

        $sql = "INSERT INTO TABLENAME (firstname, lastname, praefix_title, postfix_title) VALUES ('".$$var_firstname."', '".$$var_lastname."', '".$$var_field_a."', '".$$var_field_b."')";
        if(mysqli_query($link, $sql)){
            $success=1;
        } else{
            $error=1;
        }
    }

    // close connection
    mysqli_close($link); 

}

I am using again a for-loop where the total number of loops is hardcoded right now (8). What I need to achive is, that the hardcoded total number of loops is dynamic. So if a user fills out 3x the fields (firstname, lastname, field_a, field_b) than only 3 records should be inserted into my database. If a user fills out all 8 fields, than 8 records should be inserted into my database.

Right now I always get 8 entries into my database, also if a user only fills out one entry. Can you help me how am I able to achive what I need?

Every form field has his own unique name and id because of the $i that I am counting. Maybe I need to send a hidden field with the total number of filled fields? If so, how do I do that? Or any other approach?

2
  • Names should not be unique but arrays. Like field_a[]. Then with a foreach you will loop just as many times as field filled by the user Commented Feb 17, 2019 at 10:33
  • thank you. I understand what you mean but can you tell me, what the foreach loop should look like once I changed all my name fields to []? Commented Feb 17, 2019 at 10:50

1 Answer 1

1

Before the sql insert statement, insert something like

if ( empty( $$var_firstname ) ) {
    break;
}

This checks if the value is set, and if not it quits out of the for loop. You may wish to do more validation checks like checking first name, last name, a and b to make sure they are all empty.

Sign up to request clarification or add additional context in comments.

Comments

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.