Skip to content

Instantly share code, notes, and snippets.

@arn-ob
Created July 19, 2017 17:03
Show Gist options
  • Save arn-ob/cceb9d23a69437635e71ebf6370dbb23 to your computer and use it in GitHub Desktop.
Save arn-ob/cceb9d23a69437635e71ebf6370dbb23 to your computer and use it in GitHub Desktop.
Ajax code Data get from HTML Input
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "try";
$name = $_POST["name"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO one (name) VALUES ('$name')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<title>PHP MVC Frameworks - Search Engine</title>
<script>
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
var userInput = document.getElementById("name").value ;
var text ='{"name":"'+userInput+'"}';
var obj = JSON.parse(text);
$.ajax({
type: 'post',
url: 'InData.php',
//data: $('form').serialize(),
data : obj,
success: function () {
alert('form was submitted');
}
});
});
});
</script>
</head>
<body>
<h2>Ajax Data Insert to DB</h2>
<p><b>At last i did it </b></p>
<form>
<input type="text" id="name" name="name" ><br>
<input name="submit" type="submit" value="Submit">
</form>
<p>Matches: <span id="txtName"></span></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment