The problem is: My motive is to get the subject name from the admin and insert it into database. Then i want to give the result of my query onto the same web page without refreshing it as the ajax should do. The following code is giving wrong o/p. I tried many syntaxes but still wrong response from the code. Please tell me the correct way.
My HTML code is:
<form>
<input type="text" name="subject" />
<div id="subject"></div>
<input type="button" value="Submit" onclick="addSubject()" />
<script src="home.js">
</script>
</form>
My home.js code is:
function addSubject() {
a=document.getElementsByName("subject")[0].value;
destination="ajaxstoresubject.php?a:"+a+";";
var xhr=new XMLHttpRequest();
xhr.open("GET",destination,true);
xhr.send();
xhr.onreadystatechange=function() {
if(xhr.readyState==4 && xhr.status==200) {
document.getElementById("Subject").innerHTML=xhr.responseText;
}
}
}
ajaxstoresubject.php's code is:
<?php
$subject=$_GET["a"];
$con=mysqli_connect("localhost","root","","doortolearn");
if(!$con){
echo("No database connection.");
}
$query="insert into subjects(sid) values('$subject')";
$result=mysqli_query($con,$query);
if($result){
echo("Subject successfully inserted");
}
else{
echo("Subject could not be inserted. Might be, the subject already exists.");
}
?>
The problem is: It inserts an empty value into the sid column of subject table. I checked, sid is varchar still the problem. It is also not giving any output on the web Page.
Please don't suggest JQUERY. I don't know anything about that.