duanhe8280 2017-01-13 15:18
浏览 101
已采纳

使用Php添加/删除表单字段

I have a form which gives the ability for the user to add additional fields form to the form and it selects data from the database for the select/options.

It works ok but not entirely correct and am wondering if somebody wouldn't mind casting an eye on the code to see if can be done in a much cleaner way.

The main issue being that the select isn't sending the correct value across to the action script.

HTML output of the form:

<?php $dashboardId = $_GET['dashboard_id']; ?>

<form action="cm.php" method="POST">
    <input type="hidden" name="dashboardId" value="<?php echo $dashboardId; ?>">


    <div id="exercises">
        <div class="team">
            <select name="teamId[]">
            <?php
            $sql = "SELECT * FROM teams WHERE dashboard_id = $dashboardId";
            $result = $conn->query($sql);
                if($result->num_rows > 0){
                    while($row = $result->fetch_assoc()){
                        echo '<option value="' . $row["team_id"] . '">' . $row["team_name"] . '</option>';
                    }
                }
            ?>                
            </select>
            <button class="remove">x</button>
        </div>
    </div>
    <button id="add_exercise">add more</button>
    <br>  
    <input type="text" name="memberName">
    <br>
    <input type="submit" name="submit" value="Create Member" />
</form>

So the above renders out my simple form. The second part the JQuery that handles the facility to add additional select fields.

<script type="text/javascript">
$('#add_exercise').on('click', function() { 
    $('#exercises').append('<div class="team"><select name="teamName[]"><?php
            $sql = "SELECT * FROM teams WHERE dashboard_id = $dashboardId";
            $result = $conn->query($sql);
                if($result->num_rows > 0){
                    while($row = $result->fetch_assoc()){
                        echo '<option value="' . $row["team_id"] . '">' . $row["team_name"] . '</option>';
                    }
                }
            ?>   </select><button class="remove">x</button></div>');
    return false; //prevent form submission
});

$('#exercises').on('click', '.remove', function() {
    $(this).parent().remove();
    return false; //prevent form submission
}); 
</script>

Now as can be seen it isn't the neatest of solutions combining the jQuery with the Php however I am not sure how else I would separate it out? So what is happening is when I do a var_dump($_POST) I see that the generated select passes ["teamName"]=> array(1) { [0]=> string(3) "211" where it should be passing ["teamId"]=> array(1) { [0]=> string(3) "211"

I am fully aware it is open to SQL injection but for now I am just trying to make this little part work.

update - team table scehema

enter image description here

  • 写回答

1条回答 默认 最新

  • dongmeixian9665 2017-01-13 15:50
    关注

    Main.php File

    <!DOCTYPE html>
    <html>
      <head>
    <meta charset="utf-8">
    <title></title>
     </head>
     <body>
    <div class="wrapper">
      <div>
      <select class="options" name="">
        <option value="1">item_1</option>
        <option value="1">item_2</option>
        <option value="1">item_3</option>
      </select>
    </div>
    </div>
    <button type="button" class="addme" name="button">Add More</button>
     </body>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js">   </script>
     <script type="text/javascript">
    $(document).ready(function(){
      $('.addme').click(function(){
        $.ajax({
          url:'getData.php',
          type:'GET',
          success:function(result){
              console.log(result);
              $('.wrapper').append(result);
          }
        })
      });
    });
    

    getData.php File

    <select>
    <option value='1'>Item1</option>
    <option value='2'>Item2</option>
    <option value='3'>Item3</option>
    </select><br>
    

    In this example getData file data was static but you have write query to get dropdown list data and pass in success response.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗