简体   繁体   中英

save dynamically generated textbox & dropdown list data for one testcode into database

I have create one form which contains Test code,Test name,Test parameter are Text box & Instrument code is drop down list & also contain add another button on click of add another button text box for new Test parameter & drop down list for new Instrument code will be generated i want to save data of that newly generated Test parameter text box data & newly generated Instrument code drop down list data for single Test code..

I have create but it can not save dynamically generated new multiple text box data & generated Instrument code drop down list data please guide me. Here is my PHP code:

<?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);    

    return($oMysqli);
}
    include_once "dTestCreation.php";   
    if(isset($_POST['submit'])) 
    {

    $Testcode = $_POST['testcode'];
    $TestName = $_POST['testname'];
    $Testparameter = $_POST['testparameters'];
    $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);

    $InsertQuery1 = "INSERT INTO test_table(testcode,testparameters,instrument_code) VALUES('$Testcode','$Testparameter','$Instrumentcode')";    
    $oMysqli=getConnection();       //establish connection
    $oMysqli->query($InsertQuery1);
    }
?>
<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 name=\"instrument_name\"><option value='$tResult[$kkk]'>tResult[$kkk]</option></select>";*/
    var output1="<select id=\"instrument_code\"><?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="testparameters"></input>

<label for='Instrumentcode'>Instrumentcode</label>
<select name="instrument_code">
    <?php 
    $tResult = getIname();                      for($kkk=0;$kkk<count($tResult);$kkk++) //loop for incremenet code hereted value display in listbox.
{
    echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>";    
}`enter code here`
?>
    </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>

1) You missed name attribute into your automatically generated dropdown list so without name attribute you will not get data into php script.

2) Your newly generated text box name = testparameters[] which array so you can't get value just using $_POST['testparameters']. TO get value, you have to loop through array.

foreach($_POST['testparameters'] as $value) {
 // here , you get dynamically added texbox values.
}

Again try loop also but still value cant be stored into cant not count drop down list value & & testparameters textbox save Array keyword what i Try Here is My code:

<?php
    include_once "dTestCreation.php";   
    if(isset($_POST['submit'])) //when clikc on submit button
    {

    $Testcode = $_POST['testcode'];
    $TestName = $_POST['testname'];
    $Testparameter = $_POST['testparameters'];
    $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();

    $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];
            $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);

        }







    }   
?>
<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 name=\"instrument_name\"><option value='$tResult[$kkk]'>tResult[$kkk]</option></select>";*/
    var output1="<select id=\"instrument_code\" name=\"instrument_code\"><?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="testparameters"></input>

<label for='Instrumentcode'>Instrumentcode</label>
<select name="instrument_code">
                    <?php 
                        $tResult = getIname(); //call function getIname() included in file dTestCreation.php
                        for($kkk=0;$kkk<count($tResult);$kkk++) //loop for incremented value display in listbox.
                        {
                            echo "<option value=".$tResult[$kkk].">".$tResult[$kkk]."</option>";    //Instrument values display in listbox
                        }
                    ?>
    </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>

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