1

こんにちは私は少しajaxと少し混乱を学んでいます。

それはすべて機能しますが、100%確実ではありません。以下のスクリプトは、その構造がどのようになっているのかを示しています。私が知りたいのは、成功:関数(結果)がどのように機能するかです。私のPHPには、1または0のエコーがありますが、PHPにさらに情報、つまり正しいメッセージを含めたいと思います。

私のJavaScriptはです。

$("#username1").blur(function(){

 var username1 = $('#username1').val();
  $.ajax({
    type: "POST",
    url: "http://localhost/gonnabook/check_username_availability",
    data:  'username1='+ username1,
    success: function(result){
    if(result == 1){
        alert("true");
        }
    else{
        alert("false");
        }
   }
});

phpは

function check_username_availability(){

        $check_this = $this->input->post('username1');  
        $data = $this->tank_auth->is_username_available($check_this);
        // the above code does all the query and results in the data
        if($data){
              echo 1;
             //ideally I would like to add a message here ie This is not available
        }else{
            echo 0;

        }
 }

これまでのところ、私の理解では、phpのecho 1は、javascriptのsuccess関数が受け取るものです。しかし、PHPにメッセージを含めるにはどうすればよいでしょうか。配列などを介してそれを行うことができ、結果はresult.checkなどになりますか?重要な点は、javascriptの結果とphpのエコーの相関関係は何ですか?

4

3 に答える 3

0

これです:

server-side(php)がエコーし、ajaxがこれを成功関数でリッスンします

単純:

in php: echo "1"  ==> in ajax: success will get this 1 as result argument

必要なものは何でも渡すことができます..array、list、dict、json .. ..

ここでは、ajaxを使用したときにバックグラウンドで何が起こるかを実際の例で読むことができます https://web.archive.org/web/20140203201657/http://net.tutsplus.com/tutorials/javascript-ajax/5-ways -to-make-ajax-calls-with-jquery /

于 2012-11-09T11:58:52.557 に答える
0

最善の策は、JSONオブジェクトをエコーするのではなく、エコー0する1ことです...これを試してください...

PHP

function check_username_availability(){

        //This array will hold the data we return to JS- as many items as you want
        $result=array();  

        $check_this = $this->input->post('username1');  
        $data = $this->tank_auth->is_username_available($check_this);
        // the above code does all the query and results in the data
        if($data){
             $result['success']=1;
             $result['message']='This is a random message';
             $result['data']=data; //You can even return the retrieved data
        }else{
             $result['success']=0;

        }
        die(json_encode($result)); //Encode the array into JSON and echo the string
 }

JS

$("#username1").blur(function(){
  var username1 = $('#username1').val();
  $.post(
        "http://localhost/gonnabook/check_username_availability", 
        {'username1': username1},
        function(result){
            if (result.success==1){
                alert('success: ' + result.message);
            } else {
                alert('error');
            }
        }, 
        'json'  //Tell JS we expect JSON in return
    });
}
于 2012-11-09T12:03:47.183 に答える
0

PHPスクリプトで通常行うことは、JSON文字列としてシリアル化されたデータを返すことです。このオブジェクトは、ajax呼び出しの成功コールバックで使用できます

phpスクリプト

function check_username_availability(){

 //do something fancy

 //create your JSON return
 var $data = array("foo" => "bar","bar" => "foo");
 echo json_encode($data);
}

クライアント

$.ajax({
 type: "POST",
 url: "http://localhost/gonnabook/check_username_availability",
 data:  'username1='+ username1,
 success: function(data){
  console.log(data['foo'] + ' ' + data['bar']); //echos "bar foo"
 }
});
于 2012-11-09T12:04:19.303 に答える