-2

The first thing is that I created a select button using javascript, here is the code in :

var select = "<form action='' method='post'><select id='line' name='choice'>";

for (var j = 0; j < jsondata.length; j++) {
    select += '<option>' + jsondata[j]['productLine'] + '</option>'
};
select += "</select></form>";

document.getElementById("select").innerHTML = select; // put the button in html tag

Now I'm trying to get the value of the selected content each time, and then pass the value to the SQL in PHP, here are lines of codes of PHP

$p = $_POST['choice'];
echo $p;

$sql_p = "SELECT productCode,productName
FROM products
WHERE productLine = '".$p."'";

But I got the Undefined index: choice. And no value is received.

Could anyone help me with this?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Liu
  • 67
  • 1
  • 6

1 Answers1

1

Add a value to the option tag, so that the selected option value will be received in the $_POST.

var jsondata = [];
jsondata.push({"productLine":"Test"});
jsondata.push({"productLine":"Test1"});
var select = "<form action='' method='post'><select id='line' name='choice'>";
for (var j = 0; j < jsondata.length; j++) {
    select += '<option value="'+jsondata[j]['productLine'] +'">' + jsondata[j]['productLine'] + '</option>'
};
select += "</select></form>";
document.getElementById("select").innerHTML = select; // put the button in html tag
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
selesh20
  • 9
  • 1