Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Presumably any suggestion we make is going to be something 'you don't know about' - otherwise you wouldn't have asked! –  Strawberry Jun 8 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 8 at 11:19
    
Danger: You are vulnerable to SQL injection attacks that you need to defend yourself from. –  Quentin Jun 8 at 11:29
    
I m not covering SQL injections. :( Please solve the exact problem except of voting negative :( @Quentin –  Rahul Goel Jun 8 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 8 at 14:44

2 Answers 2

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

share|improve this answer
    
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 8 at 11:29

I check your code, but there is no problem please check $_GET prior to insert using print_r($_GET);

//html  
<form>
  <input type="text" name="subject"/>
  <div id="subject"></div>
  <input type="button" value="Submit" onclick="addSubject()" />
</form>

 //javascript
<script type="text/javascript">

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;
    }
   }
 }
</script>

// ajaxstoresubject.php
<?php
$subject=$_GET["a"];
print_r($subject);
//die;
$con=mysqli_connect("localhost","root","","phptesting");
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.");
 }
?>
share|improve this answer
    
but this actually does nothing after tries :( –  Rahul Goel Jun 10 at 16:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.