duanfan5012 2019-02-26 17:22
浏览 169
已采纳

获取选定的下拉值 - PHP

I have a dropdown populated with values I get from the database. I need to perform some queries based on the selected option, but first I need to save the selected value on a new variable.

This is my dropdown:

<form name="dashboard_form" action="" id="dashboard_form" method="post">
    <tr>
     <td><?php _e('Select City')?>:</td>
     <td>
        <select class="selected_city" name="selected_city" style="margin: 20px;width:300px;" required>
        <?php
          foreach ( $all_cities as $city ) 
           {
            echo '<option value='.$city->ID.'>'.$city->name.'</option>';
           }
         ?>
        </select>
        </td>
     </tr>
</form>

*$all_cities is an array with all the cities.

My jQuery:

<script>
            jQuery("#dashboard_form").validate({
                ignore:'',
                rules: {
                    'selected_city': {
                     required: true
                }},
                messages: {
                    'selected_city': "Please Select a City",
                },
                errorPlacement: function(error, element) {
                    jQuery(element).closest('tr').next().find('.error_label').html(error);
                }
            });
</script>

How can I save on a $new_variable my selected value from the dropdown?

EDIT 1:

I added the following to

$(document).ready(function () {
    var selected_city;

    $('.selected_city').change(function() {
        selected_city = $(this).val();
        alert(selected_city);
    });
});

The alert(selected_city) prints the correct city, how can I use this selected_city on the PHP?

EDIT2: The problem is the dropdown is not submitting anything, so I created a Button to force it to Submit:

 <input type="submit" value="<?php _e('Send Now')?>" name="send_now_button" id="send_now_button" class="button button-primary">

Then:

if(isset($_POST['send_now_button'])){
        if(isset($_POST['selected_city'])) { $selected_city = $_POST['selected_city']; } else {$selected_city = 'Lisboa';  } }

Now when I echo $selected_cities_id; it gives the correct value!

  • 写回答

1条回答 默认 最新

  • doupian9798 2019-02-26 18:35
    关注

    Your HTML is invalid. <tr> can't be the child of <form>, it has to be the child or <table>, <tbody>, <thead>, or <tfoot>. This could be preventing the <select> from being treated as part of the form, so nothing is being submitted.

    You need to put the form around the entire table.

    <form name="dashboard_form" action="" id="dashboard_form" method="post">
        <table>
            ...
            <tr>
                <td><?php _e('Select City')?>:</td>
                <td>
                    <select class="selected_city" name="selected_city" style="margin: 20px;width:300px;" required>
                    <?php
                    foreach ( $all_cities as $city ) 
                    {
                        echo '<option value='.$city->ID.'>'.$city->name.'</option>';
                    }
                    ?>
                    </select>
                </td>
            </tr>
            ...
        </table>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮