Reputation: 39
I have create one form which include Test Code,Test Name,Test Parameter,Instrument Code & Add Test Parameter button,Test Code,Test Name,Test Parameter are text box,Instrument Code is drop down list box,on click of Add Test Parameter button new text box and Instrument Code drop down list would be generated,its run time created text box value would be save into Database but Instrument Code value can not be change.it remain same for all text newly generated text box.I want save selected drop down list item can you guide me Here is My PHP code:
<?PHP
include_once "dTestCreation.php";
global $Hostname;
global $Username;
global $Password;
global $Database_name;
function getConnection()
{
$Hostname = "localhost";
$Username ="root";
$Password ="";
$Database_name="labdata";
$oMysqli = new mysqli($Hostname,$Username,$Password,$Database_name); //create connection object.
return($oMysqli); //return connection object.
}
if(isset($_POST['submit'])) //when click on submit button
{
$Testcode = $_POST['testcode'];
$TestName = $_POST['testname'];
$TestParameter = $_POST['testparameter'];
$Instrumentcode = $_POST['instrument_code'];
$InsertQuery = "INSERT INTO demo_test(testcode,testname) VALUES('$Testcode','$TestName')"; //query for insert data into table
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery);
//print_r($InsertQuery);exit();
$InsertQuery1 = "INSERT INTO test_table(testcode,testparameters,instrument_code) VALUES('$Testcode','$TestParameter','$Instrumentcode')"; //query for insert data into table
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery1);
$length = count($_POST['testparameters']);
for($i=0;$i<$length;$i++)
{
//echo $_POST['testparameters'][$i];
$a = $_POST['testparameters'][$i];
$na = array('testparameters' => $a['testparameters']);
foreach($na as $k => $v)
{
$na[$k] = mysql_real_escape_string($v);
}
$TestParameters = $na['testparameters'];
$TestParameters = $_POST['testparameters'][$i];
$InsertQuery2 = "INSERT INTO test_table(testcode,testparameters,instrument_code) VALUES('$Testcode','$TestParameters','$Instrumentcode')"; //query for insert data into table
$oMysqli=getConnection(); //establish connection
$oMysqli->query($InsertQuery2);
}
}
?>
<html>
<head>
<title>TestData</title>
<script type="text/javascript">
function create_row() //create function create_row
{
var newtr=document.createElement("tr"); //variable for tr
var newtd=document.createElement("td"); //variable for td
var newtd1=document.createElement("td"); //variable f
var output="<input type=\"text\" name=\"testparameters[]\">";
var output1="<select id=\"instrument_name\" name=\"instrument_name\"><?php include_once "dTestCreation.php"; //include file dTestCreation.php
$tResult = getIname();
for($kkk=0;$kkk<count($tResult);$kkk++)
{
echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>";
}//display values of instrument into list.
?></select>";
newtd.innerHTML=output; //display first td
newtd1.innerHTML=output1; //display second td
newtr.appendChild(newtd); //increment no of rows.
newtr.appendChild(newtd1);
document.getElementById("table1body").appendChild(newtr);
}
</script>
</head>
<body>
<form name="testdetails" method="post" target="_self" action="<?php $_PHP_SELF ?>">
<label for='Testcode'>Testcode</label>
<input type="text" name="testcode"></input>
<label for='TestName'>TestName</label>
<input type="text" name="testname"></input><br></br>
<label for='Testparameter'>Testparameter</label>
<input type="text" name="testparameter"></input>
<label for='Instrumentcode'>Instrumentcode</label>
<select name="instrument_name" id="instrument_name">
<?php
$tResult = getIname();
for($kkk=0;$kkk<count($tResult);$kkk++)
{
echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>";
}
?>
</select>
<table>
<tbody id="table1body">
<tr>
<!--<td><input type="textfield" name="testparameters"></td>-->
<td> <input type="button" name="button" value="Add Test Parameter" onclick="create_row()"> <!--call function create_row() for onclik add new row-->
</tr>
</tbody>
</table>
<input type="submit" name="submit" value="submit"></input>
</form>
</body>
</html>
Upvotes: 1
Views: 1003
Reputation: 3488
When you have different form elements with same name (suppose not using arrays), the value to the action page is send, is the value of the last form element with that name.
Just use array
<select name = "instrument_name[]" id="instrument_name">
//php code
</select>
just do print_r($_POST)
on action page and see, there will be an array for each form element, from here you can get the values
Upvotes: 1