繁体   English   中英

动态添加输入元素以形成并将值存储在数据库中

[英]Adding input element dynamically to form and storing the value in database

我必须创建一个创建按钮的表单,然后单击该按钮,将显示一个动态文本字段。

我的JavaScript对此是:

function add() {

    //Create an input type dynamically.
    var element = document.createElement("input");
    element.style.borderRadius ="5px";
    //Assign different attributes to the element.
    element.setAttribute("type", "text");
    element.setAttribute("value", "ip2");
    element.setAttribute("name", "ip2");
    element.setAttribute("placeholder", "ip2"); 
    element.setAttribute("required", "true");
    var foo = document.getElementById("fooBar");

    //Append the element in page (in span).
    foo.appendChild(element);

}

并且我为按钮和所需的容器使用以下代码:

<form method="post" action="insert.php">
<INPUT type="button" value="Add" onclick="add(document.forms[0].element.value)"/>

<span id="fooBar"></span>
</form>

输入字段很好。 我只想知道如何从动态输入字段中获取价值并将其存储在数据库中。

提前致谢

一旦将字段添加到fooBar,它就在表单内部。 如果您通过“提交”按钮(请参见下面的HTML代码)或通过javascript document.getElementById("myForm").submit(); 您必须使用服务器上的脚本(例如PHP)来处理发送到服务器的结果。

echo '<pre>';
print_r( $_POST ); /* Show ALL given $_POST input */
echo '</pre>';
error_reporting(0);
$conn = mysqli_connect("localhost","root","","pnb");
if( !$conn ) {
    die('Could not connect to MySQL: ' . mysqli_error() );
}

if( isset( $_POST['ip2'] ) AND !empty( $_POST['ip2'] ) ) {
    $ip2 = $_POST['ip2'];
    mysqli_query( $conn, 'INSERT INTO records (ip) VALUES ("' . mysqli_real_escape_string( $conn, $ip2 ) . '")' );
    echo 'Row added!';
}
mysqli_close( $conn );

HTML代码:

<form method="post" action="insert.php">
    <input type="button" value="Add" onclick="add()"/>
    <input type="submit" value="submit" />
    <span id="fooBar"></span>
</form>
<script type="text/javascript">
function add() {

    //Create an input type dynamically.
    var element = document.createElement("input");
    element.style.borderRadius ="5px";
    //Assign different attributes to the element.
    element.setAttribute("type", "text");
    element.setAttribute("value", "ip2");
    element.setAttribute("name", "ip2");
    element.setAttribute("placeholder", "ip2"); 
    element.setAttribute("required", "true");
    var foo = document.getElementById("fooBar");

    //Append the element in page (in span).
    foo.appendChild(element);
}
</script>

注意: document.forms[0].element.value不存在,因此我从上面的代码中将其删除。 我还添加了一个提交按钮,以将表单提交到服务器上的insert.php脚本。

上面的脚本将检查是否收到名称为“ ip2”的邮政包裹。 然后它将建立与数据库的MySQLi连接,并将值插入表“ tablename”中。 上面的代码应该放在insert.php

PHP:

if(isset($_POST['foo'])) {

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$item = $_POST['foo']

$sql = "INSERT INTO thetable (item)
VALUES ('$item')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

}
?>

HTML:

<form action="" method="post">
    <input type="text" name="foo" value="foo">
    <input type="submit" value="Submit">
</form>

另外,如果您要定位外部文件,则应将表单上的action attr更新为文件的url,例如action="target.php"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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