0

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>

2
  • save you mysql results in a session with php Commented Apr 11, 2018 at 3:58
  • would you please give me an example? Commented Apr 11, 2018 at 4:00

1 Answer 1

1

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

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

4 Comments

im sorry, literally the problem is inside <script>...</script>
do the same thing the php is getting the results set them to a session.
other than that with javascript you will have to move them in the URL.
to be clear, my problem is the code is not work when i put in php inside <script></script>

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.