使用jquery发送动态创建的表单

   ....... while(mysql_fetch_array($loop))
    {..............
    echo '
    <form name="myform" method="POST" action="reply.php">
    <textarea style="width:400px;" name="text'.$idanswer.'" id="text'.$idanswer.'"></    textarea>
    <input type="submit" name="submit'.$idanswer.'" id="submit'.$idanswer.'"     value="Submit">
     <input type="hidden" name="idquest" id="idquest" value="'. $question_id .'">
     <input type="hidden" name="idansw" id="idansw" value="'. $idanswer .'">
     <input type="hidden" name="idsend" id="idsend" value="'. $logOptions_id .'">
     <input type="hidden" name="namer" id="namer" value="'. $usr .'">
     </form>';
     }

It works good. But I want to send this form with jquery without refreshing the page, and i don't know how to pass the variables in the function.
I need something like this:

    <script type="text/javascript">
    $(document).ready(function() {
    $('submit'.$idanswer.'').click(function (e) {
            e.preventDefault();
            var dataString = 'text'.$idanswer.'=' + $('#text'.$idanswer.'').val() + '&idquest=' + $("#idquest").val() + '&idansw=' + $("#idansw").val() + '&idsend=' + $("#idsend").val() + '&namer=' + $("#namer").val();
            jQuery.ajax({
            type: "POST",
            url: "reply.php",
            data:dataString,
            success:function(response){
                $("#respond").prepend(response);
                                $('#text'.$idanswer.'').val('');
            },
            error:function (xhr, ajaxOptions, thrownError){
                alert(thrownError);
            }
        });
    });
});
</script>

This does not work because of variables. Any idea?

you need to echo your php variable

var dataString = 'text <?= $idanswer ?>=' + $('#text<?= $idanswer ?>').val() +&idquest=' + $("#idquest").val() + '&idansw=' + $("#idansw").val() + '&idsend=' + $("#idsend").val() + '&namer=' + $("#namer").val();

just change every $phpvar to <?= $phpvar; ?> or <?php echo $phpvar; ?>

Php can not be mixed with Js, but it can be embeded;

this is wrong:

 $('submit'.$idanswer.'')

the right is:

$('submit <?= $idanswer ?>')

or

$('submit <?php echo $idanswer; ?>')

Give each form a unique id, change submit button to a normal button input="button" so that it doesn't submit the form on clicked and add onclick property to the button like this:

<form name="myform" id="form'.$idanswer.'" method="POST" action="reply.php">
<input type="button" class="submit" name="submit'.$idanswer.'" id="submit'.$idanswer.'"     value="Submit" onClick="submitForm(\'form'.$idanswer.'\')" />

create ajax function and get parameters from each form using serialize() method which will get all form inputs values instead of adding each one manually:

$(document).ready(function() {
    function submitForm(form){
       jQuery.ajax({
            type: "POST",
            url: "reply.php",
            data: $('#' + form).serialize(),
            success:function(response){
                $("#respond").prepend(response);
                                $('#text'.$idanswer.'').val('');
            },
            error:function (xhr, ajaxOptions, thrownError){
                alert(thrownError);
            }
        });
    }
});