0

我有一个页面,用户可以在其中创建食谱。我有一个 javascript 脚本,允许用户继续添加表单输入元素以获取更多成分。假设配方有 4 种成分,用户可以继续添加输入。但是,这会将输入 ID/名称的 id 更改为成分 1/2/3/4,我无法理解如何绕过从表单发布中获取信息,因为元素的数量取决于用户的判断。您将如何将结果加载到 PHP 变量中,然后将它们插入到 mysql 查询中。

html 表单的语法

<div id="ingredient1" class="clonedInput">
                <h2 class="first-column">Ingredient</h2>
                <span style="float:left; padding-right: 10px;">
                <input type="text" name="scroll" value="Add Ingredient" id="ingredient" class="ingredient"/>
                </span>
</div>

为了添加目的 2 成分,成分 1 将更改为成分 2。

一个稍微令人困惑的问题,所以任何帮助都会很棒!

JB

4

2 回答 2

1

I think what you are looking for is using [] in your input element names. This will post the data as an array. So, say you have this as your form's ingredient input elements (after javascript has added several):

<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />

When the user POST's the data, you will be able to access the array of ingredients at $_POST['ingredients']

于 2012-12-11T00:16:51.600 回答
0

I will tell you the logic, not the code as only javascript here would not be enough.

  1. you create a table ingredients. Anything you can allow them to use. id, ecc
  2. while posting, instead of doing giving the user a freedom chosing their ingredient1, ingredient2 ... you do load from ingredients table the values and index them as ingredient[id_from_db] ecc..
  3. you create a table recipe. It should have a unique id, its description
  4. you create a relation table recipe_id, ingredient_id

then when the user post its data you create 1 row in the recipe and as many rows in the relation table as the ingredients they use.

When selecting later the recipe you also select everything which is in the relation table, it could be several rows there...

Hope it is of any help for you. Can't show you any code for this, but it is not hard to make if you understand how it goes.

maxim

于 2012-12-11T00:22:26.657 回答