您的位置:

php与ajax的例子,ajax和php交互

本文目录一览:

thinkphp中怎么用ajax

thinkphp中使用ajax很简单.主要掌握的地方有三个.

第一.tp中ajax的url需要使用大U方法.比如:$.post("{:U('User/add')}")

第二.控制器中返回结果得第一种方法.$this-error('失败','',true); 第三个参数为true.则发挥的是json数据.包含info.status.url三项.

第三.控制器中返回结果的第二种方法.$this-ajaxReturn(array('customKey1'='customValue1','customKey2'='customValue2','customKey3'='customValue3')).

掌握以上三点和ajax基本使用方法.那么在tp中使用ajax也就掌握了.简单的例子如下.伪代码,或许有错.

模板中:

script

    $.post("{:U('User/add')",{uname:xiaoming,age:15},function(data){

        //data接收返回数据

        if(data.status == 1){

            alert(data.info);

            location.href = data.url;

        }else{

            alert('错误');

        }

    });

/script

控制器中:

public function add(){

    if(IS_AJAX){

        $name = I('post.name','','trim');

        $age = I('post.age','','trim');

        if($name  $age){

            //插入数据

            $this-success('添加成功',U('User/index'),true);

        }else{

            $this-ajaxReturn(array(

                'status'    =    0,

                'info'    =    '大爷,您没输入名字',

                'url'    =    U('User/add')

            ));

        }

        

    }else{

        return false;

    }

}

大概就是这样子.至于剩下的$.ajax.$.getJson等等都是一样的道理

AJAX 向PHP传递参数

能啊!给你个例子啊!

html

head

script type="text/javascript"

var xmlhttp;

function loadXMLDoc(url)

{

xmlhttp=null;

if (window.XMLHttpRequest)

{// all modern browsers

xmlhttp=new XMLHttpRequest();

}

else if (window.ActiveXObject)

{// for IE5, IE6

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}

if (xmlhttp!=null)

{

xmlhttp.onreadystatechange=state_Change;

xmlhttp.open("GET",url,true);

xmlhttp.send(null);

}

else

{

alert("Your browser does not support XMLHTTP.");

}

}

function state_Change()

{

if (xmlhttp.readyState==4)

{// 4 = "loaded"

if (xmlhttp.status==200)

{// 200 = "OK"

document.getElementById('p1').innerHTML="This file was last modified on: " + xmlhttp.getResponseHeader('Last-Modified');

}

else

{

alert("Problem retrieving data:" + xmlhttp.statusText);

}

}

}

/script

/head

body

p id="p1"

The getResponseHeader() function returns a header from a resource.

Headers contain file information like length,

server-type, content-type, date-modified, etc./p

button onclick="loadXMLDoc('/example/ajax/test_xmlhttp.txt')"Get "Last-Modified"/button

/body

/html

如何在同一个PHP页面,通过ajax把值传给PHP变量?

举个例子:你想在用户点击时,把 apple 这个字符串,通过前端传给后端。

前端,用 jQuery 举例:

$('button').click(function () {

$.ajax({

url: '/xxx',

method: 'post',

dataType: 'json',

data: {fruit: 'apple'}

}).done(function (res) {

// 成功后的回调

}).fail(function (err) {

// 失败后的回调

});

});

后端 PHP 处理:

$fruit = $_POST['fruit']; // 获取从 ajax 传过来的 fruit 的值,这里是 apple。

如果你想在前端重新显示这个字符串 apple,那么你要用 PHP 把数据返回给页面,然后在上面 “// 成功后的回调” 里面,补充逻辑代码。

例如 PHP 把 apple 返回给前端:

return json_encode(array('fruit' = 'apple'));

前端回调处理:

// 成功后的回调

alert(res.fruit); // 弹框显示 “apple”

实际上,$_POST 能够获取所有从前端用 post 方式提交过来的数据,不管你是页面刷新方式,还是 ajax(jQuery 才叫 ajax,实际上它是 XMLHttpRequest,异步非阻塞的请求方式)