How do you submit more than one variable via Ajax?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you submit more than one variable via Ajax?

Post by simonmlewis »

Code: Select all

$.ajax({ url: '/ajax-search.php',
         data: {action: 'test'},
         type: 'GET',
         success: function(output) {
                      alert(output);
                  }
});
Something like this, but not sure how or where. OR how it compiles those two values.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you submit more than one variable via Ajax?

Post by Celauran »

simonmlewis wrote:Sure. But what is the "line of code" I had to pop in, and where.... so I can see if it works this end?
Not sure what line of code you're looking for. If you go back to the original jQuery with the AJAX call in it, where does that leave things?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you submit more than one variable via Ajax?

Post by simonmlewis »

I'm sorry I cannot see it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you submit more than one variable via Ajax?

Post by simonmlewis »

Like this?

Code: Select all

<script>
// This really belongs in a separate JS file, not embedded alongside PHP or HTML
$(document).ready(function() {
$('.home-search-input input').on('keyup', function(event) {
  var search_string = $(this).val();

  // Don't fire on very short strings
  if (search_string.length > 2) {
    var category = $('.home-search-cat select').val();
    $.ajax({
                url: '/ajax-search.php?search=' + search_string + '&category=' + category,
                method: 'GET'
            }).done(function(response) {
                $('#txtHint').html(response);
            });
  }
});

$('.home-search-cat select').change(function(event) {
	var category = $(this).val();
  var search_string = $('.home-search-input input').val();
    $.ajax({
                url: '/ajax-search.php?search=' + search_string + '&category=' + category,
                method: 'GET'
            }).done(function(response) {
                $('#txtHint').html(response);
            });
});
});
</script>			
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you submit more than one variable via Ajax?

Post by Celauran »

What can't you see?

What does your code look like now and what specifically is not working?
Post Reply