Bagaimana cara insert into database form dinamis autofill ajax ?

Script formnya

<body>
    <div class="container">
        
    <table class="table table-bordered">
    <thead>
            <tr>
                <th>Username</th>
                <th>Name</th>
                <th>Age</th>
                <th>Email</th>
                <th>Salary</th>
            </tr>
            </thead>
            <form action="insert.php" method="post">
            <tbody>
            <td><button type="submit" class="btn btn-primary" name = 'submit' >Submit</button></td>
    </form>
            <tr class='tr_input' >
                
                <td ><input type='text' class='form-control username ' name="username[]" id='username_1' placeholder='Enter username'></td>
                <td><input type='text' class='name form-control' name="name[]" id='name_1' ></td>
                <td><input type='text' class='age form-control' name="age[]" id='age_1' ></td>
                <td><input type='text' class='email form-control' name="email[]" id='email_1' ></td>
                <td><input type='text' class='salary  form-control' name="salary[]"id='salary_1' ></td>
                
<?php
echo "<pre/>";print_r($_POST);
?>
            </tr>
          
  </body>
</html>
            </tbody>
        </table>
        <br>
        <input type='button' value='Add more' id='addmore'>
    </div>
    <div>
</body>
</html>

script ajax

    <script type="text/javascript">
        $(document).ready(function(){

            $(document).on('keydown', '.username', function() {
                
                var id = this.id;
                var splitid = id.split('_');
                var index = splitid[1];

                $( '#'+id ).autocomplete({
                    source: function( request, response ) {
                        $.ajax({
                            url: "ajaxfile.php",
                            type: 'post',
                            dataType: "json",
                            data: {
                                search: request.term,request:1
                            },
                            success: function( data ) {
                                response( data );
                            }
                        });
                    },
                    select: function (event, ui) {
                        $(this).val(ui.item.label); // display the selected text
                        var userid = ui.item.value; // selected id to input

                        // AJAX
                        $.ajax({
                            url: 'ajaxfile.php',
                            type: 'post',
                            data: {userid:userid,request:2},
                            dataType: 'json',
                            success:function(response){
                                
                                var len = response.length;

                                if(len > 0){
                                    var id = response[0]['id'];
                                    var name = response[0]['name'];
                                    var email = response[0]['email'];
                                    var age = response[0]['age'];
                                    var salary = response[0]['salary'];

                                    document.getElementById('name_'+index).value = name;
                                    document.getElementById('age_'+index).value = age;
                                    document.getElementById('email_'+index).value = email;
                                    document.getElementById('salary_'+index).value = salary;
                                    
                                }
                                
                            }
                        });

                        return false;
                    }
                });
            });
            
            // Add more
            $('#addmore').click(function(){

                // Get last id 
                var lastname_id = $('.tr_input input[type=text]:nth-child(1)').last().attr('id');
                var split_id = lastname_id.split('_');

                // New index
                var index = Number(split_id[1]) + 1;

                // Create row with input elements
                var html = "<tr class='tr_input'><td><input type='text' class='form-control username' id='username_"+index+"' placeholder='Enter username'></td><td><input type='text' class='form-control name' id='name_"+index+"' ></td><td><input type='text' class='form-control age' id='age_"+index+"' ></td><td><input type='text' class='form-control email' id='email_"+index+"' ></td><td><input type='text' class='form-control salary' id='salary_"+index+"' ></td> </tr>";
                +'<td><a class="btn btn-danger btn-sm remove_button"><i class="fa fa-trash" aria-hidden="true"></i></a></td>'
                // Append data
                $('tbody').append(html);
                
                
            });
            $(document).on('click', '.remove_button', function(){
     $(this).parents('tr').remove();
});
 });

    </script>

script into database

<?php
 
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "latihan2"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
}
 
if(isset( $_POST['submit'] ) )
{
  
 
$username  = $_POST['username'];
$name= $_POST['name'];
$email= $_POST['email'];
$age= $_POST['age'];
$salary= $_POST['salary'];

$rowcount= sizeof('username');
for($n=0;$n<$rowcount;$n++){
  $sql=$con->query("INSERT INTO test (username,name,email,age,salary) VALUES($username[$n],$name[$n],$email[$n],$age[$n],$salary[$n])");
}
if($sql){
//  echo "berhasil";
}else{
  echo"gagal";
  die($con->error);
}
}
?>
avatar muhammadaldiwijaya
@muhammadaldiwijaya

2 Kontribusi 0 Poin

Dipost 9 bulan yang lalu

Tanggapan

bisa dijelaskan masalah yang kamu alami apa. Errornya apa. Yang dimau apa. (SIlahlkan diedit ditambahkan informasi ini)

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban