PHP -当我提交按钮时,我如何保存文本框中的信息


PHP - how can i save the information from the textbox when i submit the button?

我正在构建一个小脚本,当我点击按钮时,脚本会自动添加一个文本框。

我的脚本工作得很好,但是当我提交按钮时,它清除了所有的文本框,我怎么能改变这一点,当我提交时保存所有的文本框信息?

<script src="jquery.js" type="text/javascript"></script> 
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript"><!--
$(document).ready(function(){
    var counter = 2;
    $("#add").click(function () {
        if(counter==11){
            alert("Too many boxes");
            return false;
        }   
        $("#textBoxes").html($("#textBoxes").html() + "<div id='d"+counter+"' ><label for='t2'> Textbox "+counter+"</label><input type='textbox' id='t"+counter+"' > </div>'n");
        ++counter;
    });
    $("#remove").click(function () {
        if(counter==1){
            alert("Can u see any boxes");
            return false;
        }   
        --counter;
        $("#d"+counter).remove();
    });
});
// -->
</script>
</head>
<body>
<div id='textBoxes'>
    <div id='d1'>
        <label for="t1">Textbox 1</label>
        <input type='textbox' id='t1' />
    </div>
</div>
<input type='button' value='add' id='add' />
<input type='button' value='remove' id='remove' />
<input type="textbox" name="textbox[]">
<input type="textbox" name="textbox[]">
<input type="textbox" name="textbox[]">
<input type="textbox" name="textbox[]">
<input type="textbox" name="textbox[]">
<?php
if( isset( $_REQUEST['textbox'] ) )
{
    foreach( $_REQUEST['textbox'] as $textbox )
    {
        // $textbox contains the input's text.
    }
}
?>

编辑:jQuery提供了一个你可以(应该)使用的函数。clone()

我认为你应该在输入中使用'name'属性。但是当你提交的时候会发生什么呢?是否有重定向到其他页面?还是要留在此页面上并显示确认消息?在本例中,您可以使用Ajax提交表单

<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    var counter = 2;
    $("#add").click(function () {
        if (counter==11) {
            alert("Too many boxes");
            return false;
        }   
        $("#textBoxes").append('<div id="d'+counter+'"><label for="'+counter+'"> Textbox "'+counter+'"</label><input type="textbox" id="t'+counter+"' ></div>'n");
        ++counter;
    });
    $("#remove").click(function () {
        if (counter == 1) {
            alert("Can u see any boxes");
            return false;
        }   
        --counter;
        $("#d"+counter).remove();
    });
});
</script>
</head>
<body>
    <form action="" method="post">
        <div id='textBoxes'>
            <div id='d1'>
                <label for="t1">Textbox 1</label>
                <input type='textbox' id='t1'>
            </div>
        </div>
        <input type='button' value='add' id='add' />
        <input type='button' value='remove' id='remove' />
    </form>
</body>
</html>