简体   繁体   English

通过ajax调用将值传输到php文件

[英]transferring values to php file thru ajax call

I am trying to send a json string and a variable thru ajax call and edit the query in php file, My code looks like 我试图通过ajax调用发送一个json字符串和一个变量,并在php文件中编辑查询,我的代码看起来像

queryData = {
    "data": {
        "data_string": {
            "data": "oil",
            "default_field": "Content"
        }
    }
};
from = 0;
$.ajax({
    url: "/elasticsearch-head/lib/es/queryManipulate.php",
    type: 'POST',
    data: 'dataString',
    datatype: 'json',
    data: {
        field: queryData,
        start: this.from
    },
    success: function(data) {
        Dumper.alert(data);
    }
});

The PHP file looks like, Ive taken the two values in to variables, I wanna alter the json in php and return the value in the form {"from": $from, "data":{"data_string":{"data":"oil","default_field":"Content"}}} PHP文件看起来像,我已经将两个值带入了变量,我想更改php中的json并以{“ from”:$ from,“ data”:{“ data_string”:{“ data”的形式返回值:“ oil”,“ default_field”:“ Content”}}}

<?php
$testData=$_POST["field"];
$from=$_POST["start"];
?>

It should return the query 4 times and each time the value of $from should increment by 10 Is it possible to do that with ajax and php?? 它应该返回查询4次,并且每次$ from的值应增加10。是否可以用ajax和php来完成?

That would be my solution: 那就是我的解决方案:

function retreiveData(from)
{
    queryData={"data":{"data_string":{"data":"oil","default_field":"Content"}}};
    $.ajax({
        url:"/elasticsearch-head/lib/es/queryManipulate.php",
        type: 'POST',
        data: 'dataString',
        datatype: 'json',
        data: {field : queryData, start : from},
        success:function(data)
        {
            Dumper.alert(data);
            if(from < (4-1)*10)
            {
                retreiveData(from+10);
            }
        }
    });
}
retreiveData(0);

This is a recursive function calling itself four times. 这是一个递归函数,其自身调用了四次。

EDIT: Or if I misread your question you could use a for-loop on the server side: 编辑:或者如果我误解了您的问题,您可以在服务器端使用for循环:

<?php
$testData=$_POST["field"];
$from=$_POST["start"];
for($i=0; ctype_digit($from) && is_numeric($from) && $i <  4; $i++)
{
    $from+=10;
    //process data on the server side
    //echo $testData; // or whatever
}
?>

This would you let your data be processed four times. 您可以将数据处理四次。

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

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