我有一个任务。我想在文本框中键入值时将数据插入数据库.... 即使我不知道是否有可能。这是可能的。 任何例子都将不胜感激。
提前致谢
答案 0 :(得分:1)
是的,可以这样做。这里的例子适合你,但是以PHP的方式。您可以使用此引用更改为JSP。
<强> HTML 强>
<input type="text" id="hey">
<强>的jQuery 强>
$('#hey').on('keyup', function(){
$.ajax({
type : 'POST',
url : 'insertData.php', // --> server side code to insert data into db
data : {
val : $(this).val()
},
success : function(msg){
// msg -> return by server side
// any code in success
// if success will print out this 'New record created successfully'
// if error will print out this 'Error occured'
console.log(msg);
}
});
});
Php - insertData.php
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "INSERT INTO comment (myText)
VALUES (".$_POST['val'].")";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error occured";
}
$conn->close();
?>