简体   繁体   中英

insert data from database using php in javascript code

I really need inspiration how to make multiple insert using this code below. The problem is, the data from database not display in the next form. Is there anybody can help me here to give me some advice to solve my problem? i really appreciate to all anwer relate my problem. And here's the code : here the detail

 <html> <head> <title>Multiple Insert</title> <!-- Load plugin jquery nya --> <script src="bower_components/jquery/dist/jquery.min.js"></script> </head> <body> <form method="post" action="proses.php"> <!-- Buat tombol untuk menabah form data --> <button type="button" id="btn-tambah-form">Tambah Data Form</button> <button type="button" id="btn-reset-form">Reset Form</button><br><br> <b>Data ke 1 :</b> <table> <tr> <td>Item</td> <td> <select name='barang[]' class='form-control'> <?php include "include/koneksi.php"; $sql = "select * from tbl_mst_barang"; $query = mysqli_query($conn, $sql); while($data=mysqli_fetch_array($query)) { echo "<option value=$data[id_barang]>$data[nama_barang]</option>"; } ?> </select> </td> </tr> <tr> <td>Qty</td> <td><input type="text" class="form-control" name="qty[]" required></td> </tr> </table> <br><br> <div id="insert-form"></div> <hr> <input type="submit" value="Simpan"> </form> <!-- Kita buat textbox untuk menampung jumlah data form --> <input type="hidden" id="jumlah-form" value="1"> <script> $(document).ready(function(){ // Ketika halaman sudah diload dan siap $("#btn-tambah-form").click(function(){ // Ketika tombol Tambah Data Form di klik var jumlah = parseInt($("#jumlah-form").val()); // Ambil jumlah data form pada textbox jumlah-form var nextform = jumlah + 1; // Tambah 1 untuk jumlah form nya <?php include 'include/koneksi.php'; $sql1 = 'select * from tbl_mst_barang'; $query1 = mysqli_query($conn, $sql1); while ($data1=mysqli_fetch_array($query1)) //echo '<option value=$data[id_barang]>$data[nama_barang]</option>'; ?> // Kita akan menambahkan form dengan menggunakan append // pada sebuah tag div yg kita beri id insert-form $("#insert-form").append("<b>Data ke " + nextform + " :</b>" + "<table>" + "<tr>" + "<td>Item</td>" + "<td><select name='barang[]' class='form-control'><option value='<?php echo $data1['id_barang'];?>'><?php echo $data1['nama_barang']; ?></option></select></td>" + "</tr>" + "<tr>" + "<td>Qty</td>" + "<td><input type='text' class='form-control' name='qty[]' required></td>" + "</tr>" + "</table>" + "<br><br>"); $("#jumlah-form").val(nextform); // Ubah value textbox jumlah-form dengan variabel nextform }); // Buat fungsi untuk mereset form ke semula $("#btn-reset-form").click(function(){ $("#insert-form").html(""); // Kita kosongkan isi dari div insert-form $("#jumlah-form").val("1"); // Ubah kembali value jumlah form menjadi 1 }); }); </script> </body> </html> 

Here is an example of how to set a session from your results. Now your session will carry over to the next page. place Session start at the beginning of your page to begin a session.

    <?php
    session_start();

Then you set the session like this

    while($data=mysqli_fetch_array($query)){
    $_SESSION['id_barang'] = $data[id_barang];
    $_SESSION['nama_barang'] = $data[nama_barang];                  
    echo "<option value=\"".$_SESSION['id_barang']>$_SESSION['nama_barang']."></option>";
    }

you can then call them like this to check them

   echo $_SESSION['id_barang'];
   echo $_SESSION['nama_barang'];

This is the link with more info on how sessions work. http://php.net/manual/en/book.session.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM