将mysql下拉列表中的值传递给另一个而不在db php上发布

yzuktlbb  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(288)

我有三张table:联系人、公司和联系人公司。我想,当一个用户从下拉列表中选择一个特定的公司,在下面的下拉列表中,他只能看到在该公司以前选择的联系人。我尝试使用全局函数,但成功率非常有限。如果可能的话,我想避免发布第一个查询结果的结果。

<p>Company <span>*</span></p>
  <span class="icon-case"><i class="fa fa-comments-o"></i></span>
  <select name="company">';
    echo '<option value=""></option>;'

        <?php

        $query="SELECT `name` FROM `companies` ORDER BY `name`ASC";
        $result=mysqli_query($connection,$query) or die ("Query to get data from sector table failed: ".mysql_error());

global $company_id;

        while ($row=mysqli_fetch_array($result)) {
        $company_id=$row["company_id"];

        $company_name=$row["name"];
        echo "<option value=\"$company_id\">
         $company_name
         </option>";
        }

        ?>

    </select>

  </div>  

       <p>Contact in Company <span>*</span></p>
  <span class="icon-case"><i class="fa fa-comments-o"></i></span>
  <select name="contact">';
    echo '<option value=""></option>;'

        <?php
        global $company_id;

        $query="SELECT * FROM `contact_company` WHERE 

`company_id=$company_id;";

        $result=mysqli_query($connection,$query) or die ("Query to get data from sector table failed: ".mysql_error());

        while ($row=mysqli_fetch_array($result)) {
        $company_id=$row["company_id"];
        $contact_id=$row["contact_id"];
        echo "<option value=\"$company_id\">
         $contact_id
         </option>";
        }

        ?>

    </select>

  </div>

有什么建议吗?向托马索问好

watbbzwu

watbbzwu1#

我猜你错过了什么。这是片段

<?php 

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS,DATABASE); if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error(); 

echo '<p>Company <span>*</span></p>
  <span class="icon-case"><i class="fa fa-comments-o"></i></span>
  <select name="company" id="company">';

    echo '<option value="">Please select</option>';

      $query="SELECT name,company_id FROM `companies` ORDER BY `name`ASC";
                $result=mysqli_query($connection,$query) or die ("Query to get data from sector table failed: ".mysql_error());

        while ($row=mysqli_fetch_array($result)) {
        $company_id=$row["company_id"];

        $company_name=$row["name"];
        echo "<option value=".$company_id.">".$company_name."   </option>";
        }

    echo '</select>

  </div>  

  <p>Contact in Company <span>*</span></p>
  <span class="icon-case"><i class="fa fa-comments-o"></i></span>
  <select name="contact" id="contact">';
    <option value=""> Please select</option>
    </select>';

?>

<script   src="https://code.jquery.com/jquery-3.3.1.min.js"   integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="    crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
    $('#company').on('change',function(){
        var companyid = $(this).val();
        alert('hello');
        if(companyid){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'id='+companyid,
                success:function(html){
                    $('#contact').append(html);

                }
            }); 
        }
    });
});

</script>

ajaxdata.php文件

<?php /* Connect to MySQL and select the database. */ 
$connection = mysqli_connect('localhost', 'root', '','demo'); if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
?>
<?php 
$company_id=$_POST['id'];
echo $query="SELECT * FROM `contact` WHERE company_id='".$company_id."'";

 $result=mysqli_query($connection,$query) or die ("Query to get data from sector table failed: ".mysqli_connect_error());

 while ($row=mysqli_fetch_array($result)) {
        $company_id=$row["company_id"];
        $contact_id=$row["contact_id"];
        echo "<option value=\"$company_id\"> $contact_id</option>";
        }
ffvjumwh

ffvjumwh2#

我建议使用ajax来获得所需的联系人。下面是一些有用的片段

$(document).ready(function(){
    $('#company').on('change',function(){
        var companyid = $(this).val();
        if(companyid){
            $.ajax({
                type:'POST',
                url:'ajaxData.php',
                data:'id='+companyid,
                success:function(html){
                    $('#conatct').html(html);

                }
            }); 
        }
    });
});

当公司发生变化时,我们可以调用ajax。下面是ajax的参数
类型:应该是post或get
url:php文件将根据seelct公司从数据中获取
数据:选定公司
sucess:在何处显示获取的数据。
因为公司是以post格式在php文件$company\u id=$\u post['id']中提供的;基于此,我们可以从数据库中提取数据。

相关问题