0

I need to add a new form to store graph information everytime the button is clicked.

Everytime I click the button I should get the Div to increment by 1 so that the new information can be stored in the database with a unique id.

.jumbotron{
width:1000px;
height:100px;
background-color:lightblue;
}

<form action = "" method = "post">
<input type = "submit" name = "submit" value = "Add a new form">
</form>

<div class = "jumbotron">
First Name: <input type="text" name="firstname"><br>
Last Name: <input type="text" name="lastname"><br>
</div>

Refer here: https://jsfiddle.net/rk6eyym6/

11
  • 2
    And what is your problem, exactly? Commented Oct 2, 2015 at 7:36
  • I am unable to do a duplicate of the div by 1 using for loop when button is clicked. Commented Oct 2, 2015 at 7:38
  • 1
    Well, show us the code that gets executed on button submit. Commented Oct 2, 2015 at 7:39
  • Div increment to one? Or you meant ID? If ID is the case, you should manage your table structure and have a primary key and set it to Auto Increment. And what's up with those two inputs having the same name tags? Commented Oct 2, 2015 at 7:40
  • I have the javascript code not the PHP for loop. Commented Oct 2, 2015 at 7:40

1 Answer 1

1

You wanted to add more forms when the "Add a new form" button is clicked? We can achieve this using Javascript. But i'll be teaching you how to do it using a Javascript library called jQuery.

First, download the library here.

Then, we can proceed in creating the script.

<script src="jquery-1.9.1.min.js"></script> <!-- CHANGE THE NECESSARY FILE DEPENDING ON THE VERSION YOU HAVE DOWNLOADED -->
<script>

  $(document).ready(function(){ /* PREPARE THE SCRIPT */       
    $("#add").click(function(e){ /* WHEN ADD BUTTON IS CLICKED */
      e.preventDefault(); 
      $(".jumbotron").append('First Name: <input type="text" name="firstname[]"><br>Last Name: <input type="text" name="lastname[]"><br>'); /* APPEND TWO MORE FIELDS */
    });

  });
</script>

You can check this jsfiddle for an example.

Note, you have to add [] on the name tags of your input fields so that they'll be interpreted as an array when they are passed on. Not doing so will only cause to read the first set of data.

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

5 Comments

What if I want to store the values of different form into the database?
that is your server side script after you submit the form. #LoganWayne fixed your first problem.
You can use an INSERT query. You can check this link for more information about it.
I am using the onchange=this.form.submit() function so that once i insert a name it will automatically insert into my database. However i need to make sure I insert and do an update when user changes the value, how do I go about with that
@DivyaK - you can post a new question about your problem. My answer only did solve the one you are asking for this post.

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.