0

I had followed the code on adding multiple fields on click event using jquery.

    <div id="address">     
      <div id="question" style="margin-bottom:4px;" class="input">
        Question <br> <input name="qnsLabel"  type="text"id="question"> 
    </div>
    <div id="answer" style="margin-bottom:4px;" class="input"> 
        Answer <br>
        <input name="ansLabel"  type="text" id="answer">  
    </div> <br> 
    </div>

The above is the code for the html part. As the jquery work on adding the whole div and giving a class name of "clonedAddress" when the add button is clicked. So my question is, how do i store the individual fields (question and answer) into mysql?

2
  • Have you tried anything? Perhaps look into AJAX, PHP and SQL. Commented Jul 10, 2013 at 7:40
  • use <form> and post your question and answer.. :) Commented Jul 10, 2013 at 7:41

4 Answers 4

1

If you are using a general page load approach then simply use <input name="ansLabel[]"> and <input name="qnsLabel[]" type="text>. Upon submitting of the main form you will get the array of questions and answers.

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

3 Comments

I'm using the jquery to generate new div so it new text boxes of questions and answer will appear. But using the jquery generates by the whole of <div id="address">. What i needed to store is the <div id="questions"> and <div id="answers">. Any suggestion?
when generating the div you need to change it there: <div id="address"> <div id="question" style="margin-bottom:4px;" class="input"> Question <br> <input name="qnsLabel[]" type="text"id="question"> </div> <div id="answer" style="margin-bottom:4px;" class="input"> Answer <br> <input name="ansLabel[]" type="text" id="answer"> </div> <br> </div>
When in JQuery change : newElem.find('input').each(function() { this.id = this.id + newNum; this.name = this.name + '[]'; }); See if it helps.
0

What you are looking for is called AJAX, which makes an asynchronous call to some back-end script. This back-end script (PHP file that is) will run the SQL queries and such.

So the idea is to pass the necessary data from jQuery by AJAX to PHP, and then perform all database-related stuff in your PHP file.

Comments

0

I presume that the code you supplied is enclosed in the <form ... > element and there is also a submit button within the form.

As your code stands, only one 'qnsLabel' and one 'ansLabel' will be submittted to your php page. You will need to alter these to qnsLabel[] and ansLabel[] so that multiple fields, with the same name can be sent to your php page.

Once the php page is called the values entered within the html page will be in two arrays. This is where you create a loop, building you sql statement so that each question/answer can be stored in your database.

Comments

0

Edit with jQuery and AJAX, you can get the form values, something like this:

$(document).ready(function() {
    $('#form').submit(function(){
        $.ajax({
            url: "process.php",
            type: 'POST',
            data: {
                name: $('input[name="qnsLabel"]').val(),
                email: $('input[name="ansLabel"]').val()
            },
            cache: false,
            success: function(data){
                $('#out').html(data);
            }
        });
        return false;
    });
});

Then you need to make a PHP script (I called it process.php above) that will receive the values and insert them in the database. Your script can also echo a response, which you will receive in the data varaiable above. It can then be inserted in your HTML. In the above example, it's getting inserted into an element with `id+"out".

Comments

Your Answer

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