【问题标题】:Adding input element dynamically to form and storing the value in database动态添加输入元素以形成并将值存储在数据库中
【发布时间】:2015-10-16 21:23:46
【问题描述】:

我必须制作一个创建按钮的表单,单击该按钮会出现一个动态文本字段。

我的 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>

输入框没问题。我只想知道如何从动态输入字段中取值并将其存储在数据库中。

提前致谢

【问题讨论】:

  • 您可以简单地提交表单并在后端使用 php 以通过 name attr 定位值,或者您可以使用 js 通过 classid 获取值并执行阿贾克斯请求。你有工作 db conn 的代码吗?
  • 将其存储在数据库中?您的意思是将其发送到服务器并存储值?你需要一些后端语言,比如 PHP
  • 是的,我有后端 php 文件用于接收,但问题是我没有使用 post 方法通过名称 attr 获取值

标签: javascript php jquery html forms


【解决方案1】:

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"

【讨论】:

    【解决方案2】:

    一旦将字段添加到 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”的post包。它将建立一个到数据库的 MySQLi 连接并将值插入表“tablename”中。以上代码应放在insert.php

    【讨论】:

    • insert.php 文件类似于:records(ip) VALUES ('$ip2'); mysqli_query($conn,$query);
    • 您放在评论中的代码是打开一个 MySQLi 连接,而不是关闭一个 MySQL 连接!?我认为这是不可能的。我将编辑我对此代码的回答。
    • 连接很好,唯一的问题是以下部分代码不起作用:$ip2 = $_POST['ip2'];
    • 检查 $_POST['ip2'] 中是否有值。将print_r( $_POST ); 添加到脚本顶部并查看['ip2'] 是否存在并具有值。如果不是,则问题出在 HTML 代码/提交表单中。
    • 你能详细说明什么类型的问题!!
    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    相关资源
    最近更新 更多