如何在php中将动态创建的文本框值发布到下一页?
我是php、jquery和ajax的新手。我在jquery中有一个代码,可以在单击按钮时动态添加和删除文本框。我想将动态创建的文本框值发布到下一页,并在下一页中显示这些值。非常感谢您的帮助,谢谢 我已经在下面粘贴了代码 你也可以查看链接如何在php中将动态创建的文本框值发布到下一页?,php,jquery,Php,Jquery,我是php、jquery和ajax的新手。我在jquery中有一个代码,可以在单击按钮时动态添加和删除文本框。我想将动态创建的文本框值发布到下一页,并在下一页中显示这些值。非常感谢您的帮助,谢谢 我已经在下面粘贴了代码 你也可以查看链接 $(文档).ready(函数(){ var计数器=1; $(“#添加按钮”)。单击(函数(){ 如果(计数器>7){ 警报(“仅允许7个文本框”); 返回false; } var newTextBoxDiv=$(document.createElement
$(文档).ready(函数(){
var计数器=1;
$(“#添加按钮”)。单击(函数(){
如果(计数器>7){
警报(“仅允许7个文本框”);
返回false;
}
var newTextBoxDiv=$(document.createElement('div'))
.attr(“id”,“TextBoxDiv”+计数器);
newTextBoxDiv.after().html('Product#'+counter+':'+
“\n\
数量#'+计数器+':'+
“\n\
速率#'+计数器+:'+
“\n\
总数#'+计数器+':'+
' ');
newTextBoxDiv.appendTo(“#textboxsgroup”);
计数器++;
});
$(“#移除按钮”)。单击(函数(){
如果(计数器==0){
警报(“不再需要删除文本框”);
返回false;
}
计数器--;
$(“#TextBoxDiv”+计数器).remove();
});
您需要使用$\u POST superglobal,并使用HTML元素的id
考虑这一点:
<form action="foobar.php" method="post">
<input type="text" id="an_element" name="an_element" />
</form>
因此,以您的rates
文本框为例,您可能希望执行以下操作:
<?php
echo $_POST['rates'];
?>
当您动态创建文本框时,我假设您的文本框名称是动态的,我在这里看不到
$request = array(#field names#) // comma separated
$requestJson = json_encode($request);
通过
在下一页中,即在操作页中,接收隐藏字段并对其进行解码
$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.
for($i=0;$i
更新-根据评论中的最新问题
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');
var queryar=[];
newTextBoxDiv.appendTo(“#textboxsgroup”);//在此行之后添加
queryar.push(产品“+计数器+”);//在控制台中发出警报或打印,以便在单击按钮时检查数组值。
$(“#TextBoxDiv”+计数器).remove();//在此添加之后
var removietem=产品“+计数器+”/#删除的id#;
y=jQuery.grep(queryar,函数(值){
返回值!=removeItem;
});
$('').attr({
键入:“隐藏”,
id:‘foo’,
名称:'酒吧',
值:queryar
}).附录(“形式”);
感谢您的快速响应。在代码中,文本框名称是动态生成的,我的意思是每次用户按下“添加”按钮时,计数器值都会增加,id和名称也会用该值更新。name=“product'+counter+'”id=“product'+counter+'”。我希望我遵循的方法可以创建动态文本框名称。如果错误,请建议我。您是正确的,因此当用户每次单击“添加”按钮时,您将字段添加到数组中,并传递到隐藏字段,然后继续将名称附加到数组中。删除后,您将从数组中删除字段…因此,每次都可以看到文本框你的数组将包含该名称。我的意思是你告诉我将文本框名称添加到数组中。你能给我一个如何将字段添加到数组中的示例吗?谢谢你的帮助。var queryArr=[];下面是newTextBoxDiv add queryArr.push(#你的字段名称#);当用户单击“删除”时,获取id并删除数组中的特定值。非常感谢您的支持。
$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');