douchen2025 2017-02-06 09:54
浏览 119
已采纳

如何使用php获取动态生成的textarea输入的值

I'm having a challenge with a mini project I'm working on and have googled for a solution for hours without a headway. I have a column in my database table whose contents are generated by exploding a previous form inputs(separated by comma in the table). Now I need to get inputs (from users) for these values using textarea in a form. These inputs will be in arrays depending on the number of contents fetched from the db in the first place and then stored in another column in my table. The issue here is that each time I submit the form I get an undefined index notice for the name of the values in the textarea field, which is test-col []. please see my code below:

<?php
    $conn = mysqli_connect('localhost', 'root', '', 'myDb');
    $users_id = mysqli_real_escape_string($conn, $_GET['id']);
    $res = mysqli_query($conn, "SELECT tests FROM bal WHERE users_id = '$users_id'");
    if ($res){
        while ($row = mysqli_fetch_array($res)){
            $tests = explode(',', $row['tests']);   
            foreach($tests as $test){
                if ($test ==""){
                    continue;
                }
                echo '<div class="test-res" style="margin-top:10px;">
                          <form action="" method="post" role="form" class="form-horizontal">
                              <div class="form-group">
                                  <label for= "test-col" class="form-label col-md-2">'.$test.' test</label>
                                  <div class="col-md-10">
                                      <textarea class="form-control" rows="3" name="test-col[]" placeholder="Test result"> </textarea>
                                  </div>
                              </div>
                          </form>
                      </div>';
                '<br /> <br />';
            }
        }
    }
    echo '<form action="" method="post">
              <button type="submit" class="btn btn-success col-md-offset-5" name="sub-res">Send Result</button>
          </form>';
?>

//to insert textarea values in db
<?php
    if(isset($_POST['sub-res'])){
        $conn = mysqli_connect('localhost', 'root', '', 'myDb');
        foreach ($_POST ['test-col'] as $values){
            $test_results = implode("<br>", $values);
        }
        $ins = mysqli_query($conn, "INSERT INTO bal (results) VALUES
 ('$test_results') WHERE users_id = '$users_id'");
        if (!$ins){
            die(mysqli_error());
        }
        else{
            echo '<div class="alert alert-success">Successfully sent</div>';
        }
    }
?>
  • 写回答

1条回答 默认 最新

  • doushen9863 2017-02-06 10:04
    关注

    You have two forms - one in your while/foreach statement, and then one below. If you're submitting the second form, it won't contain values from the first.

    Wrap the while in the form instead;

    <form action="" method="post">
    <?php       
    $conn = mysqli_connect('localhost', 'root', '', 'myDb');
    $users_id = mysqli_real_escape_string($conn, $_GET['id']);  
    $res = mysqli_query($conn, "SELECT tests FROM bal WHERE users_id =
     '$users_id'");
    if ($res){  
        while ($row = mysqli_fetch_array($res)){    
            $tests = explode(',', $row['tests']);       
            foreach($tests as $test){           
                if ($test =="") {
                    continue;   
                }       
                echo '<div class="test-res" style="margin-top:10px;"><div class="form-group">
                  <label for= "test-col" class="form-label col-md-2">'.$test.' test</label><div class="col-md-10"><textarea class="form-control" rows="3" name="test-col[]" placeholder="Test result"> </textarea></div></div></div>';     
               '<br /> <br />' ;    
            } 
        } 
    }
    ?>
        <button type="submit" class="btn btn-success col-md-offset-5" name="sub-res">Send Result</button>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决