dongqiao8417 2015-05-04 21:51
浏览 38
已采纳

echo默认选择值

Question

The current script I'm working on has one form to get the location then after that is submitted and confirmed without errors it will allow the next button to be available to the user prior to updating it to the database.

The main reason for this is to update the map, to give the user a chance to change their mind before proceeding.

The problem is, I want to keep the data they've already submitted. By doing this

                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Country</label>

                                <select class="form-control" name="country" value=<?php if (isset($_POST['locate'])) { echo '"'.$_POST['country'].'"'; } else { echo '""'; } ?> required>
                                    <?php include('includes/country_list.php');?>
                                </select>
                            </div>

                            <div class="form-group">
                                <label>Address</label>
                                <div>
                                    <input type="text" value=<?php if (isset($_POST['locate'])) { echo '"'.$_POST['address'].'"'; } else { echo '""'; } ?> class="form-control" data name="address" data-parsley-required="true" placeholder="First line address, postcode, zip code etc.." data-parsley-trigger="change">
                                </div>
                            </div>                                

                            <div class="form-group">
                                <label>Site Name</label>
                                <div>
                                    <input type="text" value=<?php if (isset($_POST['locate'])) { echo '"'.$_POST['site_name'].'"'; } else { echo '""'; } ?> class="form-control" data name="site_name" data-parsley-required="true" placeholder="//example: Fawley Refinery, Cottam Powerstation.." data-parsley-trigger="change">
                                </div>
                            </div>
                        </div>

The problem is, I don't want to touch the country_list.php I want to select a default value in the part. However, what I'm currently using does not work.

  • 写回答

2条回答 默认 最新

  • doulu1867 2015-05-04 22:09
    关注

    The problem is, I don't want to touch the country_list.php I want to select a default value in the part. However, what I'm currently using does not work.

    To meet your requirement of not modifying the PHP include, you're going to need JS...Something like the below should work.

    NOTE: I'm making the value attribute data-value to be valid HTML:

    <select class="form-control" name="country" data-value=<?php if (isset($_POST['locate'])) { echo '"'.$_POST['country'].'"'; } else { echo '""'; } ?> required>
        <?php include('includes/country_list.php');?>
    </select>
    

    Then, in JS, with jQuery you could do:

    $(document).ready( function() {
        var $select = $('select[name=country]'),
            value   = $select.data('value')
        ;
        $select.val(value);
    } );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法