H
H
Helig2018-02-17 22:18:38
JavaScript
Helig, 2018-02-17 22:18:38

How to return response from php code to $.ajax?

I check on the server if there is a given value that ajax passed to the database and I want to return a response, how can I do this?
Here is the code:

$.ajax({
        type: "GET",
        url: window.location.href + "?id=" + per
      }).error(function(jq, status) {
        alert(jq.errorText || status);
      }).success(function(data, status, jq) {
        alert(data.id);
      });


<?
require "lib/rb.php";
R::setup( 'mysql:host=localhost;dbname=base','root','');

  if ($_GET['id'] != "") {
    header('Content-Type: application/json; charset=windows-1251');
    echo  '{ "id": "' . $_GET['id'] . '" }';

  /*	if(R::count('users', "email = ?", array($_GET['id'])) > 0 ){
      $_GET['ot'] = 1; /* Еще не решил каким способом буду обращаться к бд, тут только пример*/
    } */


    exit;
  }
    header('Content-Type: text/html; charset=windows-1251');


?>


Those. I check if there is a value and return in js let's say true, and then if it's true, then the script will do what I say.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Yanyshev, 2018-02-17
@Helig

Ajax in the url is easier location.origin and set Ajax dataType: json. If you find something in the database, then return json:
json_encode(['result' => $answer])
And on the client, already in response response.result

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question