调用Ajax的功能呢?功能、Ajax

2023-09-10 13:54:50 作者:月岛

我想实现 我有一些作品我想显示。所以,我有这些缩略图。当访问者点击缩略图,我想一个div(称为slickbox)打开并显示标题,描述和对工作的点击一个滑块。

What I want to achieve I have some works I want to show. So, I have thumbnails of these. When a visitor clicks on a thumbnail, I want a div (called slickbox) to open and show the title, the description and a slider about the work clicked.

我已经做和如何 我得到我的工作的DATAS从数据库中。这里是我的作品上市的少部分:

What I've already done and how I get my work's datas from a database. Here is the little part of my listing of works:

的index.php

index.php

<?php
  $retour_messages = mysql_query('SELECT 2K13_works.*, 2K13_categories.nom AS nomCAT FROM 2K13_works, 2K13_categories WHERE 2K13_works.cat_id = 2K13_categories.cat_id ORDER BY 2K13_works.record_date DESC') or die(mysql_error());//requete sql pour récupérer les works de la page
  ?>
  <ul id = "creations" class = "step">
    <?php
  while($donnees_messages=mysql_fetch_assoc($retour_messages)){
  echo '<li class = "step '.$donnees_messages['nomCAT'].'" id="'.$donnees_messages['work_id'].'">
               <div class = "item"><a href = "#"><img src = "'.$donnees_messages['thumbLink'].'" alt = "'.$donnees_messages['titre'].'" title = "" width = "226" height = "147"/></a>
                <div class = "caption">
                  <h3>'.$donnees_messages['titre'].'</h3>
                  <p>'.html_entity_decode($donnees_messages['resume'],ENT_QUOTES,'UTF-8').'</p>
                  <p id = "desc" class = "hidden">'.html_entity_decode($donnees_messages['description'],ENT_QUOTES,'UTF-8').'</p>
                  <!--<p id = "idw" class = "hidden">'.$donnees_messages['work_id'].'</p>-->
                </div>
              </div>
            </li>';
            }
 ?>
  </ul>

正如你所看到的,我有一个包含锂tagfor各项工作的UL标记。每个李标签需要在数据库中的工作的ID,每个李含含我想在一个slickbox显示(用于图像,我会在后面看到)。

As you can see, I have a ul tag containing a li tagfor each work. Each li tag takes the id of the work in database, and each li contains h3 tag and p tag containing the texts I want to show in a slickbox (for the images, I'll see later).

现在,我的JavaScript code为slickbox,出现和消失:

Now, my JavaScript code for the slickbox, appearing and disappearing:

front_functions.js

front_functions.js

//_____________SLICKBOX__________________________________
$('#slickbox').hide();
$("#creations li").click(function(e) {
    // shows the slickbox on clicking the noted link
    $titre = $(e.target).children("h3").text();
    $bla = $(e.target).children("#hidden").text();
    $("#description").children("h1").text($titre) ;
    $("#description").children("p").text($bla); 
      $('#slickbox').slideDown();
      e.preventDefault();
      $(e.target).empty();
      //return false;
});

这code不工作,因为我的slickbox的作品之前加载。所以这就是为什么我需要Ajax和发送和执行请求的异步方式。

This code is not working, because my slickbox is loaded before the works. So that's why I need Ajax and a asynchronous way of sending and executing requests.

我在这里读这样code:这是相当有帮助的。 但是,我有一个问题:我使用jQuery,我想用 $阿贾克斯()。而我只是真的不明白如何做到这一点。

I read this sample code here: which is quite helpful. But, I have a problem: I'm using jQuery and I would like to use $.ajax(). And I just don't really understand how to do this.

我一定要设置一个XHMLHTT prequest对象?我在哪里可以编写Ajax调用?我可以打电话,而不是一个URL的函数,?

Do I have to set an XHMLHTTPRequest object? Where can I write the Ajax call? Can I call a function, instead of an URL?

喜欢做(我不知道):

$(#creations li).click(function(e){
        $.ajax(){
           function : "displayContent(id,desc,title)",
        }
}
function displayContent(id,desc,title){
    $(#slickBox).children("h1").innerHTML(title);
    $(#slickBox).children("p").innerHTML(desc);
    $(#slickBox).show();
}

我甚至不知道我是否应该使用JSON(但,好了,因为我的数据已经存储了,我只是想显示他们,我想我不需要JSON)。

I don't even know if I should use JSON (but, well, because my data is already stored, and I just want to display them, I think I don't need Json).

请给我你知情的意见和你的上级的意见。

Please give me your informed opinion and your senior advice.

推荐答案

当你发送服务器(使用AJAX)这就像你在一个页面提交表单的请求。 这样就可以用PHP做的时候一个表单提交的每一件事情,你可以做到这一点与阿贾克斯过。 例如,如果你想调用PHP中的AJAX功能,只需发送一个参数到PHP这样的:

when you send a request for server (with ajax) this is like that you are submitting a form in a page . so every thing that you can do with php when a form submitted , you can do that with ajax too . e.g if you want to call a function in php with ajax , just send a param to php like this :

$.ajax({  
  type:'POST',
  data:{
     param:'Hey_php_call_this_function' 
  },
  success:function(data){
     alert('hey jquery , php said : ' + data);
  }
});

和服务器端:

if(isset($_POST['param']) && $_POST['param'] == 'Hey_php_call_this_function'){
    echo call_a_function();  /// "output to callback success function"  = data
}

希望有所帮助。

hope that helpful .