如何张贴动态创建的文本框值下一页在php


How to post the dynamically created text box values to next page in php?

我是php,jquery和ajax的新手。我有一个代码在jquery添加和删除文本框上按钮点击动态。我想张贴动态创建的文本框值到下一页的值,并在下一页显示这些值。非常感谢任何帮助。提前谢谢。

我已经粘贴了下面的代码…

您也可以查看链接http://jsfiddle.net/NSePb/1/

    $(document).ready(function () {
            var counter = 1;
            $("#addButton").click(function () {
            if(counter>7){
                alert("Only 7 textboxes allow");
                return false;
                }   
        var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'TextBoxDiv' + counter);
        newTextBoxDiv.after().html('<label>Product #'+ counter + ' : </label>' +
      '<input type="text" size="22" name="product' + counter + 
      '" id="product' + counter + '" value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'n'
          <label>Quantity #'+ counter + ' : </label>' +
      '<input type="text" size="1" name="qty' + counter + 
      '" id="qty' + counter + '" value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'n'
          <label>Rate #'+ counter + ' : </label>' +
      '<input type="text" size="2" name="rates' + counter + 
      '" id="rates' + counter + '" value="" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'n'
          <label>Total #'+ counter + ' : </label>' +
      '<input type="text" size="3" name="total' + counter + 
      '" id="total' + counter + '" value="" > ');
        newTextBoxDiv.appendTo("#TextBoxesGroup");

        counter++;
        });
        $("#removeButton").click(function () {
        if(counter==0){
        alert("No more textbox to remove");
        return false;
        }   
        counter--;
        $("#TextBoxDiv" + counter).remove();
        });

当你动态创建文本框时,我假设你的文本框名称是动态的,我在这里看不到。因此,将动态字段名存储为数组

$request = array(#field names#) // comma separated
$requestJson = json_encode($request);
通过

$requestJson as an hidden field in the form and submit the form.

在下一页,即在操作页接收隐藏字段并解码

$requestArr = json_decode($_REQUEST['requestFields']);

循环数组并接收值

for($i=0; $i < count($requestArr); $i++)
{
        $value = $requestArr[$i]; // get the field name
        $content = $_REQUEST[$value]; // get the input value
            $fetchedResult = array($content); // Store the text field value 
}
Now your $fetchedResult will have the values to your future usage.

已更新-根据评论中的最新问题

var queryArr = [];
newTextBoxDiv.appendTo("#TextBoxesGroup"); // after this line add 
queryArr.push(product' + counter + '); // alert OR print in console to check the array values as and when the button is clicked.
$("#TextBoxDiv" + counter).remove(); //after this add
var removeItem = product' + counter + ' // #removed id#;
y = jQuery.grep(queryArr, function(value) {
  return value != removeItem;
});
$('<input>').attr({
    type: 'hidden',
    id: 'foo',
    name: 'bar',
    value : queryArr
}).appendTo('form');

您需要使用$_POST超全局变量,并使用HTML元素的id。

考虑一下:

<form action="foobar.php" method="post">
<input type="text" id="an_element" name="an_element" />
</form>

如果你想用PHP显示这个,你可以去到action属性所指向的页面(在我们的例子中是foobar.php),并使用$_POST超全局变量。

所以PHP代码应该是这样的:
<?php
echo $_POST['an_element'];
?>
因此,以rates文本框为例,您可能需要这样做:
<?php
echo $_POST['rates'];
?>

请注意,如果您将action属性设置为get,则需要使用$_GET而不是$_POST

另外,看看你的代码,你在你的HTML中缺少form元素。而不是使用$(document.createElement('div')) .attr("id", 'TextBoxDiv' + counter);,你可能想用这个:

$(document.createElement('form'))
.attr({
id: 'TextBoxDiv' + counter,
action: 'foobar.php',
method: 'post'
});