13

I am trying to send data to my PHP script to handle some stuff and generate some items.

$.ajax({  
    type: "POST",  
    url: "test.php", 
    data: "album="+ this.title,
    success: function(response) {
        content.html(response);
    }
});

In my PHP file I try to retrieve the album name. Though when I validate it, I created an alert to show what the albumname is I get nothing, I try to get the album name by $albumname = $_GET['album'];

Though it will say undefined :/

ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30
NeedHelp
  • 133
  • 1
  • 1
  • 4

4 Answers4

48

You are sending a POST AJAX request so use $albumname = $_POST['album']; on your server to fetch the value. Also I would recommend you writing the request like this in order to ensure proper encoding:

$.ajax({  
    type: 'POST',  
    url: 'test.php', 
    data: { album: this.title },
    success: function(response) {
        content.html(response);
    }
});

or in its shorter form:

$.post('test.php', { album: this.title }, function() {
    content.html(response);
});

and if you wanted to use a GET request:

$.ajax({  
    type: 'GET',
    url: 'test.php', 
    data: { album: this.title },
    success: function(response) {
        content.html(response);
    }
});

or in its shorter form:

$.get('test.php', { album: this.title }, function() {
    content.html(response);
});

and now on your server you wil be able to use $albumname = $_GET['album'];. Be careful though with AJAX GET requests as they might be cached by some browsers. To avoid caching them you could set the cache: false setting.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks this worked for me using GET. Couldn't get this figured out :/ thanks a bunch! – NeedHelp Jul 21 '11 at 20:36
  • $.get('test.php', { album: this.title } i want to ask how to send two values – M.chaudhry Mar 19 '14 at 15:02
  • 1
    @M.chaudhry you may have found this already, but for future readers, this is [JSON](http://en.wikipedia.org/wiki/JSON), so to send multiple values, you just add another one with a comma like so: `$.get('test.php', { album: this.title, song: that.title });` – Ian Jun 27 '14 at 14:42
12

Try sending the data like this:

var data = {};
data.album = this.title;

Then you can access it like

$_POST['album']

Notice not a 'GET'

rcravens
  • 8,320
  • 2
  • 33
  • 26
4

You can also use bellow code for pass data using ajax.

var dataString = "album" + title;
$.ajax({  
    type: 'POST',  
    url: 'test.php', 
    data: dataString,
    success: function(response) {
        content.html(response);
    }
});
kaushik
  • 903
  • 7
  • 16
0

$.ajax({
type: 'POST',
url: 'test.php', data: { album: this.title }, success: function(response) { content.html(response); } });

dipak
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 30 '22 at 13:03