1

It will create text box with name=id1,id2,id3..... on clicking the add option. But when I going to fetch the value, it will give an 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>
1
  • try isset function Commented Jul 17, 2015 at 12:49

2 Answers 2

1

Checked this question in my localhost, and used firebug.. Found that form get closed in table(form without dynamically created inputs,id2,id3 etc). Thats why POST values are not getting.Your script is absolutely fine. Try to 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>
Sign up to request clarification or add additional context in comments.

Comments

0

$_POST["id2"]; get value from input NAME not ID :)

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.