doubi7739 2013-04-24 00:53
浏览 78

使用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?

  • 写回答

2条回答 默认 最新

  • duanhao1004 2013-04-24 01:06
    关注

    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; ?>')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo