简体   繁体   English

CodeIgniter-根据单击的链接从下拉列表中预先选择一个值

[英]CodeIgniter - pre select a value from dropdown based on link clicked

I'd like some help please. 请给我一些帮助。 I'm having this link 我有这个连结

<?php echo anchor('contact#apply', 'Apply for Job'); ?>

in my Careers page /view, which redirects to the Contact , where I have this dropdown menu in my form 在我的“ Careers页面/视图中,该页面重定向到“ Contact ,在我的表单中有此下拉菜单

$reasons = array(
            'request-feedback'  => 'I'd like more information please',
            'request-a-demo'=> 'I want a demo of your services', 
            'work-with-us'  => 'I am interested in working for youy company', // * this is the option I'm interested in
);
<?php echo form_label('Reason for getting in touch with us', 'reason'); ?>
<?php echo form_dropdown('reason', $reasons, null, 'class="form-control"'); ?> // * when user applies for job should have the 3rd option pre selected instead of null
<?php echo form_error('topic'); ?>

What I'd like to do is when a user clicks on that link to redirect him to the contact form and have the work-with-us option pre selected, but only in this case , in any other case the default should be the first one. 我想做的是,当用户单击该链接将其重定向到联系表单并预先选择“ work-with-us选项时, 但仅在这种情况下 ,在任何其他情况下,默认值应是第一个一。

How can I do this ? 我怎样才能做到这一点 ?

Note: both Careers and Contact pages have this structure 注意:“ Careers和“ Contact pages均具有此结构

careers/index // i.e. controller/method
contact/index

so basicaly there are two different controllers 所以基本上有两个不同的控制器

You can do this in this way. 您可以通过这种方式进行。 In the url, there should be an 'id' to identify that it coming from 'contact/apply' link.You can do it in this way 在网址中,应该有一个“ id”来标识它来自“ contact / apply”链接。您可以通过这种方式进行操作

<?php echo anchor('contact/apply?id=contact', 'Apply for Job'); ?>

in your 'apply' method in 'contact' controller, it can check the id 在“联系”控制器的“应用”方法中,它可以检查ID

public function apply(){
   if(isset($_GET['id'])){
     $id=$_GET['id'];
     if($id=='contact'){
      $selected='work-with-us';
     }
     else{
       $selected='request-feedback';
     }
 }
 else{
   $selected='request-feedback';
 }

  //the data send to view page
  $data['selected_val']=$selected;
  $this->load->view('view_page', $data);
}

then in your 'view_page' in the relevant drop down, it can set the selected value 然后在您的“ view_page”的相关下拉菜单中,可以设置所选值

<?php echo form_dropdown('reason', $reasons, $selected_val, 'class="form-control"'); ?>

Hope this will help. 希望这会有所帮助。

Way to the second case.. 第二种情况

<?php if($selected_val!='work-with-us'){ ?>
   <script>
     $(document).ready(function(){
         $("#depend_on_select").hide();
     });
   </script>
<?php } ?>

<div id="depend_on_select">
   <input type="file" name="cv">
</div>

In here the div that contain cv will show only $selected_val='work-with-us'. 在这里,包含cv的div仅显示$ selected_val ='work-with-us'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM