Sending data via AJAX

折月煮酒 提交于 2020-01-09 11:24:07

问题


i have a build a javascript which does following: Get content via ajax->php->sql and show it on index.php after clicking the content there will be shown new content.

Now I want to have a function which sends data after content is clicked to a php which will do something in in the db. How can i create a function which will send data? Thank you!

This is my code which shows content:

<script id="source" language="javascript" type="text/javascript">
function laden(){
$(function() 
{

$.ajax({                  
`usr_id`                    
  url: 'content/get.php',              


  dataType: 'json',                   
  success: function(data)         
  {
    var id = data[0];             
    var name = data[1];

    var count = data[3];


    $('#output').html('<div onclick="laden('+id+')" id="content"></div>');

  } 
});

}); } `


回答1:


You can send data to the callback script, specified in the URL, by including values in the jQuery.ajax data setting. Depending on what type of request you're making, this data will either be included in the $_GET or $_POST global variables. For example, to POST data to your callback script, you could do:

$.ajax({                    
  url: 'content/get.php',     
  type: 'post', // performing a POST request
  data : {
    data1 : 'value' // will be accessible in $_POST['data1']
  },
  dataType: 'json',                   
  success: function(data)         
  {
    // etc...
  } 
});

For more information, please read up on the jQuery.ajax function's documentation at https://api.jquery.com/jQuery.ajax/



来源:https://stackoverflow.com/questions/22179620/sending-data-via-ajax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!