0

I want to send the selected item from Bootstrap dropdown in POST to my controller, but somehow it is not being sent.

In my dropdown I fetch records from the database in <li></li> tag, like this:

 <li class="dropdown">

      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Your Sites <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li ><a href="#"><?php

    foreach($sites as $site)
    {

    echo "<li class='specialLink' id='$site->site_key'>".$site->site_key."</li>";
    }?></a></li>

      </ul>
    </li>     

This is my script to send POST data:

<script type="text/javascript">

$( ".specialLink" ).click(function() {
    var value = this.id; //get value for throw to controller
    alert(value);  

    $("#specialLink").submit(function(){
        $.ajax({
            type: "POST", //send with post
            url: "<?php echo site_url('customer/dashboard') ?>",  
            data: "value=" + value,
        });
    });
});

</script>

But I don't send anything in POST data at my controller, nor do I get any error message in the console.

20
  • Are you clicking on the element that has id #specialLink? Doing a .submit(function(){}) just sets the event handler it doesn't execute the function immediately Commented Feb 27, 2016 at 5:50
  • what is #specialLink? a form? if so.. no need to submit it.. just directly do $.ajax(... after your click function Commented Feb 27, 2016 at 5:51
  • @PatrickEvans i click on that <li> link and i want data to be passed as POST Commented Feb 27, 2016 at 5:52
  • Then just do the $.ajax call, don't put it in the event handler Commented Feb 27, 2016 at 5:53
  • 1
    This "," after "value", please take it out. If you don't have another property after "data:", this is not necessary and it is a syntax error. Commented Feb 27, 2016 at 5:53

2 Answers 2

1

I think you're not using submit correctly. The submit function binds an event handler to the submit javascript event. Read more at https://api.jquery.com/submit/.

You just want to do the ajax request when $(.specialLink) is clicked, so try:

 $( ".specialLink" ).click(function() {
    var value = this.id; //get value for throw to controller
    $.ajax({
      type: "POST", //send with post
      url: "<?php echo site_url('customer/dashboard') ?>",  
      data: "value=" + value, 
    });
});
Sign up to request clarification or add additional context in comments.

7 Comments

i did this but in my profiler i dont see any POST data
This "," after "value", please take it out. If you don't have another property after "data:", this is not necessary and it is a syntax error.
@Rajan also be sure to put it after the html has loaded. Or enclose it with a $(document).ready
@Rajan, can you clarify what you mean when you say you don't see any POST data in your profiler? Do you see a POST request, but the request has no data sent along with it?
@Sohan Codeigniter Profiler show GET AND POST DATA on browser. when i click on dropdown i dont see any data in that
|
0

you can use this code instead to send your data to php file:

 <a href="#" onclick="postExample(); return false;">Your Link here</a>

     function postExample() {
     $.post('url', {"postfield1":"val1"}, function(resp) {
         //Do something with the AJAX response
     });
 }

or

 <a href="#" onclick="$.post('url', {"postfield1":"val1"},; return false;">Your Link here</a>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.