jquery表单动态添加元素及PHP处理

tijiao.php页面代码:

if(isset($_POST['cp1'])){
    echo '<pre>';
    print_r($_POST);
   $num=((count($_POST)-2)/2);//注:其中-2指的是html页面中不是动态添加的元素个数,例如:下面html页面中的 address,company元素。/2指的是每次动态添加的元素个数,例如:下面html页面中的cp1,cpsm1元素
    for($i=1;$i<=$num;$i++){ 


        echo '这是产品'.$_POST['cp'.$i].'产品说明:'.$_POST['cpsm'.$i].'<br>';

    }
    echo $_POST['address'].$_POST['company'];
  
}

html页面代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>jquery在表格中动态添加行--第一php网--网站开发代码库</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
    <script language="javascript">
        $(function(){
            $('#add').click(function(){
                add_tr();
            })
        })
        function add_tr(){
            var n=$('#head_tr .mycp').length;
            n=n+1;
            $("#head_tr>.mycp:last").after('<br><span class="mycp">产品名称:<input type="text" name="cp'+n+'">产品说明:<input type="textarea" name="cpsm'+n+'"></span>');
        }
    </script>
</head>

<body>
<form action="tijiao.php" method="post" enctype="multipart/form-data" >增加输入框</button>

tijiao.php页面代码:

if(isset($_POST['cp1'])){
    echo '<pre>';
    print_r($_POST);
   $num=((count($_POST)-2)/2);//注:其中-2指的是html页面中不是动态添加的元素个数,例如:下面html页面中的 address,company元素。/2指的是每次动态添加的元素个数,例如:下面html页面中的cp1,cpsm1元素
    for($i=1;$i<=$num;$i++){ 


        echo '这是产品'.$_POST['cp'.$i].'产品说明:'.$_POST['cpsm'.$i].'<br>';

    }
    echo $_POST['address'].$_POST['company'];
  
}

html页面代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>jquery在表格中动态添加行--第一php网--网站开发代码库</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
    <script language="javascript">
        $(function(){
            $('#add').click(function(){
                add_tr();
            })
        })
        function add_tr(){
            var n=$('#head_tr .mycp').length;
            n=n+1;
            $("#head_tr>.mycp:last").after('<br><span class="mycp">产品名称:<input type="text" name="cp'+n+'">产品说明:<input type="textarea" name="cpsm'+n+'"></span>');
        }
    </script>
</head>

<body>
<form action="tijiao.php" method="post" enctype="multipart/form-data" >增加输入框</button>