0

I have written some code to create a dymanic HTML form element for my application,I want on submit that form will post data and php will write to database.

here is the code

 $("#add_id").click(function(name){
                name=prompt("Task name");
                if (name) {
                   $('.dashboard').append('<div class="each_task" id="task'+ count+'"><label>'+name+
          '</label><form class="task_form" action="process_task.php" method="post"><div class="ctrl_btn" id="start'+
          count+'"><input type="hidden" name="task_name" value="'+name+'" /><input name="task_start" type="hidden" id="start_time'+
          count+'" value="" /><input class="task_start" type="submit" id="'+count+'" value="Start" /></form></div><form class="task_form" action="process_task.php" method="post"><div class="ctrl_btn" id="end'+count
          +'"><input type="hidden" name="task_name" value="'+name+'" /><input name="task_end" type="hidden" id="end_time'+count+'" value="" /><input class="task_end" type="submit" id="'+count+'" value="End" /></form></div></div>').css('color', 'red');

Now when I submit the form it sends name and start_time or end_time value to php script which will insert data to database. the problem is dynamically created HTML element doesn't stick around.

Can anyone suggest how could I get around this problem and can have my dynamically created html stick to the page and also on submit post data to database.

Thanks

Rumman Ahmed
  • 189
  • 1
  • 1
  • 11
  • 1
    what do you mean `doesn't stick around` – Manoj Purohit Dec 17 '13 at 03:19
  • Sorry for confusion, basically when form submission occurs,it goes to action url,and initial page goes back to static html element page.I thought one way could be getting submitted info from db and create page with that could be one way to keep dynamically created html element. – Rumman Ahmed Dec 17 '13 at 03:28
  • one solution might be when you get the submitted values you save them in db and recreate the page with all elements intact. – Manoj Purohit Dec 17 '13 at 03:29

1 Answers1

1

I'm happy to improve my answer if I didn't get your question accurately, but I'm assuming you want to stop the page from getting refreshed when you press the form submit button.

Here's a solution to prevent the page refresh using Javascript.

It is important to note that if you use this solution, you have to handle the form submission using Javascript and AJAX. An example of how to do this.

Community
  • 1
  • 1
Henry Cho
  • 770
  • 2
  • 15
  • 33