PHP提交不发布JS生成的输入字段中的文本 [英] PHP submit does not post text from JS-generated input fields

查看:49
本文介绍了PHP提交不发布JS生成的输入字段中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html表单,其中包含文本输入表,最终将用于生成SQL INSERT查询.我的问题是我的手动输入表有5个文本输入,然后我为用户提供了添加更多内容的选项.这些额外的输入字段可以很好地创建,当我在添加元素后检查页面上的元素时,它们具有正确的名称,id,类型等.(试图发布屏幕截图以提供更好的主意,但我是新用户,所以不能-绕过这个吗?!)

I have a html form that contains a table of text inputs, ultimately to be used to generate an SQL INSERT query. The problem I have is my manually input table has 5 text inputs, then I have given the user the option to add more. These extra input fields are created fine and when I inspect the elements on the page after adding them, they have the correct name, id, types etc. (tried to post a screenshot to give a better idea but I'm a new user so can't - any way round this?!)

问题是,当我单击表单的提交"按钮时,只有前5个文本(我使用html标签手动输入的文本)才会发布.这是因为它在生成新输入之前先读取数据吗?我认为这不应该是因为我正在使用if(isset($ _ POST ['save']))){其中'save'是我的最终提交按钮.

The problem is only the text from the first 5 (the ones I manually entered using html tags) get posted when I click my form's submit button. Is this because it reads the data before the new inputs are generated? I feel that it shouldn't be because I'm using if (isset($_POST['save'])) { where 'save' is my final submit button.

我在html中获得的表如下(很抱歉道歉/语法混乱,但是在这种情况下它可以正常工作):

The table I've got in my html is as follows (apologies is poorly laid out/messy syntax, but its working fine in that sense):

edit:我在一个表单中有两个表;没关系吗?即:

edit: I have two tables within one form; that doesn't matter does it? i.e:

<form action='createtable.php' method='post' border='0'>

<table>
<tr>
<td><input type="button" value="Add column" onclick="addRowToTable();" /></td>
<td><input class='strong' type='submit' value='Create Table' name='save' /></td>
</tr>
</table>

<table name='columnnames' id='columnnames' border='0'>

<tr> <!--start table row 1-->
<td>Column 1:</td> <!--first item in row is just text-->
<td><input type='text' name='col1' id='col1' /></td> <!--second item in row is input text box-->
</tr> <!--end table row-->

<tr> <!--start table row 2-->
<td>Column 2:</td>
<td><input type='text' name='col2' id='col2' /></td>
</tr> <!--end table row-->

<tr> <!--start table row 3-->
<td>Column 3:</td>
<td><input type='text' name='col3' id='col3' /></td>
</tr> <!--end table row-->

<tr> <!--start table row 4-->
<td>Column 4:</td>
<td><input type='text' name='col4' id='col4' /></td>
</tr> <!--end table row-->

<tr> <!--start table row 5-->
<td>Column 5:</td>
<td><input type='text' name='col5' id='col5' /></td>
</tr> <!--end table row-->

</table>
</form>

我用来添加表格行的JavaScript如下:

The JavaScript I'm using to add the table rows is below:

<script>
function addRowToTable()
{
  //get table
  var tbl = document.getElementById('columnnames');
  //find length
  var numberRows = tbl.rows.length;

  // as there's no header row in the table, then iteration = numberRows + 1
  var rowNumber = numberRows + 1;
  var row = tbl.insertRow(numberRows);

  // left cell
  var cellLeft = row.insertCell(0);
  var textBox = document.createTextNode('Column ' + rowNumber + ':');
  cellLeft.appendChild(textBox);

  // right cell
  var cellRight = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'col' + rowNumber;
  el.id = 'col' + rowNumber;

  //el.onkeypress = keyPressTest;
  cellRight.appendChild(el);
}
</script>

我已经看到了一些与JS生成的表单有关的问题,这些问题不希望使用$ _POST正确提交,但是对这些问题的回答似乎很少,我想知道在我尝试读取数据时是否有人可以提供帮助小时.非常感谢!

I've seen a few questions relating to JS-generated bits of forms not wanting to submit properly using $_POST but answers to these seem fairly minimal and wondered if anyone could help as I've been trying to read the data for hours. Many thanks!

推荐答案

尽管这是一个非常老的问题,但我正在写答案,以防其他人再次遇到该问题.

Though it is a very old question, I am writing the answer in case someone else goes through it again.

PHP代码不会使用jquery从新添加的输入字段中获取值,因为它们都具有相同的名称属性.对于这些类型的动态输入字段,您需要将名称设置为数组<input type="text" name "text[]">.编写$text= $_POST['text']时,文本变量将以名称text[]存储输入字段中的所有值,并以数组形式存储它们.

The PHP code is not taking values from the newly added input fields with jquery, because they all have the same name attributes. For these kinds of dynamically input fields you need to set the name to an array <input type="text" name "text[]">. When you write, $text= $_POST['text'], the text variable will then store all the values from the input fields with the name text[] and store them in the form of an array.

希望它会有所帮助. :)

Hope it helps. :)

这篇关于PHP提交不发布JS生成的输入字段中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆