1

I have a foreach loop on index page that displays a list of few ids and in front of each id there is a link (named as detail) which is related to that id only.

The code on index page is

<?php if($request_detail): ?>
    <?php foreach($request_detail as $request): ?>
        <?php echo $request->requestid; ?>
        <a id="link" href="<?php echo base_url(); ?>recruiter/getrequestdetail/<?php echo $request->requestid; ?>"> Details </a>
    <?php endforeach; ?>              
<?php endif; ?>

<div id="container">
</div>

<script type="text/javascript">
    $("#link").click(function(e) {
      e.preventDefault(); 
      $.ajax({
        type: "get",
        url: "<?php echo base_url(); ?>recruiter/getrequestdetail/<?php echo $request->requestid; ?>",
        success: function(data) {
          $('#container').html(data); 
        }
      });
    });
</script>

The view i get from above code is

1  details
2  details
3  details

When a user clicks on detail link i wish to fetch the data of that id only from another page and display it under a particular div.

Controller

public function getrequestdetail($id)
        {
            $data['request_data'] = $this->recruiter_model->get_request_data($id);
            $this->load->view('recruiter/request_data_view',$data);
        }

Model

public function get_request_data($requestid)
    {
        /* query is getting executerd here */

            return $query->result();
    }       

The issue is that if i click on first details the id that is being passed in 3 and when i click on other details it gets redirected to another page and has the id 3

But what i want is that

when a user clicks on detail in front of 1 the data of id 1 should get displayed in container div within the same page,

when a user clicks on detail in front of 2 the data of id 2 should get displayed in container div within the same page,

when a user clicks on detail in front of 3 the data of id 3 should get displayed in container div within the same page,

Can anyone please tell how it can be done

9
  • i think your request id is blank before passing it echo somewhere to check if its not blank or null Commented Aug 26, 2016 at 10:43
  • echo $_GET['id']; in controller Commented Aug 26, 2016 at 10:43
  • use data: {id:<?php echo json_encode($request->requestid); ?>}, in your ajax Commented Aug 26, 2016 at 10:50
  • @Vishnu Bhadoriya i have updated my post and few more details can you please check it again Commented Aug 26, 2016 at 11:23
  • @ayush i have updated my post and few more details can you please check it again Commented Aug 26, 2016 at 11:23

2 Answers 2

2

Can anybody else have the Driving license number or same passport numbers as yours. Because they are identities and identities are always unique. Then how can you make all your links having same ID as they are coming from a foreach loop.

You must have to use class for the link. And also you were passing the last ID (last foreach iteration) to your ajax call.

<?php if($request_detail): ?>
    <?php foreach($request_detail as $request): ?>
        <?php echo $request->requestid; ?>
        <a class="link" href="<?php echo base_url(); ?>recruiter/getrequestdetail/<?php echo $request->requestid; ?>"> Details </a>
    <?php endforeach; ?>              
<?php endif; ?>


$(".link").click(function(e) {
      e.preventDefault(); 
      var url = $(this).attr('href');
      $.ajax({
        type: "get",
        url: url,
        success: function(data) {
          $('#container').html(data); 
        }
      });
    });
Sign up to request clarification or add additional context in comments.

2 Comments

the correct id is getting passed now, but still the 2nd and 3rd detail link are redirecting to another page
oh how could i miss such an important thing, thanks a lot it is working now
0

correct your controller

$this->load->view('recruiter/request_data_view',$data);
$data = $this->load->view('recruiter/request_data_view',$data, true);
return $data;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.