在wordpress

时间:2017-03-18 09:08:18

标签: javascript php mysql wordpress

我正在使用带有PHP和MYSQL的WordPress平台来创建一个网站,其中我有一个页面,其中包含4个下拉列表,从MYSQL数据库获取数据并使用javascript和AJAX我试图使这些下拉列表依赖在用户选择第一个并且基于用户的选择的第二个下拉显示数据的彼此之间。

问题是我使用了2个代码,以便在不刷新大厅页面的情况下使AJAX工作。

当我尝试从调试模式中的第一个下拉列表中选择时,它会显示:

未找到404 drpdown_fetch_owner.php错误页面

目录结构:

/opt/lampp/htdocs/wordpress/wp-content/themes/wp-portfolio/search_info_location.php

/opt/lampp/htdocs/wordpress/wp-content/themes/wp-portfolio/dropdown_fetch_owner.php

表:

  • site_info:

    • 的siteID
    • 网站名称
    • OWNERID
    • 列表项

OWNER_INFO:

  • OWNERID
  • OWNERNAME

问题:

用户点击第一个下拉列表后

中的变量 ownerID 在AJAX中保持为空并且没有任何值。

我在 dropdown_fetch_owner.php 代码中的SQL查询下添加了var_dump($sql);  我在调试模式下得到了这个声明:

/opt/lampp/htdocs/wordpress/wp-content/themes/wp-portfolio/dropdown_fetch_owner.php:6: 数组(大小= 0)   空

code1:

    

<form method ="post" action ="" name="submit_form">
    <table border="0" width="30%">
        <tr>
           <td>Site Name</td>
           <td>Owner Name</td>
           <td>Company Name</td>
           <td>Subcontractor Name</td>
         </tr>
         <tr>
           <td><select id="site_name"  name = "site_name">

             <?php


                 $query_site_name =$wpdb->get_results("select DISTINCT siteNAME  from site_info");
                  foreach($query_site_name as $row)
                  {
        //           $site_name = (array)$site_name;
                   echo "<option value = '".$row ->ownerID."'>".$row->siteNAME."</option>";
                  } 

             ?>

            <!--create  dropdown list owner names-->
            </select></td>

            <td><select id="owner_name"  name ="owner_name">
            <option value="">Select Owner</option>


    <!--  the below part  of code work as it should   --!>
        <!--create  dropdown list site names-->

        <form method ="post" action ="" name="submit_form">
            <table border="0" width="30%">
                <tr>
                   <td>Site Name</td>
                   <td>Owner Name</td>
                   <td>Company Name</td>
                   <td>Subcontractor Name</td>
                 </tr>
                 <tr>
                   <td><select id="site_name"  name = "site_name">

                     <?php


                         $query_site_name =$wpdb->get_results("select DISTINCT siteNAME  from site_info");
                          foreach($query_site_name as $row)
                          {
                //           $site_name = (array)$site_name;
                           echo "<option value = '".$row ->ownerID."'>".$row->siteNAME."</option>";
                          } 
                     ?>

                    <!--create  dropdown list owner names-->
                    </select></td>

                    <td><select id="owner_name"  name ="owner_name">
                    <option value="">Select Owner</option>

  <script type="text/javascript">

// make Dropdownlist depend on each other
$(document).ready(function(){
    $('#site_name').change(function(){
         var ownerID = $(this).val();
         $.ajax({
            url:"dropdown_fetch_owner.php",
            method:"POST",
            data:{ownerID:ownerID},
            datatype:"text",
            success:function(data){
                $('#owner_name').html(data);
            }

         });
       });

    });

</script>

dropdown_fetch_owner.php:

<?php
 include_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-load.php');
 global $wpdb;

$sql =$wpdb->get_results("select * from owner_info where ownerID = '".$_POST['ownerID']."' ORDER BY ownerNAME");

echo '<option value="">Select Owner</option>';
foreach($sql as $row){

//while ($row = mysqli_fetch_array($result)) {
    echo "<option value = '".$row ->ownerID."'>".  $row->ownerNAME."</option>";
}


?>

1 个答案:

答案 0 :(得分:0)

在wordpress中,使用ajax(https://codex.wordpress.org/AJAX_in_Plugins

更改

url:"<?php echo get_stylesheet_directory_uri(); ?>/dropdown_fetch_owner.php",

而不是url:"dropdown_fetch_owner.php",