PHP怎样实现单击“添加”按钮增加一行表单项,并将所有内容插入到数据库中

WBOY
Release: 2016-06-23 13:45:59
Original
1616 people have browsed it

PHP怎样实现单击“添加”按钮增加一行表单项,并将所有内容插入到数据库中

在百度上看到这个提问,想着解答一下,效果如下图:


html+jquery:

<meta http-equiv="content-type" content="text/html;charset=utf-8"><script language="javascript" type="text/javascript" src="jquery.min.js"></script><script type="text/javascript">$(function(){    $(':button[name=add]').click(function(){        insertTr();    })    $('button[name=del]').click(function(){        $(this).parents('tr').remove();    })    $(':button[name=delall]').click(function(){        $('.itme').remove();    })})var gradeI=1;function insertTr(){    var html='';    html+='<tr class="itme"><td><input type="text" name="data[time][]">';    html+='<td><input type="radio" name="data[grade]['+gradeI+']" value="1">好<input type="radio" name="data[grade]['+gradeI+']" value="2">很好';    html+='<td><select name="data[type][]"><option value="优秀生">优秀生<option value="三好生">三好生';    html+='<td><button name="del">删除';    $('#tab').append(html);    $('button[name=del]').click(function(){        $(this).parents('tr').remove();    })       gradeI++;}</script> 
Copy after login
日期 级别 种类 操作
很好

exe.php

<?php $arr_time=$_POST['data']['time'];$arr_grade=$_POST['data']['grade'];$arr_type=$_POST['data']['type']; for($i=0;$i<count($arr_time);$i++){    $insert[$i]['time']=$arr_time[$i];    $insert[$i]['grade']=$arr_grade[$i];    $insert[$i]['type']=$arr_type[$i];} echo "<pre class="brush:php;toolbar:false">";print_r($insert);echo "
Copy after login
";/*每个数据是一条数据Array( [0] => Array ( [time] => 2014年11月7日 15:50:18 [grade] => 1 [type] => 三好生 ) [1] => Array ( [time] => 2014年11月7日 15:50:24 [grade] => 2 [type] => 优秀生 ) [2] => Array ( [time] => 2014年11月7日 15:50:27 [grade] => 1 [type] => 三好生 ) )*/?> 看到结果应该知道怎么做了吧。


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!