0

I have a button which has attribute onclick to start javascipt function and my javascript ajax script has xmlhttp object. It is communicating with dbaction.php

When I click button everything is okay about script and mysql but the problem is about rows. It is inserting TWO same row for each click until it rowCount>0 and when after first click echo prints "0" for rowCount(), then if I click again now echo prints "2" for rowCount()

Where is the problem?

enter image description here

<body>

<script type="text/javascript">

function loadName() {
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
        {
            document.getElementById("sample_div").innerHTML = xmlhttp.responseText;

            $("#my_button").attr("value", "Sent");
            $("#my_button").attr("style", "color:red;");
        }
    };
    xmlhttp.open("GET", "dbaction.php?sender_id=5&receiver_id=6", true);
    xmlhttp.send();
}
  </script>

<input type="submit" id="buton" onclick="loadName();" value="Submit"/>
 <div id="sample_div" style="background-color: khaki; width:50%; margin: 0 auto;">     
 </div> 
 </body>

dbaction.php

<?php
//Database connection using PHP PDO(php database objects)
try {
$db = new PDO("mysql:dbname=member;host=localhost", "root", "",    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
 } 
catch ( PDOException $e ) {
echo $e->getMessage();
}

$sql= "SELECT * FROM friendship WHERE sender_id=".$_GET["sender_id"]." AND    receiver_id=".$_GET["sender_id"]."";
$rs=$db->prepare($sql);
$rs->execute();
$rs->fetch();
$count = $rs->rowCount();

if ($count>0){
 echo "You already sent request...";
 echo $count;
}
else {
$query = $db->query("INSERT INTO friendship (sender_id, receiver_id, is_approved) VALUES (".$_GET["sender_id"].",".$_GET["receiver_id"].",0)" );
$query->execute();

echo "Request sent";
echo $count;
}
4
  • check your console panel and see if only one request is sent , i think loadName() is calling twice Commented Jun 25, 2013 at 18:35
  • what do you mean about console panel? how can i monitorize it? Commented Jun 25, 2013 at 18:36
  • browser's console panel in firefox there is a addon firebug install it and check Commented Jun 25, 2013 at 18:39
  • if you are using Google Chrome then press f12 (It will open Developer tools) before clicking on the input and click on Network tab. Commented Jun 25, 2013 at 18:51

2 Answers 2

1

check the difference :-

<?php
//Database connection using PHP PDO(php database objects)
try {
$db = new PDO("mysql:dbname=member;host=localhost", "root", "",          array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
 } 
catch ( PDOException $e ) {
 echo $e->getMessage();
}

$sql= "SELECT * FROM friendship WHERE sender_id=".$_GET["sender_id"]." AND       receiver_id=".$_GET["sender_id"]."";
$rs=$db->prepare($sql);
$rs->execute();
$rs->fetch();
 $count = $rs->rowCount();

if ($count>0){
 echo "You already sent request...";
 echo $count;
 }
  else {
 $query = $db->query("INSERT INTO friendship (sender_id, receiver_id, is_approved) VALUES (".$_GET["sender_id"].",".$_GET["receiver_id"].",0)" );
 //$query->execute();

   echo "Request sent";
   echo $count;
}

in last else block you are using query() and execute() both,used for execute sql queries one query() directly and execute() for prepared statement

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, this is working properly. But without executing $query, how insert query inserting a row?
0

like @dianuj said in his comment, if you saw two requests at the same time in console, then change your js function like below

<script type="text/javascript">
  var in_request  = false;
  function loadName() {
    if ( in_request ) {
      return false;
    }
    in_request  = true;
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange  = function() {
      if ( xmlhttp.readyState === 4 && xmlhttp.status === 200 ) {
        in_request  = false;
        document.getElementById( "sample_div" ).innerHTML = xmlhttp.responseText;
        $("#my_button").attr( "value", "Sent" );
        $("#my_button").attr( "style", "color:red;" );
      }
    };
    xmlhttp.open( "GET", "dbaction.php?sender_id=5&receiver_id=6", true );
    xmlhttp.send();
  }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.