gpt4 book ai didi

php - 将Form标签添加到innerHTML创建的html元素中

转载 作者:行者123 更新时间:2023-11-28 20:30:00 26 4
gpt4 key购买 nike

我正在动态创建 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>

最佳答案

目前在您的 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>";

关于php - 将Form标签添加到innerHTML创建的html元素中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16605677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com