1

I have this javascript function it creates a form containing the same information

function aTask(){
var noTask = parseInt(document.getElementById("nwTask").value);
var out = "<form action = \"\" method = \"POST\">"
for (var x = 0 ; x < noTask ; x++){
    out = out + "Task Name: <input type = \"text\" id = \"taskName\" class = \"taskInp\" name = \"taskName[]\"><br />"
    out = out + "Task Description: <input type = \"text\" id = \"taskDesc\" name = \"taskDesc[]\"><br />"   
    out = out + "Task Deadline: <input type = \"date\" id = \"taskDead\" name = \"taskDead[]\"><br /><hr>"
}
out = out + "</form>"
document.getElementById("taskList").innerHTML = out
 }

My problem is I don't know how to out the input information in the database and a user may ask up to 10 of this fields.

My question is I made a dynamic form with same names and type , how am I gonna store it to the database?

This is what I have right now and nothing is happening

    if (isset($_POST["saveNTask"]) && isset($_POST["newTask"])){
    $inpCount = $_POST['newTask'];
    for ($i = 0 ; $i < $inpCount; $i++){
    addProj($_POST["taskName"], $_POST["taskDesc"],$_POST["taskDead"]);
    }
}else{
    echo "damn";
}

Thanks to anyone who has time to read and/or answer this question My database right now is a simple one it has one table name task_tbl it will become a relational database in the future but right now I want to learn how to handle this type of programming problems

I forgot to post the query

function addProj ($projName, $projDesc, $projDeadLine){
    $date = $projDeadLine;
    $query = "INSERT INTO task_tbl (task_name , task_desc,task_dateAssign,task_deadline, task_status) VALUES('"+$projName+"','"+$projDesc+"','"+$date+"','"+$projDeadLine+"','"+ToDo+"')";
    mysqli_query($con,$query);
    mysqli_close($con);
}
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
  • Looking at the server side code `if (isset($_POST["saveNTask"]) && isset($_POST["newTask"]))`, I don't see those 2 post var's present in the form anywhere. Same with `$_POST['newTask']` in the next line. Also, did you intend for the name attribute in all 3 inputs to be the same? – John McMahon Jan 25 '14 at 15:38
  • @John McMahon yes they said that html can make array if the name = something[] so i went ahead and tried it although i don't know how to verify if it really is storing the values and that is also a copy paste error thanks for pointing it out – user3235394 Jan 25 '14 at 15:57
  • To verify the contents of your form variables on the server side after a post, do a `var_dump($_POST);` while debugging. – John McMahon Jan 25 '14 at 16:05

1 Answers1

0

You don't say what kind of database you're dealing with.

If it's a relational database, you need a one-to-many relationship between the entity that represents the form and the one for dynamic elements. Every time you add a form element you insert a row.

So it might look like this (MySQL syntax):

create table parent (
    pid int not null auto_increment,
    name varchar(80) not null
    primary key(pid)
);

create table child (
    cid int not null auto_increment,
    value varchar(80),
    pid int,
    primary key(cid),
    foreign key pid reference parent(pid)
);

You have to insert the form first, get its primary key, and make sure you insert it into the foreign key column when you insert a new form child.

To query a form:

select *
from parent
join child
on parent.pid = child.pid

If it's a NoSQL database, like MongoDB, you can serialize JSON and store it with a key. Get the value back by providing the key.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • My database (MySQL)right now is a simple one it has one table name task_tbl it will become a relational database in the future but right now I want to learn how to handle this type of programming problems – user3235394 Jan 25 '14 at 15:25
  • One-to-many means two tables: parent with primary key, child with primary key and a foreign key that points back to parent primary key. Do you know what that means? If not, do so research and try it. – duffymo Jan 25 '14 at 15:29
  • yes I know what it means I don't know the JSON and how to store in a key I am searching for themnow – user3235394 Jan 25 '14 at 15:30
  • The question is tagged with mysql, not sure if that was present when you answered. – John McMahon Jan 25 '14 at 15:40
  • So i found this question http://stackoverflow.com/questions/4688880/html-element-array-name-something-or-name-something I wonder can i use the array the is made so I can input it in the database (looking at the top answer) – user3235394 Jan 25 '14 at 15:44
  • @JohnMcMahon, I'll admit that I didn't pay any attention to the tag. – duffymo Jan 25 '14 at 15:46