0

I am tryin to pass multiple values in the below method but I am unable to get this to work. What is the correct syntax? I have tried

data: ('keyword='+$(this).val(),'id='10),

and

data: {'keyword='+$(this).val(),'id='10},

and

data: {'keyword='+$(this).val(),'&id='10}//I have also tried to replace the curly braces with brackets.

This works so I think its a syntax problem?

data: ('keyword='+$(this).val())

Here is my complete request. I am using GET, I have tried using POST to no avail.

$.ajax({
    type: "GET",
    url: "showhints.php",
    data: ('keyword='+$(this).val()),
    beforeSend: function() {
        $("#search-box").css("background","#FFF url(loading.gif) no-repeat 165px");
    },
    success: function(data) {
        $("#suggesstion-box").show();
        $("#suggesstion-box").html(data);
        $("#search-box").css("background","#FFF");
    }
});

4 Answers 4

5

The easiest is to use an object literal which will then be uri encoded internally

data: {keyword: $(this).val(), id : 10}
Sign up to request clarification or add additional context in comments.

Comments

1

You would do it like

...
data: {
            keyword: $(this).val(),
            id: 10
},
...

Comments

1

You can send multiple values like this

   $.ajax({
            type: "GET",
            url: "showhints.php",
            data: {'key':'value','key':'value',..},
            beforeSend: function(){
                $("#search-box").css("background","#FFF url(loading.gif) no-repeat 165px");
            },
            success: function(data){
                $("#suggesstion-box").show();
                $("#suggesstion-box").html(data);
                $("#search-box").css("background","#FFF");
            }

Comments

0

Try the following:

HTML:

<form action="">
    <input type="text" name="name">
    <input type="text" name="surname">
    <input type="submit" name="submitForm">
</form>

Ajax call:

var name = $('input[name]');
var surname = $('input[surname]');
$('input[submitForm]').click(function() {
    url: 'someLink.php',
    dataType: json,
    data: {
        name: name.val(),
        surname: surname.val()
    },
    success: function(data) {
        console.log(data)
    }
});

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.