如何通过输入字段数组编辑成分


How can i edit my ingredients through array of input fields

我如何制作一个类似于发布成分的表格,但我只能编辑发布的成分

这是按钮的Javascript

<script>
$(document).ready(function() {
  var max_fields = 25; //maximum input boxes allowed
  var wrapper = $(".input_fields_wrap"); //Fields wrapper
  var add_button = $(".add_field_button"); //Add button ID
  var x = 1; //initlal text box count
  $(add_button).click(function(e) { //on add input button click
    e.preventDefault();
    if (x < max_fields) { //max input box allowed
      x++; //text box increment
      $(wrapper).append('<div class="col-md-6 div-' + x + '"><input                     type="text" class="form-control" name="mytext[]" required/></div>'); //add input box
      $(wrapper).append('<div class="col-md-6 div-' + x + '"><input type="text" class="form-control" name="mytext2[]" required/><a href="#" class="remove_field" div-id="' + x + '">Remove</a></div>');
    }
  });
  $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
    e.preventDefault();
    $(".div-" + $(this).attr("div-id")).remove();
    x--;
  })
});
</script>

这是插入成分的形式

<div class="input_fields_wrap">
  <div class="col-md-6">
    <h5>Notes/Scaling (quantity, additional info)</h5>
    <input type="text" class="form-control" name="mytext2[]" required>
    <br>
  </div>
  <div class="col-md-6">
    <h5>Name of Ingredient:</h5>
    <input type="text" class="form-control" name="mytext[]" required>
    <br>
  </div>
  <br>
</div>
<div class="col-md-12">
  <button class="add_field_button button loading-pulse">Add More Fields</button>
</div>

这就是我如何内爆它

        $ingredient_name =implode("^",$_POST["mytext"]);
        $ingredient_value = implode("^", $_POST["mytext2"]);

这就是我引爆的方法

$ingredient_name = explode("^",$ingredient_name);
$ingredient_value = explode("^", $ingredient_scale);
$ingredients = array_combine($ingredient_value,$ingredient_name);

这就是我循环的方式

                                    <table class="table-bordered" style="width:90%;">
                                            <th style="text-align:center;">Ingredient Scale</th>
                                            <th style="text-align:center;">Ingredient Name</th>
                                            <?php 
                                            foreach($ingredients as $amount => $name)
                                            {
                                                ?>
                                            <tr>
                                                <td style="margin:left:10px;"><?php echo "{$amount}"?></td>
                                                <td  style="margin:left:10px;"><?php echo "{$name}"?></td>
                                            </tr>
                                            <?php 
                                            }
                                            ?>
                                    </table>

例如在数据库中共有2列,

收件人_值收件人_名称

  • 1/4,1/2,1杯糖、肉、水

我希望它提取到一个输入字段,我可以编辑(重命名,添加,删除)例如

  • 1/4糖去除
  • 1/2肉去除
  • 1杯水

添加更多字段

您应该在循环内的表单中打印所有值,然后您需要某种"保存"按钮和一个准备接收新值的路由。例如,这个路由应该是一个php脚本,它将遍历新数据并执行更新。

我建议您编写PHP框架免费的独立应用程序。这样你就必须考虑很多事情,如果你是一个初学者,这对你有好处。

现在回到正题:

如果你能在循环中的某个地方打印一个数据库行的隐藏id,那就太好了:

<tr>
<td><input type="text" name="recipe_id" value="<?php echo $amount; ?> /></td>
<td><input type="text" name="recipe_id" value="<?php echo $name; ?> /></td>
<input type="hidden" name="recipe_id" value="<?php echo $recipe_id; ?> />
</tr>

还记得那个收集和更新数据的路线吗?让我们称之为"updatescript.php"。update-script.php应该对$_GET或$_POST变量中的每个字段执行检查,以查看该值是否已更改。如果是,则应更新该行;如果不是,则应跳过该行。这取决于你来创建逻辑。

此外,我注意到您的javascript代码需要进行一些调整。我只想暗示一件有点突出的事情:

$(add_button).click(function(e){

为了您未来的工作,请使用此处描述的"on"方法:http://api.jquery.com/on/

相信我,当您开始使用DOM元素、更改DOM和添加新事件时,这将是您的救星。

祝你好运!

我认为最简单的方法是这样构造输入。

<tr>
    <td>
        <input type="hidden" name="recipe[<?= $recipe_id; ?>][id]" value="<?php echo $recipe_id; ?>"  placeholder="value"/>
        <input type="text" name="recipe[<?= $recipe_id; ?>][value]" value="<?php echo $value; ?>"  placeholder="name"/>
    </td>
    <td><input type="text" name="recipe[<?= $recipe_id; ?>][name]" value="<?php echo $name; ?>" /></td>
    <td><input id="recipe_<?= $recipe_id; ?>" type="checkbox" name="recipe[<?= $recipe_id; ?>][delete]" value="1" /><label for="recipe_?= $recipe_id; ?>">Delete</label></td>
</tr>

其中$recipe-id是行id。然后您的javascript会添加更多的行,如

<tr>
    <td><input type="text" name="recipe[2][value]" value="" placeholder="value" /></td>
    <td><input type="text" name="recipe[2][name]" value="" placeholder="name" /></td>
    <td></td>
</tr>

然后在php中,您可以看到数组将被很好地格式化为食谱数组,如:

array(
    array(
        'name' => 'Pie'
        'value' => 2
        'recipe_id' => 1
    ),
    array(
        'name' => 'Deleted Pie'
        'value' => 2
        'recipe_id' => 2
        'deleted' => 1
    ),
    array(
        'name' => 'New pie'
        'value' => 2
    ),
)

然后,使用已删除的键,您可以检查是否需要删除Database行,如果它有recipe_id键,则可能需要更新它。如果它没有配方键,那么它就是一个新的配方。