1

this is my code,when i click on add button new text fields are coming but total page getting refresh and existing data also refreshing, please help me.

<?php       
    if( !isset( $_POST['j'] ) ) {
        static $j = 1;
    } else {
        //j is a reference for $i in the loop. $i will loop while it is less than $j.
        if( isset( $_POST['plus'] ) ) {
           $j = $_POST['j']+1; //by incrementing $j, $i will loop one more time.
        }
        if( isset( $_POST['minus'] ) ) {
           if( @$j < 1 ) { //if there is only one box the user can't remove it
               $j = $_POST['j']-1;
           }
        }   
    }
?>
<?php echo '<input type="hidden" name="j" value="' . @$j . '"/>' ?>
<?php for( $i = 0; $i < @$j; $i++ ) { ?>
    <input id="txt_id" name="txt_id[]"  type="text" value="" readonly="readonly"/> 
<? } ?>
<input type ="submit" value="+" name="plus">
<input type ="submit" value="-" name="minus">
3
  • set the $j value in session and try it.or else use jquery to add dynamic row its very easy. Commented Dec 20, 2012 at 6:58
  • If you want to avoid page refresh you can try ajax and add textbox fields Commented Dec 20, 2012 at 6:58
  • Avoid using error suppression @. Just check if $j is set using PHP isset() Commented Dec 20, 2012 at 7:05

3 Answers 3

2

Thats because you need to set the value for the textfields being generated.

Try replacing your for loop with this:

<?php for( $i = 0; $i < @$j; $i++ ) { ?>
      <input id="txt_id" name="txt_id[]"  type="text" value="<?php echo $_POST['txt_id'][$i];?>" readonly="readonly" />
<? } ?>
Sign up to request clarification or add additional context in comments.

2 Comments

I try Your code but it was not work,pls try to give better soulation
Thank for your help,Your work is appereciated,But I am get the error given below Thanx in advance (<br /><b>Notice</b>: Undefined index: txt_id in <b>E:\xampp\htdocs\Doctors\example2.php</b> on line <b>234</b><br />)
1

I have posted a code which is implemented on server. So tried this code on your local server. It will work and help you to find out your solution.

<html>

    <head>
        <title> Add/Remove dynamic rows in HTML table </title>
        <script language="javascript">

            function addRow(tableID) {
                var numb = document.getElementById("numb").value;
                for($i=1;$i<=numb;$i++) {
                    var table = document.getElementById(tableID);

                    var rowCount = table.rows.length;
                    var row = table.insertRow(rowCount);

                    var cell1 = row.insertCell(0);
                    var element1 = document.createElement("input");
                    element1.type = "checkbox";
                    cell1.appendChild(element1)[$i];

                    var cell2 = row.insertCell(1);
                    cell2.innerHTML = rowCount + 1;

                    var cell3 = row.insertCell(2);
                    var element2 = document.createElement("input");
                    element2.type = "text";
                    element2.name = "firstname["+rowCount+"]";
                    cell3.appendChild(element2)[$i];

                    var cell4 = row.insertCell(3);
                    var element3 = document.createElement("input");
                    element3.type = "text";
                    element3.name = "lastname["+rowCount+"]";
                    cell4.appendChild(element3)[$i];

                    var cell5 = row.insertCell(3);
                    var element4 = document.createElement("input");
                    element4.type = "text";
                    element4.name = "town["+rowCount+"]";
                    cell5.appendChild(element4)[$i];

                    var cell6 = row.insertCell(3);
                    var element5 = document.createElement("input");
                    element5.type = "text";
                    element5.name = "state["+rowCount+"]";
                    cell6.appendChild(element5)[$i];
                }

            }
        </script>
    </head>
    <body>
     <form action="#" method="post">
        <input type="button" value="Add Row" onclick="addRow('dataTable')" />
        <input type="text" name="numb" id="numb" value="" />
        <input type="submit" name="submit" value="submit" />
        <table id="dataTable" width="350px" border="1">

        </table>
        </form>
    </body>
    </html>

Comments

0

http://viralpatel.net/blogs/dynamic-add-textbox-input-button-radio-element-html-javascript/

http://jpsolution.wordpress.com/tag/add-text-box-dynamically-javascript/

hope these sites may be useful

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.