Ajax passing data to php script

前端 未结 3 1176
时光取名叫无心
时光取名叫无心 2020-11-27 04:38

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: \"         


        
相关标签:
3条回答
  • 2020-11-27 05:20

    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.

    0 讨论(0)
  • 2020-11-27 05:22

    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);
        }
    });
    
    0 讨论(0)
  • 2020-11-27 05:27

    Try sending the data like this:

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

    Then you can access it like

    $_POST['album']
    

    Notice not a 'GET'

    0 讨论(0)
提交回复
热议问题