I have 2 input fields "User" and "Comment" and I want the user input to be saved asynchronously using AJAX, so no refreshing. So far I've gotten it to add a new row to the DB, but for some reason it is empty. I believe the reason is I am not appending the values correctly.
HTML(JS is in-between head tags):
<p>User: <input type="text" id="userName" /></p>
<p>Comment : <input type="text" id="comment" /></p>
<input type="button" value="Submit" onclick="callServer();" />
JS:
function callServer(){
var usr = document.getElementById("user").value;
var cmnt = document.getElementById("comment").value;
var ajaxRequest = XMLHttpRequest();
ajaxRequest.open("POST", "insert.php", true);
ajaxRequest.send(null);
}
PHP:
<?php
// Setting variables for the elements
$user = $_POST['user'];
$comment = $_POST['comment'];
// Establishing connection and selecting db
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db('local',$con);
// Doing the query for the insert
$query = mysql_query("INSERT INTO content (Title, Article)
VALUES('$user', '$comment')");
mysql_query($query, $con);
mysql_close($con);
?>