0

i am using WordPress platform with PHP AND MYSQL in order to create a website where i have a page that includes 4 dropdown lists that get it data from the MYSQL database and using javascript and AJAX i am trying to make these dropdown list dependent on each other where the user select from teh first one and based on the user's selection the second drop down display data.

the problem is that i have used 2 codes in order to make AJAX work without refreshing the hall page.

when i try to select from the first dropdown list in the debug mode it display:

404 drpdown_fetch_owner.php error page not found

directory structure :

/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

tables:

  • site_info:

    • siteID
    • siteNAME
    • ownerID
    • List item

owner_info:

  • ownerID
  • ownerNAME

problem :

after the user click on the first droplist variable ownerID in the AJAX stay empty and do not get any value.

i added var_dump($sql); under the SQL query in the dropdown_fetch_owner.php code and i got this statement in the debug mode:

/opt/lampp/htdocs/wordpress/wp-content/themes/wp-portfolio/dropdown_fetch_owner.php:6: array (size=0) empty

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>";
}


?>
Amin 3amin
  • 67
  • 1
  • 11

1 Answers1

0

In wordpress, use ajax(https://codex.wordpress.org/AJAX_in_Plugins)

Change

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

instead of url:"dropdown_fetch_owner.php",

Tamil Selvan C
  • 18,342
  • 12
  • 44
  • 63