Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 选择选项取决于wordpress管理页面中的其他选项_Php_Jquery_Html_Ajax_Wordpress - Fatal编程技术网

Php 选择选项取决于wordpress管理页面中的其他选项

Php 选择选项取决于wordpress管理页面中的其他选项,php,jquery,html,ajax,wordpress,Php,Jquery,Html,Ajax,Wordpress,我有两个选择类别和类型,类型应该取决于类别选择,从数据库中,这是我的代码 HTML PHP class-Get\u-category\u类型{ 函数_u构造() { 如果(isset($\u POST['category\u name'])和&!empty($\u POST['category\u name'])){ 全球$wpdb; $category=$\u POST['category\u name']; $ps_type_table_name=$wpdb->前缀“ps_type”; $t

我有两个选择类别和类型,类型应该取决于类别选择,从数据库中,这是我的代码

HTML

PHP

class-Get\u-category\u类型{
函数_u构造()
{
如果(isset($\u POST['category\u name'])和&!empty($\u POST['category\u name'])){
全球$wpdb;
$category=$\u POST['category\u name'];
$ps_type_table_name=$wpdb->前缀“ps_type”;
$types=$wpdb->get_col($wpdb->prepare('SELECT DISTINCT type FROM.$ps_type_table_name.'WHERE category=%s',$category');
foreach($type作为$type){
回显“.$type.”;
}
}
}
}

实际上,这是一个使用oops的WordPress插件,请帮我解决这个问题,我想问题是如何在ajax代码中获取PHP文件,请告诉我如何在ajax URL中写入路径,以便在WordPress插件中获取该类文件。问题是您正在替换选择元素的选项。您还需要在ajax php中包含选择元素

阿贾克斯:

PHP:

$result='';
foreach($type作为$type){
$result.=''.$type';
}
$result.='';
回声$结果;

1st:我认为
\u construct
函数只有在创建新对象实例时才会调用。因此,您需要创建实例来运行构造函数

class Get_category_type {

 function __construct()
 {
   if (isset($_POST['category_name']) && !empty($_POST['category_name'])) {
       global $wpdb;
       $category = $_POST['category_name'];
       $ps_type_table_name = $wpdb->prefix . 'ps_type';
       $types = $wpdb->get_col($wpdb->prepare('SELECT DISTINCT type FROM ' . 
       $ps_type_table_name . ' WHERE category=%s', $category));

      foreach ($types as $type) 
      {
         $result.= '<option value="' . $type . '">' . $type . '</option>';
      }
      echo  $result;
   }
 }

}

 $obj = new Get_category_type(); //create instance here .
4th:数据应以正确的方式在ajax中发送

    data:{category_name:category},
5th:在ajax中添加错误处理程序

$.ajax({
    type:'POST',
    url:'category-type-select.php',
    data:{category_name=category},
    success:function (html) {
         $('#type').empty();
         $('#type').append(html);
    },
    error: function (request, status, error) {
        alert(request.responseText);
    }
});
测试目的:


出于测试目的。将您的POST方法更改为获取如下方法
if(isset($\u GET['category\u name'])和&!empty($\u GET['category\u name']){..}
,然后像这样在浏览器中直接调用url<代码>http://yoursite.com/category-type-select.php?category_name=here_any_one_category_name。如果它运行了,那么问题就在你的ajax部分。

我找到了解决问题的方法,所以我发布了答案来帮助一些需要的人,在WordPress中,有单独的钩子来使用ajax函数,这个过程介绍了如何使用它

HTML

Ajax.js

   $(document).ready(function () {
   $('#category').change(function(){
    var ctgry=$(this).val();
    $.ajax({
        type:"post",
        url:ajaxurl,
        data:{'action':'get_type_by_category','ctgry':ctgry},
        cache:false,
        success:function (html) {
            $("#type").html(html);
            //alert(html);
        },
        error:function(request,string_error,error){
            alert(string_error);
        }

    });
});
$('#category').change();
});
将js连接到WordPress

    public function enqueue_scripts(){
 wp_enqueue_script('ps-admin-page-js', plugins_url('js/admin-page.js', dirname(__FILE__)),array('jquery'),'1.1.1',true);
 }
 add_action('wp_footer', 'enqueue_scripts');

我发现在WordPress插件开发中使用ajax是一种很好的方法,它对我很有用。

您的浏览器控制台中是否有任何错误?否,它没有显示任何错误,但在“选择类型”部分中没有显示任何内容。我想,只有在创建新的对象实例时,才会调用“构造”函数。所以你需要创建实例来运行构造函数。如果我删除类并直接编写它,那么它也不起作用,我想我必须在WordPress中调用PHP文件的路径,比如:plugin_dir_path(file)/category-type-select.PHP,我不知道当前的书写方式您当前的页面和category-type-select.php是在同一个目录中还是在不同的目录中@Deekshith Shettyno,它不起作用,它是一个WordPress插件,我认为问题在于ajax URL部分如何在ajax中调用PHP文件,内部WordPressok然后问题是其他的。我用ajax数据部分更新了答案。就这样尝试。在控制台网络中,您可以检查url。右键单击并在“新建”选项卡中打开它。它是否显示了数据?很抱歉,我没有获得u?打开控制台并转到网络。然后更改类别中的选项。您可以在控制台中看到ajax url。对吗点击并在新的标签中打开它不工作,兄弟,我想我必须在WordPress中调用PHP文件的路径,比如:plugin_dir_path(file)/category-type-select.PHP,我不知道当前的编写方式-按F12打开开发者工具并导航控制台。并在选择框中更改某些内容。现在您可以看到任何日志(如果存在)@DeekshithShetty用于测试目的。如果(isset($\u get['category\u name'])&!empty($\u get['category\u name']){..}将您的POST方法更改为获取如下方法,然后在浏览器中直接调用url,如下所示。如果它是运行的,那么问题就在ajax部分@DeekshithShetty查看我的答案,并按照所有步骤获得成功@DeekshithShetty这是wordpress文件,我可以运行它,给出直接路径吗?
class Get_category_type {

 function __construct()
 {
   if (isset($_POST['category_name']) && !empty($_POST['category_name'])) {
       global $wpdb;
       $category = $_POST['category_name'];
       $ps_type_table_name = $wpdb->prefix . 'ps_type';
       $types = $wpdb->get_col($wpdb->prepare('SELECT DISTINCT type FROM ' . 
       $ps_type_table_name . ' WHERE category=%s', $category));

      foreach ($types as $type) 
      {
         $result.= '<option value="' . $type . '">' . $type . '</option>';
      }
      echo  $result;
   }
 }

}

 $obj = new Get_category_type(); //create instance here .
foreach ($types as $type) 
{
   $result.= '<option value="' . $type . '">' . $type . '</option>';
}
echo  $result;
  $('#type').empty();
  $('#type').append(html);
    data:{category_name:category},
$.ajax({
    type:'POST',
    url:'category-type-select.php',
    data:{category_name=category},
    success:function (html) {
         $('#type').empty();
         $('#type').append(html);
    },
    error: function (request, status, error) {
        alert(request.responseText);
    }
});
 $ps_type_table_name = $wpdb->prefix . 'ps_type'; ?>

<form method="post" action="">
 <label>Category :</label><br>
     <select id="category" name="category" 
     class="form-control">

           <?php
           $categories = $wpdb->get_col('SELECT DISTINCT category FROM '.$ps_type_table_name);
             foreach ($categories as $category){ ?>

       <option value="<?php echo $category; ?>"><?php echo $category; ?></option>

               <?php
                  }
                  ?>

         </select><br><br>

         <label>Types :</label><br>
        <select id="type" name="type" class="form-control">

       </select><br><br>
 public function get_type_by_category(){
    //echo file_get_contents('php://input'); for testing
    if (isset($_POST['ctgry']) && !empty($_POST['ctgry'])) {
        global $wpdb;
        $result = "";
        $category = $_POST['ctgry'];
        $ps_type_table_name = $wpdb->prefix . 'ps_type';
        $types = $wpdb->get_col($wpdb->prepare('SELECT DISTINCT type FROM ' . $ps_type_table_name . ' WHERE category=%s', $category));

        foreach ($types as $type) {
            $result .= '<option value="' . $type . '">' . $type . '</option>';
        }
        echo $result;
    }
}
    add_action('wp_ajax_get_type_by_category','get_type_by_category');
    add_action('wp_ajax_nopriv_get_type_by_category','get_type_by_category');
   $(document).ready(function () {
   $('#category').change(function(){
    var ctgry=$(this).val();
    $.ajax({
        type:"post",
        url:ajaxurl,
        data:{'action':'get_type_by_category','ctgry':ctgry},
        cache:false,
        success:function (html) {
            $("#type").html(html);
            //alert(html);
        },
        error:function(request,string_error,error){
            alert(string_error);
        }

    });
});
$('#category').change();
});
    public function enqueue_scripts(){
 wp_enqueue_script('ps-admin-page-js', plugins_url('js/admin-page.js', dirname(__FILE__)),array('jquery'),'1.1.1',true);
 }
 add_action('wp_footer', 'enqueue_scripts');