0

In Sample.php I have

<html>
<head>
<title>Test</title>
</head>

<script language="javascript">
    var count = 1;

function addRow(divId) {



    var parentDiv = document.getElementById(divId);

    var eleDiv = document.createElement("div");
    eleDiv.setAttribute("name", "olddiv");
    eleDiv.setAttribute("id", "olddiv");

    var eleText = document.createElement("input");
    eleText.setAttribute("type", "text");
    eleText.setAttribute("name", 'textbox' + count);
    eleText.setAttribute("id", "textbox" + count);

    var eleBtn = document.createElement("input");
    eleBtn.setAttribute("type", "button");
    eleBtn.setAttribute("value", "delete");
    eleBtn.setAttribute("name", "button");
    eleBtn.setAttribute("id", "button");
    eleBtn.setAttribute("onclick", "deleteRow('button')");

    parentDiv.appendChild(eleDiv);

    eleDiv.appendChild(eleText);
    eleDiv.appendChild(eleBtn);

    var golu=count.toString();
    document.getElementById("h").value= golu;
    count++;

}

function deleteRow(tableID) {
        var div = document.getElementById('olddiv');
        if (div) {
            div.parentNode.removeChild(div);
        }
        var golu=count.toString();
    document.getElementById("h").value= golu;
}

var golu=count.toString();
    document.getElementById("h").value= golu;



</script>
<body>
<form action="Page.php" method="post">
<input type="button" value="Add Row" onclick="addRow('dataTable')" />
<div id="dataTable"><INPUT type="text" name="textbox0" id="textbox0"/></div>
 <input type"hidden" id="h" name="h" value="0"/>
 <button  type="submit" name="submit" id="submit">Submit</button>
</form>

</body>
</html>

In Page.php I have

<?php
 include_once('db.php');

 $x=$_POST["h"];
 $y=intval($x);
 $z=0;
 while($z<=$y){
 $var[z]=$_POST['textbox'][$z];
 echo "$var[$z]";
 $sql="INSERT into Data values('".$var[z]."');";

 $query = mysql_query($sql);
 }
?>

My problems:

  1. My aim is to receive all the value of textbox0, textbox1,..etc. by using loop in page.php.

    But I am not able to get the desired result. Actually after submitting, Pape.php appears empty. After receiving all, I also want them to store in MySQL Database.

  2. Whenever I delete a textbox the sequence of the textboxes' id doesn't change.

Please tell me what to do.

3
  • you might want to resolve all the syntax errors first. maybe the problem will solve itself then. hint: turn on error reporting. Commented Jul 26, 2014 at 12:47
  • ya sure i did them....plz tell after that Commented Jul 26, 2014 at 13:14
  • Regarding your now deleted question, please make an answer if you solve a problem yourself, rather than just deleting it. This can be considered a sort of 'payment' for asking a question, and might possibly contribute something useful to the community. Commented Jul 29, 2014 at 16:44

1 Answer 1

2

You can do this following way.

Whenever you create textbox using JavaScript or jQuery, the maintain the count of the text box, suppose you have two textbox by default on the HTML, so store that count into the hidden field like you did that:

<input type"hidden" id="h" name="h" value="0"/>

Then try this, you are reading the value in wrong way:

Instead of using $var[z]=$_POST['textbox'][$z]; use $var[z]=$_POST['textbox'.$z];.

I think instead of editing each textbox id value, just remove it from HTML and check into the PHP code:

    <?php
     include_once('db.php');

     $x=$_POST["h"];
     $y=intval($x);
     $z=0;
     while($z<=$y){
            if(isset($_POST['textbox'.$z]) && !empty($_POST['textbox'.$z])){
                     $var[z]=$_POST['textbox'.$z];
                     echo "$var[$z]";
                     $sql="INSERT into the Data values('".$var[z]."');";
                     $query=mysql_query($sql);
            }
     }
    ?>

Another way, to solve both of your problems :)

test.html:

<html>
    <title>TEST</title>
    <body>
        <form action="test.php" method="post">
        <input type="text" name="demo[]" value=""/>
        <input type="text" name="demo[]" value=""/>
        <input type="text" name="demo[]" value=""/>
        <input type="text" name="demo[]" value=""/>
        <input type="text" name="demo[]" value=""/>
        <input type="submit">
        </form>
    </body>
</html>

test.php:

print_r($_POST);
exit;

output:

Array ( [demo] => Array ( [0] => zxc [1] => zxc [2] => ewe [3] => ecc [4] => zzx ) )
Sign up to request clarification or add additional context in comments.

5 Comments

actually my half problem is solved but yet the sequence of textbox is not altering.
@VictoriousLight any luck??
nope brother...........still trying to resolve.....the code above not working. if you have another solution please send me the codes...
I Got the Sequence Problem Solution.
Sidenote: Your answer contains $sql="INERT into which should read as $sql="INSERT into; typo. Missing an S.

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.