0

I would like to send an ajax command to a php script each time when a button is clicked. The divs are called r, l, t, b for the different directions. So far I have tried using the code below. But somehow it doesn't work. I have not a lot of experience in jquery and that's why I am asking here for a simple solution. I could write 4 times the same function but this is certainly not what I am looking for.

$(document).ready(function(){

function control(id){
    $.ajax({
        type: "POST",
        url: "control.php",
        data: {id: "1"},
        success:  function(data){
            alert(data);
        }
    });
}

$('#r').mousedown(function(){
    control($(this).attr('id'));
});

});
3
  • 2
    Describe "it doesn't work" Commented Jul 1, 2014 at 11:14
  • Does your php not catch it? Does the AJAX call fire? Have you put an {error:function(){}} into the ajax call to see if it actually was being fired? Commented Jul 1, 2014 at 11:17
  • What are you sending back from control.php. Json? Html? Commented Jul 1, 2014 at 11:18

3 Answers 3

3

Well first...

Just give all your divs a class

 <div id="r" class="direction"></div>
  <div id="l" class="direction"></div>
    <div id="t" class="direction"></div>
      <div id="b" class="direction"></div>

Then rewrite your jquery

$(document).ready(function(){
      $('.direction').mousedown(function(){
          $.post("control.php",{id: $(this).attr('id')},function(msg){
               alert(msg);
          });
      });
  });

If you happen to be receiving JSON from the servers.... You must specify the dataType as 'json' or 'jsonp' for cross domain json and use $.ajax

function control(theid){
$.ajax({
    type: "POST",
    url: "control.php",
    data: {id: theid},
    dataType: 'json',
    success:  function(msg){
        alert(msg);
    }
});

}

Or if you wanna keep it simple like the above $.post example.....use getJSON

   $(document).ready(function(){
      $('.direction').mousedown(function(){
          $.getJSON("control.php",{id: $(this).attr('id')},function(msg){
               alert(msg);
          });
      });
  });
Sign up to request clarification or add additional context in comments.

1 Comment

This was a very good answer as well, and its even better because you bind the event to a class instead of multiple id's. my answer works, but in my opinion this was the proper answer.
2

It's hard to know exactly what you are asking, but basically you want a multipurpose click event for multiple divs.

in jQuery, you can add multiple selectors by separating the selectors with commas like so:

$('#id, .class, element')

Here is an example that uses less code to provide the same functionality, if I understood your question correctly.

$(function(){
    $('#r,#l,#t,#b').mousedown(function(){
        $.post('control.php',{id: $(this).attr('id')},function(data){
            //handle data callback
            alert(data);
        });
    });
});

I used $.post instead of $.ajax , but bear in mind if you use the simpler methods like post, get, or getJSON, you will not be able to assign an error callback as far as I'm aware. Hopefully this changes in future releases of jQuery.

Comments

1
function control(my_id){
    $.ajax({
        type: "POST",
        url: "control.php",
        data: {id: my_id},
        success:  function(data){
            alert(data);
        }
    });
}

Using $_POST['id']; you should get the information you are looking for.

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.