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

3
  • 1
    what do you mean doesn't stick around Commented Dec 17, 2013 at 3: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. Commented Dec 17, 2013 at 3:28
  • one solution might be when you get the submitted values you save them in db and recreate the page with all elements intact. Commented Dec 17, 2013 at 3:29

1 Answer 1

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.

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

1 Comment

Thanks man. I think I can use that for my application.. Thanks again.

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.