保存运行时创建下拉列表值到数据库中

时间:2012-09-24 12:08:21

标签: php javascript mysql html

我创建了一个表单,其中包括测试代码,测试名称,测试参数,仪器代码和  添加测试参数按钮,测试代码,测试名称,测试参数是文本框,仪器代码是下拉列表框,单击添加测试参数按钮新文本框和仪器代码下拉列表将生成,其运行时创建文本box值将被保存到数据库中但是Instrument Code值无法更改。对于所有文本新生成的文本框保持相同。我想保存选中的下拉列表项可以指导我吗 这是我的PHP代码:

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

1 个答案:

答案 0 :(得分:1)

如果您有不同的具有相同名称的表单元素(假设不使用数组),则操作页面的值为send,是具有该名称的最后一个表单元素的值。

解决方案1:

  • 点击添加新测试名称,在每个记录集的隐藏字段中写下所选下拉值。

解决方案2:

只需使用数组

<select name = "instrument_name[]" id="instrument_name">
//php code
</select>

只需在操作页面上执行print_r($_POST),看看每个表单元素都会有一个数组,从这里你可以得到值