How to update a mysql database without reloading page

前端 未结 4 705
抹茶落季
抹茶落季 2020-12-18 13:26

thanks for looking. I have a very long list of items, users click on an image (a plus sign) to add the item to their personal list. At the moment when they click the + it l

4条回答
  •  温柔的废话
    2020-12-18 14:19

    You would achieve this via JavaScript utilizing something referred to as "AJAX". An Asynchronous JavaScript And XML request allows JavaScript to send a request to another page and get the results from it.

    So utilizing ajax, you would go to the URL you wanted, and then you could display the message to a user.

    I use a library called jQuery to get something like this done

    $.ajax({
        'url': 'path/to/add-item.php', 
        'type': 'GET',
        'dataType': 'json', 
        'data': {itemid: xx}, 
        'success': function(data) {
          // what happens if the request was completed properly
        },
        'error': function(data) {
          // what happens if the request fails.
        }
      });
    

    Note that just because a request completes properly, doesn't mean the item was added as necessary.

    I would suggest you read up on the following to get a good understanding of how exactly to adapt this to your needs.

    http://json.org/

    http://api.jquery.com/jQuery.ajax/

    http://ca3.php.net/json_encode

提交回复
热议问题