简体   繁体   English

通过Ajax请求调用php函数

[英]Call a php function via Ajax Request

Im trying to code my homepage more structured. 我试图对我的主页进行结构化编码。 I have to work a lot with mysql db queries and I want to create for every table a own .php file with all necessary functions which I want to call via Ajax Request. 我必须处理很多mysql db查询,并且我想为每个表创建一个自己的.php文件,其中包含我想通过Ajax Request调用的所有必要功能。

Therefore I got the following snippet by a stackoverflow answer: 因此,我通过stackoverflow答案得到了以下代码片段:

$.ajax({ url: '/my/site',
         data: {action: 'test'},
         type: 'post',
         success: function(output) {
                      alert(output);
                  }
});

On the server side, the action POST parameter should be read and the corresponding value should point to the method to invoke, eg: 在服务器端,应读取action POST参数,并且相应的值应指向要调用的方法,例如:

if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    switch($action) {
        case 'test' : test();break;
        case 'blah' : blah();break;
        // ...etc...
    }
}

My problem: 我的问题:

I want to assign form data as well via the data attribute and I don't know how I can do this. 我也想通过data属性分配表单数据,但我不知道该怎么做。 I tried the following (this was just a guess which didnt work): 我尝试了以下操作(这只是一个没有用的猜测):

var data = $(this).serialize();
$.ajax({ url: '/my/site',
         data: {action: 'test', data},
         type: 'post',
         success: function(output) {
                      alert(output);
                  }
});

serialize() will return the data from the form as a string . serialize()将以字符串形式从表单返回数据。 You can just append the rest of the string with your remaining queries. 您可以仅将字符串的其余部分与其余查询一起附加。

Example: 例:

<form id="form">
    <input name="form_name_1" value="form_value_1">
    <input name="form_name_2" value="form_value_2">
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    var data = $('#form').serialize();
    data += '&action=test';

    $.ajax({
        url: '/my/site',
        data:data,
        type:'post',
        success:function(output) {
            alert(output);
        }
    });
});
</script>

You need to specify the data as a parameter 您需要将数据指定为参数

var data = $(this).serialize();
$.ajax({ url: '/my/site',
         data: {action: 'test', data: data},
         type: 'post',
         success: function(output) {
                      alert(output);
                  }
});

The data parameter of the ajax call expects an object, as defined the in ECMA/Javascript spec - eg a series of key/value pairs ajax调用的data参数需要一个对象,如ECMA / Javascript规范中所定义-例如,一系列键/值对

The you can access it via $_POST['data'] 您可以通过$_POST['data']访问它

Personally, I tend to json serialise it to avoid http quirks around arrays... 就个人而言,我倾向于对其进行json序列化,以避免数组周围出现HTTP怪癖...

data: {action: 'test', data: JSON.stringify(data)}

then in PHP: 然后在PHP中:

$data = json_decode(isset($_POST['data']) ? $_POST['data'] : "{}");

If data was posted, it will now be present in $data as a complex object, otherwise $data will contain an empty object 如果发布了数据,则它现在将作为复杂对象出现在$data中,否则$data将包含一个空对象

You can post your data as JSON object 您可以将数据发布为JSON对象

$.ajax({
    type: "POST",
    url: "/some/url",
    data: JSON.stringify(jsobject),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(output){
      alert(output);
    }
});

and in the php script you just do json_decode to get the object or associative array. 在php脚本中,您只需执行json_decode即可获取对象或关联数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM