insert data into database using ajax, javascript and php -


the problem is: motive subject name admin , insert database. want give result of query onto same web page without refreshing ajax should do. following code giving wrong o/p. tried many syntaxes still wrong response code. please tell me 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 subjects(sid) values('$subject')"; $result=mysqli_query($con,$query); if($result){     echo("subject inserted"); } else{     echo("subject not inserted. might be, subject exists."); } ?> 

the problem is: inserts empty value sid column of subject table. checked, sid varchar still problem. not giving output on web page.

please don't suggest jquery. don't know that.

check correct format query string. try changing destination="ajaxstoresubject.php?a:"+a+";"; destination="ajaxstoresubject.php?a="+a;


Comments

Popular posts from this blog

.htaccess - htaccess convert request to clean url and add slash at the end of the url -

php - facebook and github login HWIOAuthBundle and FOSUserBundle in Symfony2.1 -

hadoop - Sqoop installation export and import from postgresql -