在刷新之前将数据从jquery检索到php

时间:2012-07-12 10:27:46

标签: php jquery arrays forms

如何从jquery生成的输入中检索数据以使用php $ _POST处理它们?

(function($){

         $countForms = 1;

              $.fn.addForms = function(idform){

                            var myform = "<table>"+
                             "  <tr>"+
                             "     <td>Field A ("+$countForms+"):</td>"+
                             "     <td><input type='text' name='field["+$countForms+"][a]'></td>"+
                             "     <td>Field B ("+$countForms+"):</td>"+
                             "     <td><textarea name='field["+$countForms+"][b]'></textarea></td>"+
                             "     <td><button>remove</button></td>"+
                             "  </tr>"+
                             "</table>";



                            if(idform=='mybutton'){

                                myform = $("<div>"+myform+"</div>");
                                $("button", $(myform)).click(function(){ $(this).parent().parent().remove(); });
                                $(this).append(myform);
                                $countForms++;
                            }

              };
        })(jQuery);         



        $(function(){
            $("#mybutton").bind("click", function(e){
            e.preventDefault();
            var idform=this.id;

                if($countForms<3){
                    $("#container").addForms(idform);
                }       
            });
        });

html / php code:

if ( isset( $_POST['field'] ) )
{
    echo '<table>';
    foreach ( $_POST['field'] as $diam )
    {
        // here you have access to $diam['top'] and $diam['bottom']
        echo '<tr>';
        echo '  <td>', $diam['a'], '</td>';
        echo '  <td>', $diam['b'], '</td>';
        echo '</tr>';
    }
    echo '</table>';
    }

?>

<body>

<div>
<form method="post" name="b" >
<table>
    <tr>
        <td>Field A</td>
        <td><input type='text' name='field[0][a]'></td>
        <td>Field B</td>
        <td><textarea name="field[0][b]"></textarea></td>
        <td><button>remove</button></td>
    </tr>
</table>
</div>
<div id="container"></div>
<button id="mybutton">add form</button>

<div align="center">
<p><input type="submit" value="Registar" name="registar"></p>
</div>
</form>

我无法从加载页面后jquery生成的重复输入中检索数据。我知道因为foreach只是回显了html部分中输入的值。任何建议我如何获取jquery输入的值来处理它们与PHP?

0 个答案:

没有答案