简体   繁体   中英

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

I have to make a form which creates a button, clicking upon which a dynamic text field appears.

My javascript for this is:

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);

}

and I am using following code for the button and required container:

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

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

Input field is fine. I only want to know how to take value from the dynamic input field and store it in the db.

Thanks in advance

Once the fiels is addedd to fooBar, it is inside the form. If you submit the form via a submit button (see below HTML code) or via javascript document.getElementById("myForm").submit(); you have to handle the results sent to the server with a script on the server, eg with 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 code:

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

Note: document.forms[0].element.value does not exist, hence I removed it from the code above. Also I added a submit button, to submit the form to the insert.php script on the server.

The above script will check if a post package with the name "ip2" is received. It than will setup a MySQLi connection to a database and insert the value in table "tablename". The above code should be placed in 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>

Also if you are targeting an external file you should update the action attr on the form to the url of the file eg action="target.php"

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