简体   繁体   English

根据用户输入创建表单字段

[英]Creating form fields from user input

I found a code that creates some form fields based on a number chose by the user. 我发现了一个代码,该代码根据用户选择的数字创建一些表单字段。

<script type='text/javascript'>
         function addFields(){
            var number = document.getElementById("member").value;
            var container = document.getElementById("container");
            while (container.hasChildNodes()) {
                container.removeChild(container.lastChild);
            }
            for (i=0;i<number;i++){
                container.appendChild(document.createTextNode("Member " + (i+1)));
                var input = document.createElement("input");
                input.type = "text";
                input.name = "Member["+i+"]";
                container.appendChild(input);
                container.appendChild(document.createElement("br"));
            }
        }
</script>


<input type="text" id="member" name="member" value="">Number of Members: <br />
<input type="button" id="enterdetails" value = "Enter Details"onclick="addFields()">
<div id="container"/>
</div>

It works fine. 工作正常。 But I want to create elements with names/values imported from a database. 但是我想创建具有从数据库导入的名称/值的元素。 In php I do like this: 在PHP中,我这样做:

<SELECT name='smth'>
<OPTION value=""></OPTION>
<?php

$qry = $dbh->query("select * FROM table");

while ($a = $qry->fetch(PDO::FETCH_ASSOC)) {
$smth = $a['smth'];
echo "<OPTION value='$smth'>".$smth."</OPTION>";
}
?> 
</SELECT>

Need help on how to do it in javascript. 需要如何使用javascript的帮助。

You cant get database fields with javascript. 您无法使用javascript获取数据库字段。 You must use php or something like this. 您必须使用php或类似的东西。

Maybe this can help you. 也许这可以帮助您。 Here is the link . 这是链接 choose your database and table, and select your field names, and choose field's type of form (text area, text, radio button groups, etc) and push create. 选择数据库和表,然后选择字段名称,然后选择字段的表单类型(文本区域,文本,单选按钮组等),然后按“创建”。 You will have a bootstrap designed form and php insert code writed with medoo (medoo.in). 您将拥有一个引导程序设计的表单,并使用medoo(medoo.in)编写了php插入代码。

you can use this. 你可以用这个 create your database first and make form automaticly. 首先创建数据库并自动创建表单。 Copy code and paste to your pages. 复制代码并粘贴到您的页面。

Also i offer you to use meddo to connect and use sql with it. 我也为您提供使用meddo来连接和使用sql的方法。 There is a sample in that link form.min.php, includes your database and user and pass information. 该链接form.min.php中有一个示例,其中包括您的数据库以及用户和密码信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM