views:

38405

answers:

6

Hello, ive a problem using JQuery..

Im creating a HTML with a loop and it has a column for Action, that column is a HyperLink that when the user click the link call a JavaScript function and pass the parameters...

example:

<a href="#" OnClick="DoAction(1,'Jose');" > Click </a>
<a href="#" OnClick="DoAction(2,'Juan');" > Click </a>
<a href="#" OnClick="DoAction(3,'Pedro');" > Click </a>
...
<a href="#" OnClick="DoAction(n,'xxx');" > Click </a>

i want that function to Call an Ajax JQuery function with the correct parameters.

any help ??

+11  A: 

Using POST

function DoAction( id, name )
{
    $.ajax({
         type: "POST",
         url: "someurl.php",
         data: "id=" + id + "&name=" + name,
         success: function(msg){
                     alert( "Data Saved: " + msg );
                  }
    });
}

Using GET

function DoAction( id, name )
{
     $.ajax({
          type: "GET",
          url: "someurl.php",
          data: "id=" + id + "&name=" + name,
          success: function(msg){
                     alert( "Data Saved: " + msg );
                   }
     });
}

EDIT:

A, perhaps, better way to do this that would work (using GET) if javascript were not enabled would be to generate the URL for the href, then use a click handler to call that URL via ajax instead.

<a href="/someurl.php?id=1&name=Jose" class="ajax-link"> Click </a>
<a href="/someurl.php?id=2&name=Juan" class="ajax-link"> Click </a>
<a href="/someurl.php?id=3&name=Pedro" class="ajax-link"> Click </a>
...
<a href="/someurl.php?id=n&name=xxx" class="ajax-link"> Click </a>

<script type="text/javascript">
$(function() {
   $('.ajax-link').click( function() {
         $.get( $(this).attr('href'), function(msg) {
              alert( "Data Saved: " + msg );
         });
         return false; // don't follow the link!
   });
});
</script>
tvanfosson
You don't have to change anything for GET, except changing TYPE to GET. Querystring will automaticly be build.
Pim Jager
@Pim -- updated, thanks. I was still pretty new to jQuery when I wrote this.
tvanfosson
If you want to pass more than one parameter to the URL,use data as data:{id:'123' , name:"MyName"}where the 123 is the value of the parameter idmyName is the value for the parameter namethe value here can be string or variable having the value to be passed.just a simple trick to avoid string concatenation.
frictionlesspulley
This accepted answer looks excellent to me, but I can't help but wonder, how would this look with the onClick bindings done in jQuery instead of right in the <a> tags? I am led to believe jQuery is meant to handle the event bindings too.
Marcus
@Marcus - the way I would actually do it is generate the URL as the href attribute. Then I'd add a click handler that gets the url from the anchor's href element and posts/gets that via ajax, returning false from the click handler. I can add that as an alternative.
tvanfosson
A: 

Do you want to pass parameters to another page or to the function only?

If only the function, you don't need to add the $.ajax() tvanfosson added. Just add your function content instead. Like:

function DoAction (id, name ) {
    // ...
    // do anything you want here
    alert ("id: "+id+" - name: "+name);
    //...
}

This will return an alert box with the id and name values.

Leandro Ardissone
+2  A: 

If you want to do an ajax call or a simple javascript function, don't forget to close your function with the return false

like this:

function DoAction(id, name) 
{ 
    // your code
    return false;
}
tanathos
A: 

hello all i want to use $("#id").click(function(){}); in document.ready to register click event of some button for example. now i want to pass some parameters to this function that will be executed when the button is clicked. thanks in advance regards adeel

adeel
A: 

try something like this

#vote_links a will catch all ids inside vote links div id ...

<script type="text/javascript">

  jQuery(document).ready(function() {
  jQuery(\'#vote_links a\').click(function() {// alert(\'vote clicked\');
    var det = jQuery(this).get(0).id.split("-");// alert(jQuery(this).get(0).id);
    var votes_id = det[0];


   $("#about-button").css({
    opacity: 0.3
   });
   $("#contact-button").css({
    opacity: 0.3
   });

   $("#page-wrap div.button").click(function(){
Roko Mise
A: 

That's great!

It solved my Ajax problem with passing parameters in Symfony. Thank you very much!

Regards CAP

CAP