1

我创建了一个表格,其中包括测试代码、测试名称、测试参数、仪器代码和添加测试参数按钮,测试代码、测试名称、测试参数是文本框,仪器代码是下拉列表框,单击添加测试参数按钮新文本框和仪器代码下拉列表将生成,其运行时创建的文本框值将保存到数据库中,但仪器代码值不能更改。对于所有新生成的文本框保持相同。我要保存选择下拉列表项你能指导我这是我的 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>
4

1 回答 1

1

当您有同名的不同表单元素时(假设不使用数组),发送到操作页面的值是最后一个具有该名称的表单元素的值。

解决方案1:

  • 单击添加新测试名称时,将选定下拉列表的值写入每个记录集的隐藏字段中。

解决方案2:

只需使用数组

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

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

于 2012-09-24T12:23:05.837 回答