【问题标题】:chained select list with input field - and values taken from database带有输入字段的链式选择列表 - 以及从数据库中获取的值
【发布时间】:2013-01-29 18:35:02
【问题描述】:

我想要达到的目标:

使用链式选择列表将数据插入数据库表(选项值取自数据库表)

要求:对于第一个选择列表(“tip_cheltuiala”),可用选项值必须仅是已插入行中未使用的选项值(可用选项为:选项 1、2和 3;我已经用选项 1 和 3 插入了行,现在只有选项 2 必须可用)

1.选择列表“tip_cheltuiala”:

echo '<select name="tip_cheltuiala'.$row_chelt['id_factura'].'" id="tip_cheltuiala'.$row_chelt['id_factura'].'" class="selectContentIncasare">'.$opt_mod_inchidere->TipCheltuiala().'</select>';

以及该选择列表的功能:

class SelectListModInchidere{
public function TipCheltuiala(){
                    //looking for options that are  already in the table
        $stmt_chelt_inchisa = $this->conn->prepare('SELECT tip_cheltuiala FROM cheltuieli_mod_inchidere');
        $stmt_chelt_inchisa->execute(array());
        $results_chelt_inchisa = $stmt_chelt_inchisa->fetchAll();
        foreach($results_chelt_inchisa as $row_chelt_inchisa) {
            $chelt_inchisa[] = $row_chelt_inchisa['tip_cheltuiala'];
        }
        print_r($chelt_inchisa); // returns options 1 and 3

        for($i=0; $i < count($chelt_inchisa); $i++){    

            $stmt_tip_chelt = $this->conn->prepare('SELECT * FROM mi_categ_cheltuiala 
            WHERE tip_cheltuiala <> :chelt_inchisa');
            $stmt_tip_chelt->execute(array('chelt_inchisa' => $chelt_inchisa[$i]));
            $tip_cheltuiala = '<option value="0">selectati ...</option>';
            while($row_tip_chelt = $stmt_tip_chelt->fetch()) {
                $tip_cheltuiala .= '<option value="' . $row_tip_chelt['tip_cheltuiala'] . '">' . $row_tip_chelt['tip_cheltuiala'] . '</option>';
            }
            return $tip_cheltuiala;
        }
    }
}
$opt_mod_inchidere = new SelectListModInchidere();

我有第一个问题:选择列表填充了选项 2(这是正确的),但也填充了选项 3 - 我不知道为什么。

2。选择列表“mod_inchidere”: 根据选择列表“tip_cheltuiala”中的选定选项返回选项值

echo '<select name="mod_inchidere'.$row_chelt['id_factura'].'" id="mod_inchidere'.$row_chelt['id_factura'].'" class="selectContentIncasare">
      <option value="0">selectati ...</option>
      </select>';

以及该选择列表的函数(与函数 TipCheltuiala 属于同一类):

public function ModInchidere(){
        $stmt_mod_inch = $this->conn->prepare('SELECT * FROM mi_mod_inchidere WHERE categorie_cheltuiala = :categorie_cheltuiala');
        $stmt_mod_inch->execute(array('categorie_cheltuiala' => $_POST['id_categ_cheltuiala']));
        $mod_inchidere = '<option value="0">selectati ...</option>'; 
        while($row_mod_inch = $stmt_mod_inch->fetch()) {
            $mod_inchidere .= '<option value="' . $row_mod_inch['mod_inchidere'] . '">' . $row_mod_inch['mod_inchidere'] . '</option>';
        }
        return $mod_inchidere;
    }

3.最后一步:根据选择列表“mod_inchidere”中的选定选项,我需要返回一个与选择列表“mod_inchidre”中的选项相关的值(也存储在数据库中),并将该值放入输入字段中,因此用户可以(如果他愿意)修改该值。

在那一步我不知道如何完成。 我可以将该值放在另一个选择列表中,但是:用户无法修改该值,也不是这样做的方法。

请帮帮我。

表结构

mi_categ_cheltuiala -> |编号 | tip_cheltuiala | categorie_cheltuiala |

mi_mod_inchidere -> |编号 | categorie_cheltuiala | mod_inchidere |

cheltuieli_mod_inchidere(我需要插入数据的表格)-> |编号 | tip_cheltuiala | categorie_cheltuiala | mod_inchidere |价值 |

要获得我需要放入输入字段的值,我需要为字段“mod_inchidere”查询表“mi_categ_valoare”

mi_categ_valoare -> |编号 | mod_inchidere |价值 |

$_POST['id_categ_cheltuiala'] -> 解释:

通过 jQuery,我获取在此选择列表“tip_cheltuiala”中选择的内容并将数据发送到方法 TipCheltuiala()

<script type="text/javascript">
$(document).ready(function(){
    $("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").attr("disabled","disabled");
            $("select#tip_cheltuiala<?php echo $row_chelt['id_factura']; ?>").change(function(){
            $("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").attr("disabled","disabled");
            $("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").html("<option>asteptati ...</option>");
            var id_categ_cheltuiala = $("select#tip_cheltuiala<?php echo $row_chelt['id_factura']; ?> option:selected").attr('value');
            $.post("class/select_mod_inchidere.php", {id_categ_cheltuiala:id_categ_cheltuiala}, function(data){
            $("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").removeAttr("disabled");
            $("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").html(data);
        });
    }); 
</script>

然后我使用将调用方法 TipCheltuiala() 的服务文件

【问题讨论】:

  • 你能分享所有相关表的表模式吗? tip_cheltuialami_categ_cheltuialami_mod_inchidere
  • 查看原文中的 LE
  • 使用$_POST['id_categ_cheltuiala'],用户是如何访问categorie_cheltuiala的?
  • 我使用 ajax 调用来获取数据。我正在路上,稍后我会带着剧本回来。
  • @SparKot - 查看原始帖子的编辑 $_POST['id_categ_cheltuiala']

标签: php sql


【解决方案1】:

说实话:非英语的命名约定让我头晕目眩:)

问题:There I have the first issue: the select list is populated with option 2 (that is correct) but also with option 3 - I can't figure out why.

使用下面的单个查询来丢失tip_cheltuila

SELECT 
    tip_cheltuila
FROM
    mi_categ_cheltuiala
WHERE
    tip_cheltuila NOT IN (SELECT 
            tip_cheltuiala
        FROM
            cheltuieli_mod_inchidere);

需要研究其他问题...

【讨论】:

  • 我会稍后再试。我会记住您对命名约定的建议:)
  • 获取丢失tip_cheltuiala 的查询正在正常工作-谢谢。你有时间研究其他问题吗?
猜你喜欢
  • 2015-10-19
  • 1970-01-01
  • 2013-11-25
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
相关资源
最近更新 更多