1

what I want to do is

<a href="#" onClick="myjqueryfunc(data1,data2)"></a>

and in the jquery

 myjqueryfunc(variable1,variable2) { 
var  formData = "name=variable1&age=variable2"; 
$.ajax({
    url : "AJAX_POST_URL",
    type: "POST",
    data : formData,
    success: function(data, textStatus, jqXHR)
    {
    } }

so how can i do this ?

3
  • 'name=' + variable1 + '&age=' + variable2 is the most straightforward. But I'd rather use object notation here: formData = { name: var1, age: var2 }. Commented Jul 23, 2015 at 19:27
  • if you want to get data from form then use form.serialize() Commented Jul 23, 2015 at 19:29
  • could you write for me a complete example ? (of course except php page) Commented Jul 23, 2015 at 19:32

3 Answers 3

1

Javascript doesn't substitute variables inside strings, you would have to use string concatenation. But in the case of $.ajax, it's better to use an object.

myjqueryfunc(variable1,variable2) { 
    var  formData = {name: variable1, age: variable2};
    $.ajax({
        url : "AJAX_POST_URL",
        type: "POST",
        data : formData,
        success: function(data, textStatus, jqXHR)
        {
        } 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
<a href="#" data-name="x" data-age="12" id="smth"></a>

in js

$("#smth").click(function(){
var name=$(this).data('name');
var age=$(this).data('age');

$.ajax({
    ...
    data : { var1 :name , var2:age  },
    ...
});

});

Comments

0

You can send data with the data parameter

$.ajax({
    ...
    data : { foo : 'bar', bar : 'foo' },
    ...
});

2 Comments

no, you misunderstood me. i want send data over link to jquery. not over jquery to other php page. i know how can i do it. but i cant send data over link to jquery.
The arguments to the function send the data to jquery.

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.