简体   繁体   English

通过 HTML/PHP 动态创建表单

[英]Dynamically creating forms through HTML/PHP

Hoping someone could help me here.希望有人能在这里帮助我。 I'm in the middle of making a website and I'm pretty new to HTML and PHP.我正在制作一个网站,而且我对 HTML 和 PHP 还很陌生。 JavaScript is completely new to me. JavaScript 对我来说是全新的。

I have to allow the admin to dynamically create a form on the website I'm making.我必须允许管理员在我正在制作的网站上动态创建一个表单。 The admin wants to be able to click a button which will take them to a new page where they can name the field(s) they want to appear in the form and then save this form which they can then fill in and save to a database etc. They want to be able to do this when they log in to the site, so basically like having admin rights but not doing it through code, it has to be done on the website.管理员希望能够点击一个按钮,将他们带到一个新页面,在那里他们可以命名他们想要出现在表单中的字段,然后保存这个表单,然后他们可以填写并保存到数据库中等等。他们希望在登录网站时能够做到这一点,所以基本上就像拥有管理员权限而不是通过代码来做到这一点,它必须在网站上完成。

I am wondering is this possible?我想知道这可能吗? I don't really see how it will work when it comes to dynamically creating the fields in the database then inserting the information to those fields in the database also.在动态创建数据库中的字段然后将信息插入数据库中的这些字段时,我真的不知道它是如何工作的。 I don't no how I can create the queries seeing to store the information either.我也不知道如何创建查询来存储信息。

If anyone could help I would be very thankful, I really don't no where to begin to try and implement this feature.如果有人可以提供帮助,我将非常感激,我真的不知道从哪里开始尝试实现此功能。

Yes, it is possible, of course.是的,当然有可能。

It's not a good idea to store HTML in database.将 HTML 存储在数据库中不是一个好主意。 You can save dynamic data (eg input type, class, etc.) and then output them in page using simple php script when requested.您可以保存动态数据(例如输入类型、类等),然后在需要时使用简单的 php 脚本将它们输出到页面中。 For example:例如:

echo '<input type="'.$type.'">'; //$type is data read from db.

As far as I knew form your comments, you don't know how many notes you have to write per user in db, so you'll need to get it as an array.据我所知,从您的评论中,您不知道每个用户必须在 db 中写多少笔记,因此您需要将其作为数组获取。 For this you can name your inputs like: name[] .为此,您可以将输入name[]name[] For example you can have this HTML inside your form:例如,您可以在表单中包含此 HTML:

<input name="fieldName[]" value="myField">
<input name="fieldName[]" value="anotherField">

When you get it using $_POST you'll get array:当您使用$_POST获取它时,您将获得数组:

[fieldName] => array(2)
               {
                    [0] => "myField",
                    [1] => "anotherField"
               }

So you if you do:所以如果你这样做:

foreach ($_POST['fieldName'] as $field)
     echo $field;

output will be myFieldanotherField as you see it's executed one by another so you can store them in db using INSERT keyword.输出将是myFieldanotherField因为您看到它一个一个地执行,因此您可以使用INSERT关键字将它们存储在 db 中。 Here's how to save using PDO and here's more info about PDO 这是使用 PDO 进行保存的方法以及有关 PDO 的更多信息

From PHP end create a whole website in dynamically there is no restriction best example is CMS below code i am just trying to help him how its logic create from PHP end从 PHP 端动态创建整个网站没有限制最好的例子是下面的代码的CMS我只是想帮助他如何从 PHP 端创建它的逻辑

admin_rights.php admin_rights.php

<!--<script src="http://code.jquery.com/jquery-1.7.2.js"></script>-->
<script>
function dynamic_field(type,div_no){
    if(type == 'text'){
        document.getElementById('dynamic_field_'+div_no).innerHTML='TextField Name : <input type = "text" name="txt_field"> -> your text field has been generated just define name';    
    }else if (type == 'textarea'){
        document.getElementById('dynamic_field_'+div_no).innerHTML='TextArea Name : <input type = "text" name="text_area"> -> your text area has been generated just define name';  
    }else if (type == 'table_name'){
        document.getElementById('dynamic_field_'+div_no).innerHTML='Table Name : <input type = "text" name="table_name"> -> your table has been generated just define name';    
    }
}

</script>
Admin Rights <br />
<form action="action.php" method="post">
<input type="button" value="TextField" onclick="dynamic_field('text',1)" />
<input type="button" value="TextArea" onclick="dynamic_field('textarea',2)" />
<input type="button" value="Table Name" onclick="dynamic_field('table_name',3)" />

<br />

<?php 
for($i = 1; $i<=10; $i++){
?>
    <div id="dynamic_field_<?php echo $i;?>"></div>
<?php 
}
?>
<input  type="submit" value="submit" />
</form>

action .php行动.php

<pre>
<?php

$con = mysql_connect("localhost","root","");
if (!$con){
die('Could not connect: ' . mysql_error());
}

mysql_select_db("dynamic_form", $con);

mysql_query("
CREATE TABLE 
`dynamic_form`.`".$_REQUEST['table_name']."`
( `id` INT(11) NOT NULL AUTO_INCREMENT , `".$_REQUEST['txt_field']."` VARCHAR(225) , `".$_REQUEST['text_area']."` TEXT , PRIMARY KEY (`id`))  ;")
?>

Congrulation you have successfully generated <?php echo $_REQUEST['table_name'];?> table

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

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