Ajax passes data to a PHP script

I am trying to send data to my PHP script to process some things and create some elements.

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

In my PHP file, I am trying to get the album name. Although when I check it, I created a warning to show that albumname I get nothing, I try to get the album name $albumname = $_GET['album'];

Although it will say undefined: /

+8
jquery ajax php
source share
3 answers

You are sending an AJAX POST request, so use $albumname = $_POST['album']; on your server to get the value. I would also recommend that you write such a query to ensure proper coding:

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

or in shorter form:

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

and if you want to use a GET request:

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

or in shorter form:

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

and now on your server you can use $albumname = $_GET['album']; . Be careful though with AJAX GET requests, as some of them may be cached. To avoid caching them, you can set the cache: false parameter.

+30
source share

Try sending the data as follows:

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

Then you can access it, for example

 $_POST['album'] 

Please note that it is not 'GET'

+9
source share

You can also use the following code to transfer data using ajax.

 var dataString = "album" + title; $.ajax({ type: 'POST', url: 'test.php', data: dataString, success: function(response) { content.html(response); } }); 
+2
source share

All Articles