我有一个动态生成的表单。单击addmore按钮时,表单会自动生成。 这是我的html表单标记
<form name="addcourses" action="" method="post">
<fieldset><legend>Add Courses</legend>
<table width="500px" border="0" id="tbltest">
<tr>
<td><select name="txtcategory0" id="id_str">
<option value="" selected="selected"></option>
<option value="Management">Management</option>
<option value="Medical">Medical</option>
<option value="Dental">Dental</option>
<option value="Nursing">Nursing</option>
<option value="Ayurvedic">Ayurvedic</option>
<option value="Homeopathy">Homeopathy</option>
<option value="Unani">Unani</option>
<option value="Physiotherapy">Physiotherapy</option>
<option value="Pharmacology">Pharmacology</option>
<option value="Naturopathy & Yoga">Naturopathy & Yoga</option>
<option value="Engineering">Engineering</option>
<option value="Information Technology">Information Technology</option>
<option value="Computer / IT Training Institutes">Computer / IT Training Institutes</option>
<option value="Animation">Animation</option>
<option value="Polytechnic">Polytechnic</option>
<option value="Maritime">Maritime</option>
<option value="ITI">ITI</option>
<option value="Biotechnology">Biotechnology</option>
<option value="Law">Law</option>
<option value="Fashion Technology">Fashion Technology</option>
<option value="Hotel Management">Hotel Management</option>
<option value="Mass Media">Mass Media</option>
<option value="Distance Learning">Distance Learning</option>
<option value="Library Science">Library Science</option>
<option value="Degree Colleges">Degree Colleges</option>
<option value="B.Ed">B.Ed</option>
<option value="Fine Arts">Fine Arts</option>
<option value="Film Institutes">Film Institutes</option>
<option value="Tutorials">Tutorials</option>
<option value="Entrance Coaching">Entrance Coaching</option>
<option value="Vocational Courses">Vocational Courses</option>
<option value="Beautician Courses">Beautician Courses</option>
<option value="Language Courses">Language Courses</option>
<option value="Certificate Courses">Certificate Courses</option>
<option value="Schools">Schools</option>
<option value="International Schools">International Schools</option>
<option value="Private Schools">Private Schools</option>
<option value="Nursery / Play Homes">Nursery / Play Homes</option>
<option value="Flying Institutes">Flying Institutes</option>
<option value="Call Center Training">Call Center Training</option>
<option value="Sports Academy">Sports Academy</option>
<option value="10 + 2">10 + 2</option>
</select></td>
<td><input type="text" name="txtcourse_name0" id="id_cname" value="" /></td>
<td><input type="text" name="txtseat0" id="id_seats" value="" style="width:40px;" /></td>
<td><select name="txtfees0" id="id_elig" >
<option value="" selected="selected"></option>
<option value="1 - 5000">1 - 5000</option>
<option value="5001 - 10000">5001 - 10000</option>
<option value="10001 - 20000">10001 - 20000</option>
<option value="20001 - 30000">20001 - 30000</option>
<option value=">30001 - 40000">30001 - 40000</option>
<option value="40001 - 50000">40001 - 50000</option>
<option value="50001 - 60000">50001 - 60000</option>
<option value="60001 - 70000">60001 - 70000</option>
<option value="70001 - 80000">70001 - 80000</option>
<option value="80001 - 90000">80001 - 90000</option>
<option value="90001 - 1 Lac">90001 - 1 Lac</option>
<option value="1 Lac - 1.5 Lac">1 Lac - 1.5 Lac</option>
<option value="1.5 Lac - 2 Lac">1.5 Lac - 2 Lac</option>
<option value="2.5 Lac - 3 Lac">2.5 Lac - 3 Lac</option>
<option value="3.5 Lac - 4 Lac">3.5 Lac - 4 Lac</option>
<option value="4.5 Lac - 5 Lac">4.5 Lac - 5 Lac</option>
<option value="5.5 Lac - 6 Lac">5.5 Lac - 6 Lac</option>
<option value="6.5 Lac - 7 Lac">6.5 Lac - 7 Lac</option>
<option value="7.5 Lac - 8 Lac">7.5 Lac - 8 Lac</option>
<option value="8.5 Lac - 9 Lac">8.5 Lac - 9 Lac</option>
<option value="9.5 Lac - 10 Lac">9.5 Lac - 10 Lac</option>
<option value="10.5 Lac - 11 Lac">10.5 Lac - 11 Lac</option>
<option value="11.5 Lac - 12 Lac">11.5 Lac - 12 Lac</option>
<option value="12.5 Lac - 13 Lac">12.5 Lac - 13 Lac</option>
<option value="13.5 Lac - 14 Lac">13.5 Lac - 14 Lac</option>
<option value="14.5 Lac - 15 Lac">14.5 Lac - 15 Lac</option>
<option value="15 Lac & Above">15 Lac & Above</option>
</select></td>
</tr>
</table>
<div> <input type="button" onclick="addRow();" value="Add More"/>
<input type="button" value="Remove" onclick="removeRow();" />
<input type="hidden" name="len" value="1" /></div>
<input name="submit" type="submit" value="submit" />
</fieldset>
</form>
我使用这个java脚本添加表单字段
<script type="text/javascript">
function addRow() { var tbl = document.getElementById('tbltest'); var lastRow = tbl.rows.length; //如果表中没有标题行,则迭代= lastRow + 1 var iteration = lastRow; var row = tbl.insertRow(lastRow);
var cell = row.insertCell(0);
var sel = document.createElement('select');
sel.name = 'txtcategory' + iteration;
sel.id = 'id_str' + iteration;
sel.onchange = function (){ streamChange(iteration, this) };
sel.options[0] = new Option('Stream','');
sel.options[1] = new Option('Management','Management');
sel.options[2] = new Option('Medical','Medical');
sel.options[3] = new Option('Dental','Dental');
sel.options[4] = new Option('Nursing','Nursing');
sel.options[5] = new Option('Ayurvedic','Ayurvedic');
sel.options[6] = new Option('Homeopathy','Homeopathy');
sel.options[7] = new Option('Unani','Unani');
sel.options[8] = new Option('Physiotherapy','Physiotherapy');
sel.options[9] = new Option('Pharmacology','Pharmacology');
sel.options[10] = new Option('Naturopathy & Yoga','Naturopathy & Yoga');
sel.options[11] = new Option('Engineering','Engineering');
sel.options[12] = new Option('Information Technology','Information Technology');
sel.options[13] = new Option('Computer / IT Training Institutes','Computer / IT Training Institutes');
sel.options[14] = new Option('Animation','Animation');
sel.options[15] = new Option('Polytechnic','Polytechnic');
sel.options[16] = new Option('Maritime','Maritime');
sel.options[17] = new Option('ITI','ITI');
sel.options[18] = new Option('Biotechnology','Biotechnology');
sel.options[19] = new Option('Law','Law');
sel.options[20] = new Option('Fashion Technology','Fashion Technology');
sel.options[21] = new Option('Hotel Management','Hotel Management');
sel.options[22] = new Option('Mass Media','Mass Media');
sel.options[23] = new Option('Distance Learning','Distance Learning');
sel.options[24] = new Option('Library Science','Library Science');
sel.options[25] = new Option('Degree Colleges','Degree Colleges');
sel.options[26] = new Option('B.Ed','B.Ed');
sel.options[27] = new Option('Fine Arts','Fine Arts');
sel.options[28] = new Option('Film Institutes','Film Institutes');
sel.options[29] = new Option('Tutorials','Tutorials');
sel.options[30] = new Option('Entrance Coaching','Entrance Coaching');
sel.options[31] = new Option('Vocational Courses','Vocational Courses');
sel.options[32] = new Option('Beautician Courses','Beautician Courses');
sel.options[33] = new Option('Language Courses','Language Courses');
sel.options[34] = new Option('Certificate Course','Certificate Course');
sel.options[35] = new Option('Schools','Schools');
sel.options[36] = new Option('International Schools','International Schools');
sel.options[37] = new Option('Private Schools','Private Schools');
sel.options[38] = new Option('Nursery / Play Homes','Nursery / Play Homes');
sel.options[39] = new Option('Flying Institutes','Flying Institutes');
sel.options[40] = new Option('Call Center Training','Call Center Training');
sel.options[41] = new Option('Sports Academy','Sports Academy');
sel.options[42] = new Option('10 + 2','10 + 2');
cell.appendChild(sel);
var cell2 = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'txtcourse_name' + iteration;
el.id = 'id_cname' + iteration;
cell2.appendChild(el);
var cell4 = row.insertCell(2);
var el = document.createElement('input');
el.type = 'text';
el.style.width = '40px';
el.name = 'txtseat' + iteration;
el.id = 'id_seats' + iteration;
cell4.appendChild(el);
var cell8 = row.insertCell(3);
var sel = document.createElement('select');
sel.name = 'txtfees' + iteration;
sel.id = 'id_elig' + iteration;
sel.options[0] = new Option('Specify Fees','');
sel.options[1] = new Option('1 - 5000','1 - 5000');
sel.options[2] = new Option('5001 - 10000','5001 - 10000');
sel.options[3] = new Option('10001 - 20000','10001 - 20000');
sel.options[4] = new Option('20001 - 30000','20001 - 30000');
sel.options[5] = new Option('30001 - 40000','30001 - 40000');
sel.options[6] = new Option('40001 - 50000','40001 - 50000');
sel.options[7] = new Option('50001 - 60000','50001 - 60000');
sel.options[8] = new Option('60001 - 70000','60001 - 70000');
sel.options[9] = new Option('70001 - 80000','70001 - 80000');
sel.options[10] = new Option('80001 - 90000','80001 - 90000');
sel.options[11] = new Option('90001 - 1 Lac','90001 - 1 Lac');
sel.options[12] = new Option('1 Lac - 1.5 Lac','1 Lac - 1.5 Lac');
sel.options[13] = new Option('1.5 Lac - 2 Lac','1.5 Lac - 2 Lac');
sel.options[14] = new Option('2.5 Lac - 3 Lac','2.5 Lac - 3 Lac');
sel.options[15] = new Option('3.5 Lac - 4 Lac','3.5 Lac - 4 Lac');
sel.options[16] = new Option('4.5 Lac - 5 Lac','4.5 Lac - 5 Lac');
sel.options[17] = new Option('5.5 Lac - 6 Lac','5.5 Lac - 6 Lac');
sel.options[18] = new Option('6.5 Lac - 7 Lac','6.5 Lac - 7 Lac');
sel.options[19] = new Option('7.5 Lac - 8 Lac','7.5 Lac - 8 Lac');
sel.options[20] = new Option('8.5 Lac - 9 Lac','8.5 Lac - 9 Lac');
sel.options[21] = new Option('9.5 Lac - 10 Lac','9.5 Lac - 10 Lac');
sel.options[22] = new Option('10.5 Lac - 11 Lac','10.5 Lac - 11 Lac');
sel.options[23] = new Option('11.5 Lac - 12 Lac','11.5 Lac - 12 Lac');
sel.options[24] = new Option('12.5 Lac - 13 Lac','12.5 Lac - 13 Lac');
sel.options[25] = new Option('13.5 Lac - 14 Lac','13.5 Lac - 14 Lac');
sel.options[26] = new Option('14.5 Lac - 15 Lac','14.5 Lac - 15 Lac');
sel.options[27] = new Option('15 Lac & Above','15 Lac & Above');
cell8.appendChild(sel);
}
function removeRow()
{
var tbl = document.getElementById('tbltest');
var lastRow = tbl.rows.length;
if (lastRow > 1) tbl.deleteRow(lastRow - 1);
}
这是我的php将数据插入mysql
<?php
$dbc = mysql_connect('localhost', 'root', 'ziadpager');
if (!$dbc)
{
die('could not connect: '.mysql_error());
}
mysql_select_db ('a360', $dbc);
if(isset($_POST['submit']))
{
$length = mysql_real_escape_string($_POST['len']);
//$institute_id= 1;
for($loop=0; $loop<=$length; $loop++)
{
$category = "txtcategory".$loop;
$course_name = "txtcourse_name".$loop;
$seat = "txtseat".$loop;
$fees = "txtfees".$loop;
$cat = mysql_real_escape_string($_POST[$category]);
$cou = mysql_real_escape_string($_POST[$course_name]);
$sea = mysql_real_escape_string($_POST[$seat]);
$fee = mysql_real_escape_string($_POST[$fees]);
$store = "INSERT INTO institute_courses (course_id, category, course_name, seats, fees) VALUES (NULL, '$cat', '$cou', '$sea', '$fee')";
$go = mysql_query($store);
}
}
&GT;
问题是当我在隐藏字段中的值为1时,我只能向mysql数据库插入两组值
<input type="hidden" name="len" value="1" /></div>
如果我将值增加到10
<input type="hidden" name="len" value="10" /></div>
如果我没有输入10组数据,我会收到此错误
注意:未定义的索引:txtcategory2 在C:\ Program Files \ Apache软件中 基金会\ APACHE2.2 \ htdocs目录\网站\ addcourses3_1.php 第18行
注意:未定义的索引: C:\ Program中的txtcourse_name2 Files \ Apache软件 基金会\ APACHE2.2 \ htdocs目录\网站\ addcourses3_1.php 第19行
注意:未定义的索引:txtseat2 in C:\ Program Files \ Apache Software 基金会\ APACHE2.2 \ htdocs目录\网站\ addcourses3_1.php 第20行
注意:未定义的索引:txtfees2 in C:\ Program Files \ Apache Software 基金会\ APACHE2.2 \ htdocs目录\网站\ addcourses3_1.php 在第21行
等......
请告知我做错了什么,用户应该能够在数据库中输入尽可能多的数据而不会出现任何错误。
请帮忙 提前谢谢
答案 0 :(得分:0)
问题是
$length = mysql_real_escape_string($_POST['len']);
//$institute_id= 1;
for($loop=0; $loop<=$length; $loop++)
您计算出循环的长度恰好是该隐藏字段的值。您需要以不同的方式计算长度,例如一段时间测试检查是否有数据,或者使用javascript更新字段。
除非我遗漏了某些内容,否则您的HTML代码只会发送一个选定的选项。如果是这样,你不想在数据库中写几个 - 因为你没有数据。
要传输几行,您必须:
(1)使用&lt; select ... multiple&gt;
(2)通过Javascript或数据发送后找到一些了解manay行的方法
(3)使用该数字代替长度。