I want to create a dynamic form to create users, with a "Add user" button that creates new inputs for another user.
$('#add-user').click(function() {
$(this).before(
'<input type="text" placeholder="Username">' +
'<input type="text" placeholder="Email">' +
'<br>'
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input type="text" placeholder="Username">
<input type="text" placeholder="Email">
<br>
<button type="button" id="add-user">Add user</button>
<button type="submit">Create users</button>
</form>
I want the form data structured this way server-side (I am using PHP with Laravel):
$users = [
[
'username' => 'toto',
'email' => '[email protected]',
],
[
'username' => 'titi',
'email' => '[email protected]',
],
[
'username' => 'tata',
'email' => '[email protected]',
]
]
I can't work out how I should name my inputs in order to do that. I know I must use the array names like this name="user[]", but I am stuck.
nameattribute of my inputs?