0

我正在动态创建 html 元素:一个文本框和一个提交按钮,提交时应该转到 php 代码并将数据放入数据库中。

但是如果有人能告诉我哪里出错了,那么 php 部分就不起作用

<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>
4

1 回答 1

3

目前在您的 javascript 中,您的动态按钮类型是按钮,因此它没有提交表单。因此,在 javascript 中将 type='button' 更改为 type='submit'。即像这样改变它

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>";
于 2013-05-17T09:30:57.970 回答