简体   繁体   English

使用PHP,mySQL和JS填充下拉框时出现问题

[英]Problems while populate Drop down box with PHP, mySQL and JS

I look around a long time and found a solution that works for me, to populate a 2nd drop down box with data based from the 1st box. 我环顾了很长时间,发现了一个适用于我的解决方案,可以使用来自第一个框的数据填充第二个下拉框。

My first drop down: 我的第一个下拉列表:

$select   = $dbh->query("SELECT DISTINCT `pageid`, `name` FROM `site` ORDER BY `pageid` ASC");
$contents = $select->fetchAll(PDO::FETCH_OBJ);
foreach($contents as $content) {
< option value="< ?=$content->pageid;?>">< ?=$content->name;?>< /option>
}
< /select>
< script type="text/javascript">document.getElementById('site').value = "< ?=$_POST['site'];?>";< /script>

My second drop down: 我的第二个下拉列表:

< select id="page" name="page" class="form-control">< /select>
< script type="text/javascript">document.getElementById('page').value = "< ?=$_POST['page'];?>";</script>

My JS code: 我的JS代码:

$("#site").change(function() {
  $.ajax({
    url : "get_page.php?id=" + $(this).val(),
    type: 'GET',
    dataType:'json',
    success : function(data) {
      if (data.success) {
        $('#page').html(data.options);
      }
    }
  });
});

And the code to get the data for the 2nd drop down: 以及用于获取第二个数据的代码的下拉列表:

$id = $_GET['id'];
if (!isset($id) || !is_numeric($id))
  $reponse = array('success' => FALSE);
else {
  $query = $dbh->prepare("SELECT `pageid`, `page`, `pagename` FROM `site` WHERE `pageid` = :id ORDER BY `pagename` ASC");
  $query->execute(array(':id' => $id));
  $rows = $query->fetchAll(PDO::FETCH_ASSOC);
  $options = "";
  foreach ($rows as $row) {
    $options .= '< option value="'. $row[page] .'">'. $row[pagename] .'< /option>';
  }
  $response = array( 'success' => TRUE, 'options' => $options );
}
header('Content-Type: application/json');
echo json_encode($response);

Everything works fine. 一切正常。 When I press the submit button, I get the results I need. 当我按下提交按钮时,我得到了所需的结果。 BUT - the data from the 2nd drop down will be lost after pressing the submit button. 但是-按提交按钮后,第二个下拉列表中的数据将丢失。 If I want to another selection from the 2nd drop down, I need to change the 1st drop box, then I need to change it back and then I can make a new selection on the 2nd box ... 如果要从第二个下拉列表中选择另一个选项,则需要更改第一个下拉框,然后需要将其更改回去,然后可以在第二个框中进行新选择...

Sorry - sounds a little bit confused ... 抱歉-听起来有点困惑...

I try to explain again: 我尝试再次解释:

  1. 1) 1st Drop down Box - choose a Domain (Domain A) 1)第一个下拉框-选择一个域(域A)

    2) 2nd Drop down Box - data will be loaded and I choose a page (Page A) 2)第二个下拉框-数据将被加载,我选择一个页面(Page A)

    3) Press submit button 3)按提交按钮

    4) I get my results 4)我得到我的结果

    5) I want to choose anothe Page (Page B) 5)我要选择另一页(B页)

    6) 2nd Drop down Box is empty 6)第二个下拉框为空

    7) 1st Drop down Box is still filled with my first decision (Domain A) 7)第一个下拉框仍然充满我的第一个决定(域A)

    8) I need to coose Domain B and go back to Domain A to load the data for 2nd Drop down box again. 8)我需要整理域B并返回域A,以再次加载第二个下拉框的数据。

Is there a way that the data from the 2nd Drop down Box will be stored in the Drop down Box? 有没有办法将第二个下拉框中的数据存储在下拉框中?

I test already a few ways to populate data - but this code above (found here on Stackoverflow) is the only one, I get to work. 我已经测试了几种填充数据的方法-但是上面的这段代码(在Stackoverflow上找到)是唯一的一种,我可以开始工作。

Regards Torsten 问候托尔斯滕

Your data is lost, because POST request updates your document. 您的数据丢失,因为POST请求会更新您的文档。 To prevent losing data, you can use ajax for submit. 为了防止丢失数据,可以使用ajax进行提交。 Something like: 就像是:

$("button").on("click", function(){
    $.post(url, data);
});

For more details see http://api.jquery.com/jquery.post/ 有关更多详细信息,请参见http://api.jquery.com/jquery.post/

As mentioned by Sergio, you can use jquery ajax function for submitting the form OR after submission you can just check it out on page load that if 1st Drop down is having value > 0 you fire your My JS code. 正如Sergio所提到的,您可以使用jquery ajax函数来提交表单,或者在提交之后,您可以在页面加载时将其检出,如果1st Drop down的值> 0 ,则触发My JS代码。 So wrapping it all under one thing, it will go as: 因此,将所有内容包装在一件事中,它将变为:

$(document).ready(function(){
   var drpDwn1 = $('#site').val();
   if (drpDwn1 > 0) {
       // fire ajax function
       $.ajax({
       url : "get_page.php?id=" + $(this).val(),
       type: 'GET',
       dataType:'json',
       success : function(data) {
         if (data.success) {
          $('#page').html(data.options);
         }
        }
      });
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM