Skip to content

Instantly share code, notes, and snippets.

@jameshibbard
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameshibbard/5c9a9b5ec187227267ca to your computer and use it in GitHub Desktop.
Save jameshibbard/5c9a9b5ec187227267ca to your computer and use it in GitHub Desktop.
Submit whatever value is entered into text field via AJAX
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AJAX demo</title>
</head>
<body>
<input type="text" id="myTextField" />
<button id="ajax">Submit AJAX request</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$("#ajax").on("click", function(){
var text = $("#myTextField").text();
$.ajax({
type: "POST",
url: "submit.php",
cache: false,
data: { "value" : text },
success: function(response){
alert("The server says: " + response);
},
error: function(response){
alert ("Ajax Error");
console.log(response);
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment