0

我创建了一个表单,其中包含测试代码、测试名称、测试参数是文本框和仪器代码是下拉列表并且还包含单击添加另一个按钮为新测试参数添加另一个按钮文本框和新仪器的下拉列表将生成代码我想保存新生成的测试参数文本框数据和新生成的单个测试代码的仪器代码下拉列表数据的数据..

我已经创建但它无法保存动态生成的新的多个文本框数据和生成的仪器代码下拉列表数据请指导我。这是我的PHP代码:

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

2 回答 2

0

1)您在自动生成的下拉列表中错过了名称属性,因此如果没有名称属性,您将无法将数据输入 php 脚本。

2)您新生成的文本框名称 = testparameters[] 哪个数组,因此您无法仅使用 $_POST['testparameters'] 获取值。要获得价值,您必须遍历数组。

foreach($_POST['testparameters'] as $value) {
 // here , you get dynamically added texbox values.
}
于 2012-09-22T13:33:19.143 回答
0

再次尝试循环,但仍然无法将值存储到无法计数下拉列表值&&测试参数文本框保存数组关键字我尝试的内容这是我的代码:

<?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>
于 2012-09-22T14:02:55.150 回答