1

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.

4
  • Is there a reason why you are not considering using some CMS (Wordpress? drupal? joomla? ) for the website ? sounds like your life would be much easier . especially if you declare that you do not have the proper skills needed . that is exactly what those systems are for . Commented Jun 13, 2012 at 10:41
  • @Obmerk Kronen I can't use either of those as its an internal tool for a company I'm doing student placement on. Commented Jun 13, 2012 at 10:45
  • well - 1) I do not see a problem with using wordpress as an internal system for anything (I even one time made it as a PMS integrated system for hotels) and 2) - I just suggested it because what you asked seems fairly complicated to do without the proper skills. there is way too much to learn, and I doubt (hope, but still doubt) that someone here can provide you a ready-made solution that would work for you out of the box (without even knowing your DB structure..) Commented Jun 13, 2012 at 10:48
  • Oh right ok. It would probably be too late to change to Wordpress now as this is one of the last features needed for the time being. I knew this was going to be hard, I had a engineer working with me on what I am developing now a while ago and he said it would be quite a complicated thing to do also. I would love a ready made solution :) but even an idea where to begin would be great. Commented Jun 13, 2012 at 10:50

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

I don't really understand that to be honest @PLB particularly when it comes to storing in the database. What the admin wants is a button for example 'Add Field' - When this is clicked a new text field will appear, they can name the text field such as Name, then click something like 'Save Form' and this saves the form the admin created. Maybe its just a lack of experience but I don't understand how it will all work when it comes to saving the fields in the database.
The whole data will be kept in array, after submitting "Save Form" your php script will iterate over that array and save one item by another (you have to create something INSERT INTO table_name VALUES (value1, value2,...)) and then execute. But this is old-styled query. you can use pdo now and I've given you a tutorial.
Thanks, I'll have a look at PDO now
1

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

9 Comments

Are you going to create new table everytime? This will make thing very complicated when you start reading generated data. It's be better to keep everything in one table.
i just explain to him how its logic make from php end @PLB
I've just set this up quickly and it is the idea that I'm looking for anyways. @PLB Using what you sent me as a tutorial do you mean?
@davef any issue regarding my answer please ask me
@QueryMaster is it possible to message you or will I just ask here?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.