简体   繁体   English

如何根据数据库输出的内容显示输入字段

[英]How to display the input fields according to the what outputs come from the database

I am trying to displaying the input fields depending upon the what outputs are coming from the database. 我试图显示输入字段,具体取决于来自数据库的输出。 Please share any idea or logic in this. 请分享任何想法或逻辑。 I have more than 25 fields. 我有25个以上的字段。

Page1.php Page1.php

I have Name(Text type), Email(Email type), gender(Radio), country(Select dropdown), Address( Textarea) in the form. 我在表单中有姓名(文本类型),电子邮件(电子邮件类型),性别(无线电),国家(选择下拉列表),地址(文本区域)。 The user will click on check box whatever he needs from the form and click on submit then the value of the fields which he selects will store in the database. 用户将在表单中根据需要单击复选框,然后单击“提交”,然后他选择的字段的值将存储在数据库中。

Form page 表格页面 在此处输入图片说明

Page2.php Page2.php

Here I am displaying the fields which are selected from Page1.php but also I need input type related to which fields come from the database. 在这里,我显示的是从Page1.php中选择的字段,但我还需要与哪些字段来自数据库有关的输入类型。

For example: In the page1 I choose Name, Email, Country and submitted the form then In page2 I have to display the output <input type="text" name="Name">,<input type="text" name="Name">,<select></select>. 例如:在page1中,我选择Name,Email,Country并提交表单,然后在page2中,我必须显示输出<input type="text" name="Name">,<input type="text" name="Name">,<select></select>.

I need to know what logic I have to use it. 我需要知道必须使用什么逻辑。 Can any one help me with the script? 有人可以帮我编写脚本吗?

Form code 表格代码

<form action="" method="post">
<input type="checkbox" name="check-fields[]"  value="Name">
<label>Name(Text type)</label>
<input type="checkbox" name="check-fields[]"  value="Email">
<label>Email(Email type)</label>
<input type="checkbox" name="check-fields[]"  value="Gender">
<label>Gender(Radio type)</label>
<input type="checkbox" name="check-fields[]"  value="Select">
<label>Country(Drop down)</label>
<input type="checkbox" name="check-fields[]"  value="textarea">
<label>Address(Textare)</label>
<input type="submit" name="submit" value="submit">
</form>

Storing the value in the database 将值存储在数据库中

if (isset($_POST['submit'])) {
// prepare and bind
$a=$_POST['check-fields'];
$elements= implode(',',$a);
$sql_elements="INSERT INTO test (elements) VALUES (?)";
$stmt = $conn->prepare($sql_elements);
$stmt->bind_param("s",  $elements);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
}

Page 2 第2页

I am getting output but how can I set the input fields? 我正在获取输出,但是如何设置输入字段?

$sql_fields="SELECT elements FROM test WHERE id=3";
if ($stmt = $conn->prepare($sql_fields)) {
            $stmt->execute(); 
            $stmt->bind_result($elements);
        while ($stmt->fetch()) {
            $arr=explode(",", $elements);
            echo "<pre>";
            print_r($arr);
            echo "</pre>";
       }
   }

$stmt->close();
$conn->close();
?>

You're trying to create a personalized form for different users if I'm correct, meaning that you will allow a user to generate a form based on a selection of fields the user can choose from. 如果我是对的,您正在尝试为不同的用户创建个性化表单,这意味着您将允许用户根据用户可以选择的字段来生成表单。

What you need to do is create a table called "forminputtypes" where you create a row for each different input field you can think of, and want to give the user as a choice to select from. 您需要做的是创建一个名为“ forminputtypes”的表,在其中为您可以想到的每个不同输入字段创建一行,并希望为用户提供选择的选择。 Your table would looke like this: 您的表如下所示:

Id | FieldName | Type  | etc ->
-------------------------------
1  | Name      | text  | ...
2  | Email     | email | ...
3  | Gender    | radio | ...

You can add more columns in the table to store more information (think of possible values in a radio input or a dropdown). 您可以在表中添加更多列以存储更多信息(考虑单选输入或下拉菜单中可能的值)。

Now at in Page1.php you select all input types from this table, and display them like you already are. 现在,在Page1.php中,从此表中选择所有输入类型,并像以前一样显示它们。 However, the value of these checkboxes will be the corresponding Id value of the record in the database like so: 但是,这些复选框的值将是数据库中记录的相应Id值,如下所示:

<input type="checkbox" name="check-fields[]"  value="3">

Now when someone chooses the Gender field, you can see in your Page2.php that the user did so by matching his choice '3' to the record in the database. 现在,当有人选择“性别”字段时,您可以在Page2.php中看到用户通过将其选择的“ 3”与数据库中的记录进行匹配来做到这一点。 If you want to save this information, you can create another table called UserFormInputFields which will function as a couple table between your user table and the FormInputTypes table. 如果要保存此信息,可以创建另一个名为UserFormInputFields表,该表将用作用户表和FormInputTypes表之间的一对表。

When you want to display the form in Page2.php, you simply get all the input fields the user chose by selecting on Id from the FormInputTypes table. 当您想在Page2.php中显示表单时,只需从FormInputTypes表中的Id选择,即可获得用户选择的所有输入字段。 Since you know the type of each input field (because it's a column in the table) you can display them all correctly. 由于您知道每个输入字段的类型(因为它是表中的一列),因此可以正确显示它们。

If you want to do it in core PHP only.Just put if else around the html tags.If value is coming from Database then you show particular tag else nothing. 如果您只想在核心PHP中执行此操作,请在html标记周围添加其他标记。如果值来自数据库,则显示特定标记,否则不显示任何内容。 You can modify your page 2 code like. 您可以像修改您的第2页代码。

<?php
$sql_fields="SELECT elements FROM test WHERE id=3";
if ($stmt = $conn->prepare($sql_fields)) {
            $stmt->execute(); 
            $stmt->bind_result($elements);
        while ($stmt->fetch()) {
            $arr=explode(",", $elements);
            echo "<pre>";
            print_r($arr);
            echo "</pre>";
       }
   }

$stmt->close();
$conn->close();
?>
<form action="" method="post">
<?php if(in_array("Name",$arr)) { ?>
 <input type="text" name="name"  >
 <label>Name</label>
<?php } ?>
<?php if(in_array("Email",$arr)) { ?>
 <input type="email" name="email">
 <label>Email</label>
<?php } ?>
<?php if(in_array("Gender",$arr)) { ?>
 <input type="radio" name="gender"  value="Male">
 <input type="radio" name="geneer"  value="Female">
<label>Gender(Radio type)</label>
<?php } ?>
//.............write other fiels like this.
<input type="submit" name="submit" value="submit">
</form>

I hope this helped. 希望对您有所帮助。

暂无
暂无

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

相关问题 如何使用普通 javascript 根据所选输入显示字段 - How to display fields according to selected input using plain javascript 根据从下拉框中选择的内容来更改输入字段的值 - Change the value of input fields, according to what is selected from the Dropdown box 如何使用来自数据库的值验证Tapestry的表单字段? - How to validate Tapestry's form fields with values that come from database? 单击我的Web表单上的Submit按钮时,即使这些字段具有值,输入字段的验证错误也会如何显示? - When clicking the Submit button on my web form, how come the validation errors for the Input fields display, even when these fields have values? 输入数字并根据输入的数字显示输入字段 - Input number and display the Input Fields according to the number inputed 自动完成显示数据库中的所有数据(不根据输入字段中的单词类型) - Autocomplete display all data from database ( Not according to words type in input field ) 仅当 ID 存在于数据库中时才显示输入字段 - Display input fields only if ID exists in database 如何以自举模式显示来自输入字段的信息? - How to display information from input fields in a bootstrap modal? 根据用户输入的输入字段数 - no of input fields according to user input 如何在复选框上单击以使用数据库中的值填充输入文本字段 - How to populate input text fields with values from database on checkbox clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM