【问题标题】:Dependent drop down displaying value id instead of displaying name on post submit从属下拉显示值 id 而不是在提交后显示名称
【发布时间】:2018-06-25 22:37:56
【问题描述】:

我有 3 列

  1. 国家(id,country_name)
  2. 州(id、country_id、state_name)
  3. files_dev (id,country_name,state_name)

国家和州我正在使用 onchange 事件处理程序来选择元素,然后让它在每次用户选择国家时向我的 PHP 脚本发出 AJAX 请求,它将显示相关的州名。

问题在于 POST SUBMIT 我想插入国家名称而不是其值 id

这是我正在使用的代码

<?php
        if ($country_result->num_rows > 0) {
        // output data of each row
        while($row = $country_result->fetch_assoc()) {
?>
    <option value="<?php echo $row["id"]; ?>">
<?php echo $row["country_name"]; ?></option><?php

我尝试将 $row["id"] 更改为 $row["country_name"],非常感谢这里的任何帮助

index.php

<form class="form-horizontal" method="post" id="signin" enctype="multipart/form-data">

                            <div class="control-group">
                                    <label class="control-label" for="inputPassword"  placeholder="Select Gram Panchayat" ><strong>Select Gram Panchayat</strong></label>
                                        <div class="controls">
                                        <?php
                                        require_once('../admin/lib/db.php');
                                        $country_result = $conn->query('select * from countries');
                                        ?>
                                            <select name="country" class="chzn-select" id="countries-list" required>

                                        <?php
                                            if ($country_result->num_rows > 0) {
                                        // output data of each row
                                        while($row = $country_result->fetch_assoc()) {
                                            ?>
                                            <option value="<?php echo $row["id"]; ?>"><?php echo $row["country_name"];?></option>
                                            <?php

                    echo "<option value='". $row['id']."'>".$row['country_name'] .'</option>';

                                        }

                                    }
                                    ?>
                                            </select>
                                    </div>
                          </div>

                                    <div class="control-group">
                                    <label for="inputPassword" class="control-label"  placeholder="Select Village" ><strong><strong>Select Village</strong></strong></label>
                                        <div class="controls">
                                            <select name="state" id="states-list" required>


                                            </select>
                                        </div>
                          </div>

                                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
                                            <script>
                                    $('#countries-list').on('change', function(){
                                        var country_id = this.value;
                                        $.ajax({
                                        type: "POST",
                                        url: "village_dev.php",
                                        data:'country_id='+country_id,
                                        success: function(result){
                                            $("#states-list").html(result);
                                        }
                                        });
                                    });
                                    </script>

village_dev.php

    <?php
   require_once('../admin/lib/db.php');
   error_reporting(0);
   $country_id = mysql_real_escape_string($_POST['country_id']);
   if($country_id!='')
   {
    $states_result = $conn->query('select * from states where country_id='.$country_id.'');
    $options = "<option value=''>Select Village</option>";
    while($row = $states_result->fetch_assoc()) {
    $options .= "<option value='".$row['state']."'>".$row['state']."</option>";
    }
    echo $options;
   }

   ?>

如果我在这里遗漏了什么,请告诉我,我想将值名称而不是 id 提交到数据库。

【问题讨论】:

  • 显示您的查询。我们也帮不了你。
  • if (isset($_POST['save'])){ $gp = $_POST['country']; $query = mysql_query("select * from files_dev where file = '$file' ")or die(mysql_error()); $count = mysql_num_rows($query); if ($count &gt; 0){ ?&gt; &lt;script&gt; alert('Files already Exist'); window.location = "upload_files_dev.php"; &lt;/script&gt; &lt;?php }else{ if(move_uploaded_file($file_loc,$folder.$final_file)){ mysql_query("insert into files_dev (id,gp,village,year,rec_type,file,type,size) values('','$gp','$village','$year','$rec_type','$file','$file_type','$file_size')")or die(mysql_error());
  • 对不起,如果你能理解我的查询,我也在插入其他记录。
  • 我认为这不是您在此处粘贴的相关代码。
  • 嗨 Ankit 兄弟,我已经粘贴了所有需要的代码,请帮助看看我们是否可以显示名称而不是将 id 传递给数据库

标签: php mysql ajax


【解决方案1】:

试试html5的data-属性:

将您的国家/地区下拉代码更改为:

<?php
require_once('../admin/lib/db.php');
$country_result = $conn->query('select * from countries');
?>
<select name="country" class="chzn-select" id="countries-list" required>
<?php
if ($country_result->num_rows > 0) {
    // output data of each row
    while($row = $country_result->fetch_assoc()) {
        echo '<option value="'.$row["country_name"].'" data-countryID="'.$row["id"].'">'.$row["country_name"].'</option>';
    }
}
?>
</select>

并使用以下内容更改您的 ajax 脚本:

<script>
    $('#countries-list').on('change', function(){
        var country_id = $('#countries-list option:selected').data( 'countryID' );
        $.ajax({
            type: "POST",
            url: "village_dev.php",
            data:'country_id='+country_id,
            success: function(result){
                $("#states-list").html(result);
            }
        });
    });
</script>

【讨论】:

  • 抱歉延迟回复,如果我使用数据属性它仍然是相同的行为第二个下拉列表将为空白,必须传递 ID 属性以获取第二个下拉值,但是在插入数据库时​​,我想插入名称而不是 ID,如何解决这个问题??否则我必须遵循另一种使用文本文件或 JSON 数据的方法
  • 感谢 Ankit 兄弟,现在它工作正常我犯的错误是我调用 country_id 而不是 id,现在第二个下拉列表也已填充,我可以将 country_name 传递给数据库.感谢您的支持和帮助:)
  • 很高兴为您提供帮助:)。如果它适合您,请接受作为答案。
【解决方案2】:

这里的问题是你想得到一些你输入的值之外的信息

这就是为什么你会得到 id 而不是州名!

<form class="form-horizontal" method="post" id="signin" enctype="multipart/form-data">

                            <div class="control-group">
                                    <label class="control-label" for="inputPassword"  placeholder="Select Gram Panchayat" ><strong>Select Gram Panchayat</strong></label>
                                        <div class="controls">
                                        <?php
                                        require_once('../admin/lib/db.php');
                                        $country_result = $conn->query('select * from countries');
                                        ?>
                                            <select name="country" class="chzn-select" id="countries-list" required>

                                        <?php
                                            if ($country_result->num_rows > 0) {
                                        // output data of each row
                                        while($row = $country_result->fetch_assoc()) {
                                            ?>
                                            <option value="<?php echo $row["country_name"]; ?>"><?php echo $row["country_name"];?></option>
                                            <?php

                    echo "<option value='". $row['country_name']."'>".$row['country_name'] .'</option>';

                                        }

                                    }
                                    ?>
                                            </select>
                                    </div>
                          </div>

                                    <div class="control-group">
                                    <label for="inputPassword" class="control-label"  placeholder="Select Village" ><strong><strong>Select Village</strong></strong></label>
                                        <div class="controls">
                                            <select name="state" id="states-list" required>


                                            </select>
                                        </div>
                          </div>

                                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
                                            <script>
                                    $('#countries-list').on('change', function(){
                                        var country_id = this.value;
                                        $.ajax({
                                        type: "POST",
                                        url: "village_dev.php",
                                        data:'country_id='+country_id,
                                        success: function(result){
                                            $("#states-list").html(result);
                                        }
                                        });
                                    });
                                    </script>

所以现在您可以将您需要的内容发布到您的数据库中,您输入的值将是该州的名称!

干杯

【讨论】:

  • 回显“".$row['country_name'] .'';如果我们使用它,则依赖下拉列表会出现问题,我尝试使用它来表示它正在工作,但仅适用于我正在获取 id 的国家/地区
【解决方案3】:

所以您使用的是&lt;option value="&lt;?php echo $country["id"]; ?&gt;"&gt;&lt;?php echo $country["name"]; ?&gt;&lt;/option&gt;,当您提交表单时,您会在 PHP 有效负载中获得 ID。收到 id 后,运行选择查询以获取与 id 关联的名称。这样,您无需更改其他选择的 javascript,因为您没有更改 HTML 和 Javascript 中的任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多