Reputation: 131
Hoping someone could help me here. I'm in the middle of making a website and I'm pretty new to HTML and PHP. JavaScript is completely new to me.
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.
Upvotes: 1
Views: 10582
Reputation: 12535
Yes, it is possible, of course.
It's not a good idea to store HTML in database. You can save dynamic data (e.g. input type, class, etc.) and then output them in page using simple php script when requested. 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. For this you can name your inputs like: name[]
. For example you can have this HTML inside your form:
<input name="fieldName[]" value="myField">
<input name="fieldName[]" value="anotherField">
When you get it using $_POST
you'll get array:
[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. Here's how to save using PDO and here's more info about PDO
Upvotes: 2
Reputation: 7097
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
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
<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
Upvotes: 1