0

In Js file I've created a function which has to send an id and return boolean value to php file which returns false or true; but how can I do it?

JS function

function findDB(id)
{

}

PHP side

include_once("kutuphane/inc.php"); 
$id = $_POST['tipi'];

$sql= "select count(id) from bolge_db where parent_id=$id"; 

    $faz2= $_SESSION["VT"]->doQuery($sql);

 $flag=false;
 if($faz2>0)
  {
    $flag= true;
  }
   else
  {
    $flag= false;
  }
2

2 Answers 2

1

It's simple AJAX! Try out this code in js:

function MakeRequest()
{
  var xmlHttp = getXMLHttp();

  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }

  xmlHttp.open("GET", "ajax.php", true); 
  xmlHttp.send(null);
}

keep in mind that you have to echo something in php! eg: echo 1; echo 0;

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

Comments

1

Or using jQuery

var data = {
    var1 : "string or another var"
};

$.post('url.php',data , function(data) {
  var response = data;
  //Do What Ever You Want
});

4 Comments

My actual am is posting data to php and getting data from php. there I guess we can do it without function so $.post(url.php,data) is enough?
Yes it is, and you can grab data from PHP, but if you need a return statement than put $.POST inside the function end just return response.
so is it true in this way $.post('dbController.php',cuIzgaraKay.id,function(cuIzgaraKay.id) { var response = cuIzgaraKay.id; });
@user1702486 don't send the id directly you should send it as an Object $.post('dbController.php',{id:cuIzgaraKay.id},function(data) { var response = data; })

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.