0

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.

Rahul Goel
  • 27
  • 1
  • 4
  • Presumably any suggestion we make is going to be something 'you don't know about' - otherwise you wouldn't have asked! – Strawberry Jun 08 '14 at 11:16
  • Please solve my this problem, you may vote it -100. I m too barehanded to solve this problem from last five days. :( @Strawberry I shall remember you throughout my life. – Rahul Goel Jun 08 '14 at 11:19
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jun 08 '14 at 11:29
  • I m not covering SQL injections. :( Please solve the exact problem except of voting negative :( @Quentin – Rahul Goel Jun 08 '14 at 11:32
  • @RahulGoel — Moaning when people look at your code, decide that they don't have the time to debug it, but helpfully point out a serious security problem is not a good way to endear yourself to the community. – Quentin Jun 08 '14 at 14:44

2 Answers2

1

Check the correct format for GET query string. Try changing destination="ajaxstoresubject.php?a:"+a+";"; to destination="ajaxstoresubject.php?a="+a;

Dariusz
  • 371
  • 1
  • 9
  • Thank you for the answer but it is still the same. Please give me other answer. I think you can solve this problem – Rahul Goel Jun 08 '14 at 11:29
-2

Its All works fine here just now I checked your code every thing works fine.

  1. Most importantly Try to work with XAMPP server because some times I also download free projects and when I try to run that project using WAMP server, its not working, but XAMPP start the in real quick way, WAMP always want updates, but I will not perfer to you work with WAMP on local server...

    Start work with XAMPP its really nice and user friendly too.

  2. Decalre your datastring properly just now I noticed your datastring not declared properly it should be decalre like this:

     destination="ajaxstoresubject.php? a="+a;
    
  3. I also run this code on my local server and its work perfectly with JavaScript, just concentrate on your double quotes, commas, camel letters while declaring...

Community
  • 1
  • 1
Anuj
  • 13
  • 3