javascript - Retrieving value from dynamically created text box in php? -


it create text box name=id1,id2,id3..... on clicking add option. when going fetch value, give error " undefined index: id2"

<script> var i=1; function myfunction() {     i++;     var x = document.createelement("input");     x.setattribute("type", "text");     x.setattribute("name", "id"+i);     elem = document.getelementbyid("hide_specific");    elem.appendchild(x);  } </script>   <body> <?php  $submission=""; if($_server["request_method"]=="post"){$submission=$_post["id2"];}  ?>  <table> <form action="" method="post">  <tr> <td> <div  id="hide_specific"><input type="text" name="id1" ><span       onclick="myfunction()">add</span></div>  </td> </tr>  <tr> <td><input type="submit" name="set" value="set"></td></tr> <tr><td> <?php echo $submission; ?></td></tr>  </form> </table> </body> 

checked question in localhost, , used firebug.. found form closed in table(form without dynamically created inputs,id2,id3 etc). thats why post values not getting.your script absolutely fine. try add form tag before table. code shown below

    <body> <?php $submission=""; if($_server["request_method"]=="post"){$submission=$_post["id2"];} ?> <form action="" method="post">  <!-- ######### open form tag before table --> <table>           <tr>             <td>                 <div  id="hide_specific"><input type="text" name="id1" ><span       onclick="myfunction()">add</span></div>              </td>         </tr>          <tr> <td><input type="submit" name="set" value="set"/></td></tr>         <tr><td>                 <?php echo $submission; ?></td></tr>   </table> </form> </body> 

Comments