数组不是从HTML发送到PHP(Array not being sent from HTML to PHP)

编程入门 行业动态 更新时间:2024-10-28 11:20:22
数组不是从HTML发送到PHP(Array not being sent from HTML to PHP)

我正在开展一个个人项目,让我可以记录我走过的所有苏格兰山脉(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

更多推荐

本文发布于:2023-08-04 16:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是从   数组   发送到   HTML   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!