1

<script type="text/javascript">
var rowNum = 0;
function addRow(frm) {
	rowNum ++;
	var row = '<p id="rowNum'+rowNum+'"> Barang: ';
	row += '<select name="???">';
	row += '<option value="A1">A1</option>';
	row += '<option value="A2">A2</option>';
	row += '<option value="A3">A3</option>';
	row += '<option value="A4">A4</option>';
	row += '</select>';
	row += ' Satuan: <input type="text" size="5" name="satuan[]" value="'+frm.add_satuan.value+'"> Quantity: <input type="text" name="qty[]" value="'+frm.add_qty.value+'"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"><hr color=red></p>';

	$('#itemRows').append($(row));
	 frm.add_qty.value = '';
	 frm.add_nama.value = '';
         frm.add_satuan.value = '';
};
  </script>

How to make drop-down list but with query mysql to show A1, A2, dll. When submit button for drop-down can't post data anything. Can I store data with this javascript. For text input success post data.

1
  • Hi, i make dinamic row for insert data. But for Barang must from database query. Can u help for show in javascript? Commented Jan 19, 2016 at 7:40

2 Answers 2

1

DO NOT DO WHAT YOU ARE TRYING TO DO. You should never, ever, ever write queries on the front-end. You should do your absolute best to hide every detail of the server/database from the user. It is a massive security risk. Please read about SQL injection attacks for starters.

How you should do this:

Store the values of the dropsdowns in JavaScript. Let's keep them in a single object to make life easy:

Your JS:

var options = {
  A1: $("#rowNum select option[value='A1']").text(),
  A2: $("#rowNum select option[value='A2']").text(),
  A3: $("#rowNum select option[value='A3']").text(),
  A4: $("#rowNum select option[value='A4']").text()
};

// Now, send this object to your PHP via an AJAX call. Let's assume for simplicity that you will do this using jQuery:
$.ajax({
  url: 'my/php/script.php',
  data: options,
  success: function (data) { console.log('Yay, it worked!'); },
  error: function (jqXHR, textStatus, error) { console.log('crap it didn't work', jqXHR, textStatus, error); }
});

Your PHP

<?php

$options = $_REQUEST['options']
// You need to verify the options are valid (and don't have bad values) but that's a different question

// Build your query here. Your PHP is run on the server only so no one else will see it or be able to change it.
Sign up to request clarification or add additional context in comments.

4 Comments

I still dont understand with new suggest and new technical. Whats key for I learn for use your suggest ? :) Thanks
@user2400395 You need to go learn about these things (all easily googleable): 1) how to make an AJAX request using jQuery. 2) how to run MySQL queries from PHP
thanks Matthew Herbst
@user2400395 welcome. Please feel free to come back to SO and search for/ask other questions about what you are learning if needed.
0

You can try this code with some modification. Use jQuery's each to iterate over the array value.

$('#emptyDropdown').empty();
// Parse the returned json data
//var opts = $.parseJSON(data);//Remove comment if you are using JSON

// Use jQuery's each to iterate over the opts value
$.each(opts, function(i, d) {
    // You will need to alter the below to get the right values from your data.
    $('#emptyDropdown').append('<option value="' + d.yourID + '">' + d.yourValue + '</option>');
});

1 Comment

Where I paste your code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.