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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐