【问题标题】:Recovery variable Ajax in WordpressWordpress 中的恢复变量 Ajax
【发布时间】:2014-10-18 20:55:59
【问题描述】:

我正在尝试制作一个带有自动完成功能的表单,并且必须根据在前一个字段中输入的数据、数据库中的数据以及 Wordpress 中的所有内容来完成这些字段......

对于自动完成,没问题,它适用于 Wordpress,我只是添加了一些代码,例如:

$("#nomPersonne").autocomplete
  ({
     source: 'wp-content/themes/hero-child/listePersone.php'

  });

还有一个这样的页面listePersonne:

<?php

include_once 'connexion.php';

$term = $_GET['term']; 
$requete = $link->prepare("SELECT `wp_terms`.`name` FROM `wp_term_taxonomy` 
JOIN `wp_terms` ON `wp_terms`.`term_id`=`wp_term_taxonomy`.`term_id`
WHERE `wp_term_taxonomy`.`taxonomy`='nomPersonne' AND `wp_terms`.`name` LIKE :term");

$requete->execute(array('term' => '%' . $term . '%'));

$array = array(); // on créé le tableau

while ($donnee = $requete->fetch()) { // boucle pour obtenir les données
array_push($array, $donnee['name']); // et on ajoute celles-ci à notre tableau
}

echo json_encode($array); // il n'y a plus qu'à convertir en JSON
?>

因此出现人员列表,当一个人选择一个人时(如果发现合适的话),以下组织列表将仅列出上述列表中所选人员的组织。

在发现有必要使用 Wordpress 语句之前,我率先使用了 Ajax:

add_action('wp_ajax_do_ajax', 'notre_fonction_ajax');

我仍然无法将我的变量(值 PersonName)传递给下一个字段。

所以我有一个 JS 文件:

jQuery(document).ready(function(){
 jQuery('#nomPersonne').mouseleave(function(){
      doAjaxRequest();
 });
});
function doAjaxRequest(){

 jQuery.ajax({
      url: 'wp-admin/admin-ajax.php',
      data:{
           'action':'do_ajax',
           'fn':'get_id_value'
           },
      dataType: 'JSON',
      success:function(data){
                jQuery("#nomOrgani").html(data);
                         },
      error: function(errorThrown){
           alert('error');
           console.log(errorThrown);
      }

 });

}

我在function.php中的函数:

函数 notre_fonction_ajax(){

// ce switch lancera les fonctions selon la valeur qu'aura notre variable 'fn'

 switch($_REQUEST['fn']){
      case 'get_id_value':
           $output = ajax_get_id_value($_REQUEST['count']);
      break;
      default:
          $output = 'No function specified, check your jQuery.ajax() call';
      break;

 }

//Maintenant nous allons transformer notre résultat en JSON et l'afficher

 $output=json_encode($output);
 if(is_array($output)){
    print_r($output);
 }
 else{
    echo $output;
 }
 die;

}

function ajax_get_id_value(){
function ajax_get_id_value(){

include_once 'connexion.php';

$term = $_GET['term']; 

$requete = $link->prepare("SELECT `wp_postmeta`.`meta_value`  FROM `wp_postmeta` 
JOIN `wp_term_relationships` ON `wp_postmeta`.`post_id` =  'wp_term_relationships`.`object_id` 
JOIN `wp_term_taxonomy` ON `wp_term_relationships`.`term_taxonomy_id` =  `wp_term_taxonomy`.`term_taxonomy_id`
JOIN `wp_terms` ON `wp_terms`.`term_id` = `wp_term_taxonomy`.`term_id`
WHERE `wp_terms`.`name` = 'Malaysia Airlines' AND  `wp_postmeta`.`meta_key`='organization' AND `wp_postmeta`.`meta_value` LIKE :term ");

$requete->execute(array('term' => '%' . $term . '%'));

$array = array(); // on créé le tableau

while ($donnee = $requete->fetch()) { // on effectue une boucle pour obtenir les données
array_push($array, $donnee['meta_value']); // et on ajoute celles-ci à notre tableau
}

echo json_encode($array); // il n'y a plus qu'à convertir en JSON
}
}

在这个文件中,我设置了“Malaysia Airlines”,但它实际上是我应该恢复的变量。

当我查看 Firebug 时,我在控制台上看到了答案:["Malaysia Airlines"],但我无法在下一个查询中使用该值

无论如何,我刚刚完成了我的课程,我正在接受培训,但我很难让它们在所有语言、CMS 等方面协同工作......

现在我的大脑就像果冻一样...如果有人有灯,我将永远感激不尽!

凯利格。

【问题讨论】:

  • Sola、perduta、abbandonata...
  • 如果您有任何建议,我会在这个周末回复它...
  • 我想我在逻辑的某个地方犯了一个错误,我想我要从零开始一切。我有两个列表(人员和组织),如果 BDD 中存在数据,则由自动完成填充,这部分正在工作。然后我想取回选择的两个字段以请求在 BDD 中显示相应数据(如果存在)。我想根据第一个字段中的内容填写第二个字段来改进。你将如何进行最简单和最有效的?祝你有美好的一天!
  • 我在我的 JS 文件中有检索 PersonName 和 nomOrgani 输入的函数,并且相应的 php 函数工作:FOR PersonName: File JS

标签: javascript php ajax wordpress


【解决方案1】:
jQuery(document).ready(function(){
 jQuery('#nomPersonne').change(function(){
      doAjaxRequest1();
 });
});
function doAjaxRequest1(){
  jQuery.ajax({
      url: 'wp-admin/admin-ajax.php',
      data:{
           'action':'do_ajax',
           'fn':'get_pers_value', 
           'value': document.getElementById("nomPersonne").value
           },
      dataType: 'JSON',
      success:function(data){
                //alert('çà marche !');
                         },
      error: function(errorThrown){
           alert('error');
           console.log(errorThrown);
      }
 });
 }

对于 nomOrgani:文件 JS

jQuery(document).ready(function(){
jQuery('#nomOrgani').change(function(){
      doAjaxRequest2();
 });
});

 function doAjaxRequest2(){

  jQuery.ajax({
      url: 'wp-admin/admin-ajax.php',
      data:{
           'action':'do_ajax',
           'fn':'get_org_value', 
           'value': document.getElementById("nomOrgani").value
           },
      dataType: 'JSON',
      success:function(data){
          //alert('çà marche !');      
          show('signalIntox');
                         },
      error: function(errorThrown){
           alert('error');
           console.log(errorThrown);
      }

 });

}

add_action('wp_ajax_nopriv_do_ajax', 'notre_fonction_ajax');
add_action('wp_ajax_do_ajax', 'notre_fonction_ajax');
function notre_fonction_ajax(){
    // ce switch lancera les fonctions selon la valeur qu'aura notre variable 'fn'
     switch($_REQUEST['fn']){
      case 'get_pers_value':
           $output = ajax_get_pers_value('value');
      break;
      case 'get_org_value':
           $output = ajax_get_org_value('value');
      break;
      default:
          $output = 'No function specified, check your jQuery.ajax() call';
      break;
 }
 // Maintenant nous allons transformer notre résultat en JSON et l'afficher
 $output=json_encode($output);
 if(is_array($output)){
    print_r($output);
 }
 else{
    echo $output;
 }
 die;
}
function ajax_get_pers_value($nomPersonne){
$nom = $_REQUEST['value'];
return $nom;
}  
function ajax_get_org_value($nomOrgani){
$org = $_REQUEST['value'];
return $org;
}

所以一切正常,因为我在 firebug "GET admin-ajax.php?Action ..." 中获得两个输入的值,在显示值的响应选项卡中,例如 "NAME PERSON" 和组织。

现在,使用我要请求的值: 对于 nomPerson 值,推导出所有组织在 BDD 中查找 nomOrgani 和 PersonName 的值:使具有这两个名称的应用程序显示具有此名称的帖子的内容。 我认为,成功方函数(数据)发送到一个 php 文件以使我的请求......跟随!

【讨论】:

    【解决方案2】:
    function doAjaxRequest1(){
         jQuery.ajax({
              url: 'wp-admin/admin-ajax.php',
              data:{
                   'action':'do_ajax',
                   'fn':'get_pers_value', 
                   'value': document.getElementById("nomPersonne").value
                   },
              dataType: 'JSON',
              success:function(data){
                        //alert('çà marche !');
                        createCookie('nom',document.getElementById("nomPersonne").value,0);
                        console.log(readCookie('nom'));
                        alert(readCookie('nom')); 
                        },
              error: function(errorThrown){
                   alert('error');
                   console.log(errorThrown);
              }
         });
     }
    

    现在,我在 Javascript 中创建了函数来设置和获取 cookie。 我已经尝试了我的 succes 功能,它有效!

    但是现在我必须在我的 SQL 请求中使用这些 cookie 来显示我的信息。 不知道如何混合javascript和SQL,欢迎提出建议...... 祝你今天过得愉快。 凯利格。

    【讨论】:

      【解决方案3】:

      还没有完全解决……但差不多;实际上,我的 cookie 有时间滞后,第二个来晚了,可以这么说……我有第一个请求的“nom”和第二个请求的“org”。另一方面,如果我填写字段两次以同样的方式显示好的信息很好,因此一旦记录了两个好的cookie,SQL查询和显示就可以工作。此外,它只适用于Firefo。在 Chrome 和 IE 中,第二个自动完成列表不显示,在 chrome 开发工具中我找到了 cookie 'org' 但没有找到 cookie 'nom' .. 。 我并不遥远,我坚持! 祝你今天过得愉快。 凯利格。

      【讨论】:

        【解决方案4】:

        事实上,我已经在另一个 Ajax 调用中嵌套了一个 Ajax 调用。 当我创建第二个 cookie 时,我调用 (Ajax) 来创建 div(查询和显示)。 这有点令人费解,但基本上它可以工作,不会显示我想要的所有视图,但我认为这是查询本身的问题。 在任何情况下,“Ajax 引擎”都有效! 谢谢你。 祝你今天过得愉快。 凯利格。

        【讨论】:

          猜你喜欢
          • 2013-01-27
          • 1970-01-01
          • 2022-12-12
          • 1970-01-01
          • 2016-04-13
          • 1970-01-01
          • 1970-01-01
          • 2020-06-30
          • 2021-06-02
          相关资源
          最近更新 更多