14

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
7421 gold badge16 silver badges31 bronze badges
asked Jul 21, 2011 at 20:19

4 Answers 4

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.

answered Jul 21, 2011 at 20:21
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks this worked for me using GET. Couldn't get this figured out :/ thanks a bunch!
$.get('test.php', { album: this.title } i want to ask how to send two values
@M.chaudhry you may have found this already, but for future readers, this is 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 });
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'

answered Jul 21, 2011 at 20:21

Comments

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);
 }
});
answered May 21, 2016 at 3:10

Comments

0

You could use the following code:

$.ajax({
 type: 'POST',
 url: 'test.php', 
 data: { album: this.title }, 
 success: function(response) {
 //And insert the response in the html once received 
 content.html(response); 
 } 
});
Andy McRae
75212 silver badges21 bronze badges
answered Jul 30, 2022 at 2:17

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.