我正在开展一个个人项目,让我可以记录我走过的所有苏格兰山脉(Munros)。 我经常步行两次,每天3次,所以我需要一个动态的表格来控制有多少个字段。
我计划以4个数组存储关于山的数据,名称,高度,位置和最近的城镇。
当我将数据输入到表单中后,我点击提交并尝试在屏幕上显示名称以检查表单是否正常工作。
我似乎无法让阵列工作,虽然我确实有其他元素成功显示(例如加息的日期)。
下面是html
<form name="munroForm" action="process.php" class="register" method="POST"> <h1>Munro Data Entry Form</h1> <fieldset class="row1"> <legend>Trip Date</legend> <p> <label>Date of Trip*</label> <input type="date" required name="date"> </P> </fieldset> <fieldset class="row1"> <legend>Munro Details</legend> <table id="munroTable" class="form" border="1"> <tbody> <tr> <td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td> <td> <label>Name *</label> <input type="text" maxlength = "30" required="required" name="name[]"> </td> <td> <label>Height *</label> <input type="text" maxlength = "5" required="required" name="height[]"> </td> <td> <label>Location</label> <input type="text" maxlength = "20" name="location[]"> </td> <td> <label>Closest Town</label> <input type="text" maxlength = "15" name="closest_town[]"> </td> </tr> </tbody> </table> <p> <input type="button" value="Add Munro" onClick="addRow('munroTable')" /> <input type="button" value="Remove Munro" onClick="deleteRow('munroTable')" /> </p> <div class="clear"></div> </fieldset> <input class="submit" type="submit" value="send" />例如,我尝试了各种不同的php命令来尝试显示输入的数据
foreach ($_POST['name'] as $value) { // Do something with each valid friend entry ... if ($value) { echo $value."<br />"; } }我甚至试图显示数组的大小,看它是否存储了正确数量的数据。 这总是返回0。
echo "Number of munros =".sizeof($name);我不确定JavaScipt是否干扰了这一点,尽管我怀疑它。 但只要我把它粘贴在下面。
function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; if(rowCount < 5){ // limit the user from creating fields more than your limits var row = table.insertRow(rowCount); var colCount = table.rows[0].cells.length; for(var i=0; i<colCount; i++) { var newcell = row.insertCell(i); newcell.innerHTML = table.rows[0].cells[i].innerHTML; } }else{ alert("Maximum 5 Munros"); } } function deleteRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; for(var i=0; i<rowCount; i++) { var row = table.rows[i]; var chkbox = row.cells[0].childNodes[0]; if(null != chkbox && true == chkbox.checked) { if(rowCount <= 1) { // limit the user from removing all the fields alert("Cannot Remove all the Munros."); break; } table.deleteRow(i); rowCount--; i--; } } }这是我第一次编写PHP以外的一些小的自定义wordpress主题。 我在Mac上使用MAMP。
任何建议,将不胜感激。
I am working on a personal project that will allow me to log all the Scottish Mountains (munros) I have walked. I often walk two, or 3 a day so I needed a dynamic form to control how many fields to have.
I planned to store the data about the mountains in 4 arrays, Name, Height, Location and Closest Town.
When I enter the data into the form, I then hit submit and try to display the names on the screen to check that the form is working correctly.
I can't seem to get the arrays working although I do have other elements being displayed successfully (the date of the hike for example).
Below is the html
<form name="munroForm" action="process.php" class="register" method="POST"> <h1>Munro Data Entry Form</h1> <fieldset class="row1"> <legend>Trip Date</legend> <p> <label>Date of Trip*</label> <input type="date" required name="date"> </P> </fieldset> <fieldset class="row1"> <legend>Munro Details</legend> <table id="munroTable" class="form" border="1"> <tbody> <tr> <td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td> <td> <label>Name *</label> <input type="text" maxlength = "30" required="required" name="name[]"> </td> <td> <label>Height *</label> <input type="text" maxlength = "5" required="required" name="height[]"> </td> <td> <label>Location</label> <input type="text" maxlength = "20" name="location[]"> </td> <td> <label>Closest Town</label> <input type="text" maxlength = "15" name="closest_town[]"> </td> </tr> </tbody> </table> <p> <input type="button" value="Add Munro" onClick="addRow('munroTable')" /> <input type="button" value="Remove Munro" onClick="deleteRow('munroTable')" /> </p> <div class="clear"></div> </fieldset> <input class="submit" type="submit" value="send" />I have tried various different php commands to try and display the data entered, for example
foreach ($_POST['name'] as $value) { // Do something with each valid friend entry ... if ($value) { echo $value."<br />"; } }I have even attempted to display the size of the array to see if it stores the correct amount of data. This always returns 0.
echo "Number of munros =".sizeof($name);I am unsure if the JavaScipt is interfering with this, although I doubt it. But just incase I have pasted it below.
function addRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; if(rowCount < 5){ // limit the user from creating fields more than your limits var row = table.insertRow(rowCount); var colCount = table.rows[0].cells.length; for(var i=0; i<colCount; i++) { var newcell = row.insertCell(i); newcell.innerHTML = table.rows[0].cells[i].innerHTML; } }else{ alert("Maximum 5 Munros"); } } function deleteRow(tableID) { var table = document.getElementById(tableID); var rowCount = table.rows.length; for(var i=0; i<rowCount; i++) { var row = table.rows[i]; var chkbox = row.cells[0].childNodes[0]; if(null != chkbox && true == chkbox.checked) { if(rowCount <= 1) { // limit the user from removing all the fields alert("Cannot Remove all the Munros."); break; } table.deleteRow(i); rowCount--; i--; } } }This is my first time writing PHP outside of doing some small customisations to wordpress themes. I am using MAMP on mac.
Any suggestions would be appreciated.
最满意答案
尝试$_POST查看从表单传递的所有参数的列表
print_r($_POST)将打印所有值
try $_POST to see a list of all parameters that were passed from the form
print_r($_POST)Will print the full array of values
更多推荐
发布评论