简体   繁体   中英

Adding Form tag to html elements created by innerHTML

I am dynamically creating html elements: a text box and a submit button which on submitting should go to a php code and place the data in a database.

But the php part is not working if anyone can tell where I'm going wrong

<html>
<head>
    <title>My first PHP page</title>
<script>
    function addsubdomain()
    {
        document.getElementById('subdomain').innerHTML  +="<form id='form1' method='post' action='new.php'> \n\
            <input type='text' id='textid' name='subdomain' value='' /><br /> \n\
            <input type='button' id='button2' name='submit2' value='submit'/></form>";
    }
</script>
</head>
<body>       
    <input type="button" id="button1 "name="submit1"  value="Add SubDomain" onclick="addsubdomain();"/>
    <div id="subdomain"></div>
     <?php
        if(isset($_POST['submit2']))
        {
            echo "hii";// Not working not coming here
            echo $_POST['subdomain'];
        // Insert Query
        }                      
     ?>
</body>
</html>

Currently in your javascript your dynamic button type is button hence it is not submitting the form. Hence, Change your type='button' to type='submit' in javascript. ie change it like this

document.getElementById('subdomain').innerHTML  +="<form id='form1' method='post' action='new.php'> <input type='text' id='textid' name='subdomain' value='' /><br /> <input type='submit' id='button2' name='submit2' value='submit'/></form>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM