0

i want to post dynamically generated multiple text box using php by foreach loop. for instance i m working on question bank and for post multiple answer from php. here is my code that dynamically generated textbox but now i donot know how to post value of generated textbox thru foreach loop.

$("#questionbank").live('click',function(){
    var mydata = '<div id="questionbank-content" class="page">';
    mydata += '<div class="question"><div class="Label">Question</div><div class="textarea"><textarea id="question" rows="3" cols="30"></textarea></div></div><div class="answer"><div class="Label">Answer</div><div class="textarea"><textarea id="answer" rows="3" cols="30"></textarea></div><div class="option"><input id="a" name="answers" type="radio"></div><span id="add">Add</span>';
    var i=1;
    $('#add').live('click',function(){
         j=i++;
         $(this).after('<div class="Label">Answer</div><div class="textarea"><textarea id="answer + '+ j +'" rows="3" cols="30"></textarea></div><div class="option"><input id="a + '+ j +'" name="answers" type="radio"></div><span id="add">Add</span>');

        //$(this).after('<input id="'+ j +'" type="text" value="'+ j +'"/><span id="add">Add</span>');
        $(this).remove(); 
    });
    mydata += '</div></div>';
    $("#leftcontainer").html(mydata);

});
1
  • 1
    are the text boxes part of a form? Commented Jun 7, 2011 at 10:53

2 Answers 2

1

Consider the following HTML

<div id="start">Start</div>

<div id="container">

</div>

I guess you want to post data from each value of the textbox/textarea by $.post() or $.ajax()

var i=1;
$('#start').one('click',function(){
    j=i++;
   $('#container').append('<input id="answer'+ j +'" type="text" value=""/><span id="add">Add</span>');
    $('#container').after('<div id="submit">Submit</div');
});

$('#add').live('click',function(){
     j=i++;
    $(this).after('<input id="answer'+ j +'" type="text" value=""/><span id="add">Add</span>');
    $(this).next().next().after('');
    $(this).remove(); 
});

$('#submit').live('click',function(){
    var x = $('#container > input');
    var qstring='op=insertquestion'; //additional para if u want to pass any
    $.each(x,function(index,value){
        var id = $(this).attr('id');
        var data = $(this).val();
        qstring += '&'+id+'='+data+'';
    });
    $('#start').before(qstring + '<br/>');
});

After you get the variable qstring just post the string using $.post() method.

Example on JSFIDDLE.net

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

Comments

0

The easiest way would be to construct the textarea and submit button via jQuery and attach an click handler. Not a complete solution but it should hopefully give you an idea:

$("#add").live("click", function() {
    var textarea = $("<textarea />", { "class": "textarea" });
    var submitButton = $("<button />", { "type": "submit" });

    submitButton.bind("click", function() {
        var text = textarea.val();
        $.post("/path/to/script.php", { text: text });
    });
});

This will create a textarea and a submit button. When invoking the submit button, a HTTP POST is made to script.php with the textarea content as a parameter.

2 Comments

do you can give any other code that post dynamically generated textbox value thru foreach loop
do you can give any other code that post dynamically generated textbox value thru foreach loop

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.