0

I have page a with 2 forms

the first part of the form

<form id="A" role="form" enctype="multipart/form-data" method="post">
<input class="form-control" placeholder="Name of the Company" name="ven_name" type="text" autofocus >
<input class="form-control" placeholder="Address" name="address" type="text" autofocus >
<input class="form-control" placeholder="City" name="city" type="text" autofocus >
</form>


<form id="B" role="form" enctype="multipart/form-data" method="post">
<input type="text" name="usr_f_name[]" placeholder="First Name" />
<input type="text" name="usr_l_name[]" placeholder="Last Name" />
<input type="text" name="usr_mobile[]" placeholder="Mobile Number" />
<input type="text" name="usr_email[]" placeholder="Email" />
<input type="password" name="usr_password_1[]" placeholder="Password"/>    
</form>

both of them will be saved on a Database. Form A will be saved on Table A and Form B will be save on Table B. For Form B i'm using a Jquery so User can add multiple Users at a time ( maximum 3 user in one go )

here is the Php for Form B

    $usr_array = array_keys($_POST['']);
    foreach ($id_array as $id) {
        $usr_f_name =  mysqli_real_escape_string ($_POST['usr_f_name'][$id]);
        $usr_l_name =  mysqli_real_escape_string ($_POST['usr_l_name'][$id]);
        $usr_mobile =  mysqli_real_escape_string ($_POST['usr_mobile'][$id]);
        $usr_email =  mysqli_real_escape_string ($_POST['usr_email'][$id]);
        $usr_password_1 =  md5 ($_POST['usr_password_1'][$id]);



$sql = "INSERT INTO table_b SET f_name = '$usr_f_name', l_name = '$usr_l_name', mobile  ='$usr_mobile',username='$usr_email', email='$usr_email', password='$usr_password_1'";
        }
        if(mysqli_query($databaseLink, $sql)){
            echo "Records added successfully.";
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($databaseLink);
        }
    }

how do I tell php which form to iterate on?

6
  • how do i tell which form to iterate on. Commented Oct 11, 2017 at 10:30
  • Question is unclear to me. And, What problem are you facing? Commented Oct 11, 2017 at 10:30
  • Where are your <form> tags? From the code you've shown (which, don't forget, is the only thing we know about your program) there are, technically, no forms at all. You can only submit 1 form per postback. (Where "form" is defined as everything between a <form> and </form> pair of tags). Commented Oct 11, 2017 at 10:39
  • Ok thanks for editing. Like I said you can only submit one form per postback. If you're happy with that, then you want to know which form was submitted, am I right? In that case simply look for the existence of the fields you know belong to each form. Or, maybe a better separation of concerns would be to make the forms post back to different URLs and be processed separately. Commented Oct 11, 2017 at 10:45
  • 1
    @TheNagaTanker like I said, that's one way to do it. kerbholz's answer demonstrates another way. Alternatively if you need all the information to be submitted in one dataset, then you need to combine the 2 forms into 1. You didn't entirely make the requirement clear so it's hard to give a definitive answer. Commented Oct 11, 2017 at 12:06

2 Answers 2

2

Add a hidden input field to both forms.

<form role="form" enctype="multipart/form-data" method="post">
<input type="hidden" name="form" value="A" />
...
</form>
<form role="form" enctype="multipart/form-data" method="post">
<input type="hidden" name="form" value="B" />
...
</form>

In your script you can check the field's value:

<?php
// ... sanitize ...
if ( $_POST['form'] == 'A' ) {
  // Form A has been submitted
} else if ( $_POST['form'] == 'B' ) {
  // Form B has been submitted
}
Sign up to request clarification or add additional context in comments.

Comments

1

Change naming of your fields:

<div class="form-B"
  <input class="form-control" placeholder="Name of the Company" name="formA[ven_name]" type="text" autofocus >
  <input class="form-control" placeholder="Address" name="formA[address]" type="text" autofocus >
  <input class="form-control" placeholder="City" name="formA[city]" type="text" autofocus >
</div>


<div class="form-B">
  <input type="text" name="formB[0][usr_f_name]" placeholder="First Name" />
  <input type="text" name="formB[0][usr_l_name]" placeholder="Last Name" />
  <input type="text" name="formB[0][usr_mobile]" placeholder="Mobile Number" />
  <input type="text" name="formB[0][usr_email]" placeholder="Email" />
  <input type="password" name="formB[0][usr_password_1]" placeholder="Password"/>    
</div>

That way your $_POST will look like this:

[
    'formA' => [
        'ven_name' => '',
        'address' => '',
        'city' => '',
    ],
    'formB' => [
        [
            'usr_f_name' => '',
            /* ... */
        ],
        [
            'usr_f_name' => '',
            /* ... */
        ]
        /*...*/
    ]
]

And you can do:

$query = 'INSERT INTO formB (usr_f_name, ...) VALUES ';
$inserts = [];
$params = [];

foreach ($formData['formB'] as $fields) {
    /* ... */
}

4 Comments

that looks promising, ill try i and let you know. but what will happen to <input type="text" name="formB[usr_f_name][0]" placeholder="First Name" /> if a user decides to add multiple user in one go ???
@TheNagaTanker I think in that case you should create the elements for the new user as <input type="text" name="formB[usr_f_name][1]" placeholder="First Name" /> changing the 0 for a 1. And then 2 for the next one, if they add another user.
@TheNagaTanker Check update, instead of [0] at the end it should be after formB to wrap all user field to one key. If user adds another set of fields you have to replace formB[0] with formB[1] and so on.
@TheNagaTanker In PHP you can use foreach ()

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.