jQuery的到PHP,然后再返回! [英] jQuery to PHP and back again!

查看:131
本文介绍了jQuery的到PHP,然后再返回!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递的内容来回PHP jQuery的,反之亦然。我不知道我是否完全理解去了解这一点的最好方式,我希望对一些最好的建议和澄清。

下面是一些我想做一个例子。在PHP列出目录中的文件(其路径是从jQuery的传递给它),其存储在一个数组,然后将它们传递回的jQuery。我想使用该数组用于各种目的的价值观,但我真的只是想了解的信息传递来回在两者之间,羯羊它是从一个数组,或只是一个普通的变量。 的Merci beaucoup!

PHP的:
    

  $文件=阵列();
$ DIR =($ _ POST ['目录']);
$计数= 0;

如果($处理=执行opendir($迪尔)){
    而(虚假!==($文件= readdir的($处理))){
        如果($文件=放!。;&安培;!$文件=...&放大器;&安培; strpos($文件,名为.jpg',1)){$计数++;
            $文件[$文件] = $文件;
        }
    }
    closedir($处理);
}
    回声json_en code($文件);
?>
 

jQuery的:

  $(文件)。就绪(函数(){

    $('A')。点击(函数(五){
        即preventDefault();

        $。员额(php.php,路径/到/目录/',
            功能(数据){
                警报(数据);
        },JSON);
    });

});
 

解决方案

如果我正确地读你的问题,你不是真的找对这个特定code提示,但更多的关于传输数据的过程中意见来回PHP和jQuery之间。让我们简要地JSON本身的样子,再看看通信的每一面。

JSON

JSON是数据的字符串格式为pretty的便于人类和计算机读取重presenting集合的简单方法。您可以得到充分的说明 http://www.json.org/ 但它基本上可以归结为:

  • 数据是由封闭式{} 字符
  • 数据格式为字符串:值,其中字符串作为一个参考标签
  • 字符串中的格式,其次是任何单code字符,除了 / ,紧接着又是报价
  • 可以是另一个字符串,一个数字,一个完整的数据对象,一个布尔值,或某些设定的上述数值数组
  • 在一个数组的格式为 [,后跟一个逗号分隔值列表,随后]

PHP

在PHP的侧面,则正在接收的寻呼请求,并使用附加参数来决定如何处理该页面。对于JSON的应用程序,这意味着将数据加载到一个数组,然后 json_en code()函数做繁重的工作转换的数组转换成JSON格式。如果您手动创建的JSON作为一个字符串的应用程序的其余部分将工作一样的,但显然这将使在PHP $ C $下你要做更多的工作。因此,辅助函数:)

的jQuery

在jQuery的一面,调用 $。员额()发出一个AJAX请求,从服务器中检索的页面。在这种情况下,你发送请求到 php.php

第二组中的 $。员额()调用参数是参数的集合,其中应包括 {接着逗号分隔集:一个标签,然后一个冒号,然后一个值。一旦您指定的所有参数,关闭集合了} 。需要注意的是,虽然这类似于JSON字符串,它不JSON。该标签没有周围的引号JSON需要的方式。任何字符串值,但是,确实需要引号。

$。后的第三个参数()通话将被自动应用到从页面请求,接收到的数据的功能。该函数的结果会被自动加载到任何变量,你在函数定义中指定,你可以使用该数据中的功能,pretty的多少,你请。在你的榜样,您只需将数据发送到一个警告框,但你可以做更多的事情吧。实际上可以分析这个作为JSON收集和JSON的内执行基于个体成分的含量从各种动作(这最终意味着你是直接作用于从原来的PHP阵列个体值)。

$。后第四个参数()调用数据类型。它不是必需的,但使用它是必需的,如果你要访问的JSON收集并不仅仅是一个字符串。有了它,你可以指定要返回数据的类型是JSON,只需包括JSON。如果你这样做,你可以直接在第三个参数函数访问JSON集合中的元素,通过引用他们的标签。

下面是一个完整的JSON $的例子后()电话:

  $后(test.php的,{FUNC:getNameAndTime},
   功能(数据){
     警报(data.name); // pretend这是约翰·
     执行console.log(data.time); // pretend是上午10:05
   },JSON);
 

所以在这里,一个Ajax请求被发送到test.php的使用参数 FUNC =getNameAndTime该PHP使用,以确定它应该返回一个JSON-ZH {:约翰,时间:名字,10:05 AM} codeD的形式集合,然后响应到达指定的功能第一个警报(),其值为约翰,然后登录10:05 AM。此外,唯一的原因是 data.name data.time 工作在这个函数是因为我们指定了JSON是在第四个参数的返回类型。

I'd like to pass content back and forth from PHP to jQuery and vice versa. I'm not sure if I fully understand the best way to go about this and am hoping for some best advice and clarification.

Below is an example of something I'm trying to do. The PHP lists the files in a directory (whose path is passed to it from jQuery), stores them in an array, then passes them back to jQuery. I'd like to use the values in that array for various purposes but really I just want to understand passing information back and forth between the two, wether it's from an array, or just a plain variable. Merci beaucoup!

The PHP:

$files = array();
$dir = ($_POST['dir']);
$count = 0;

if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && strpos($file, '.jpg',1)) {$count++;
            $files[$file] = $file;
        }
    }
    closedir($handle);
}
    echo json_encode($files);
?>

The jQuery:

$(document).ready(function(){

    $('a').click( function(e) {
        e.preventDefault();

        $.post("php.php", 'path/to/directory/',
            function(data) {
                alert(data);
        }, "json");
    });

});

解决方案

If I read your question correctly, you're not really looking for tips on this specific code, but more for comments about the process of transferring data back and forth between PHP and jQuery. Let's look briefly at JSON itself, and then look at each side of the communication.

JSON

JSON is a simple way of representing collections of data in a string format that is pretty easy for both humans and computers to read. You can get the full description at http://www.json.org/ but it basically boils down to:

  • data is enclosed by { and } characters
  • data is in the format string : value, where the string acts as a reference label
  • string is in the format of ", followed by any unicode char except / or ", followed by another quote
  • value can be another string, a number, a complete data object, a boolean value, or an array of some set of the above values
  • an array is of the format [, followed by a comma separated list of values, followed by a ]

PHP

On the php side, you are receiving a page request and using the attached parameters to decide how to process the page. For a JSON app, that means loading the data into an array, and then the json_encode() function does the grunt work for converting that array into JSON format. The rest of the app would work just the same if you manually created the JSON as a string, though obviously this would make for a lot more work in the PHP code for you. Hence the helper function :)

jQuery

On the jQuery side, the call to $.post() issues an AJAX request, to retrieve a page from the server. In this case, you are sending a request to php.php.

The second set of parameters in the $.post() call is a collection of parameters, which should consist of { followed by comma-separated sets of: a label, then a colon, then a value. Once you have specified all parameters, close the collection with a }. Note that though this is similar to a JSON string, it is not JSON. The label does not have quotes around it the way JSON requires. Any string values, however, do need quotes.

The third parameter of the $.post() call is a function that will be automatically applied to the data that is received from the page request. The results of the function are automatically loaded into whatever variable you specify in the function definition, and you are able to use that data within the function, pretty much as you please. In your example, you simply sent the data to an alert box, but you can do much more with it. You could actually parse this as a JSON collection and perform various actions based on the contents of individual components from within the JSON (which, ultimately, means that you are directly acting on individual values from the original php array).

The fourth parameter on the $.post() call is the data-type. It isn't required, however using it is required if you want to access the json collection as more than just a string. With it, you can specify that the type of data you are returning is json, by simply including "json". If you have done this, you can access the elements of the JSON collection directly within the third parameter function, by referencing their label.

Here's an example of a complete JSON $.post() call:

$.post("test.php", { "func": "getNameAndTime" },
   function(data){
     alert(data.name); //pretend it's John
     console.log(data.time); //pretend it's 10:05am
   }, "json");

So here, an ajax request is sent to test.php with the parameter func="getNameAndTime" which the php uses to determine that it should return a json-encoded collection of the form {"name":"John", "time":"10:05am"}, and then the response reaches the function specified to first alert() with the value "John" and then log "10:05am". Again, the only reason that data.name and data.time works in this function is because we specified that json was the return type in the fourth parameter.

这篇关于jQuery的到PHP,然后再返回!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆