weixin_33726313 2014-08-07 12:51 采纳率: 0%
浏览 13

如何使用Ajax

i hope someone can help me with this problem

i have a logic problem in code to solve

i have a value in jquery in like this

$(".test").click(function(){
var category=$(this).attr("data-id");
}

data-id value is 1

php codes (test.php)

function test()
{
$newdata=data-id + data-id;
return newdata;
}
echo test();

i need the code to call the data-id value from first jquery to put in the test() function

and the last , i need to put the echo -ed newdata value after the php function calculate the value to put it inside .test div with this second jquery

$("li").click(function(){
$('.test').text(data-id);
}

i need the complete step to step code for this problem , thanks for notice and helping! , sorry for bad english

  • 写回答

1条回答 默认 最新

  • weixin_33743880 2014-08-07 13:31
    关注

    You have several things here, the server processes the page (php) and sends it to the client. In order to get the client altered content back to the server (php) for processing, you would use ajax.

    First you need to get the value from your field provided by the client which you are already doing with:

    $(".test").click(function(){
      var category=$(this).attr("data-id");
    }
    

    Now, once you have that value, you need to pass it to your PHP page. To do this, we use ajax, you will need to alter your click function:

    $(".test").click(function(){
    
      var testDiv = $(this);
    
      //get the category value
      var category = testDiv.attr("data-id");
    
      //pass the category value to your php page
      $.ajax({
        url: 'test.php',
        method: 'POST',
        data: 'category='+category,
        success: function(returnedData) 
        {
        }
      });
    
    });
    

    You can now access the $_POST variable in your PHP function:

    //within test.php
    function test()
    {
      $newdata=$_POST['category'] + $_POST['category'];
      return $newdata;
    }
    echo test();
    //if category was 1, this would echo 2 (as 1 + 1 = 2)
    

    The last step is to put the returned value into your "test" div that was clicked:

    //to place the returned data from test.php
    //alter your .test click function
    $(".test").click(function(){
    
      var testDiv = $(this);
    
      //get the category value
      var category = testDiv.attr("data-id");
    
      //pass the category value to your php page
      $.ajax({
        url: 'test.php',
        method: 'POST',
        data: 'category='+category,
        success: function(returnedData) 
        {
          //repopulate the clicked div with the returned data
          testDiv.html(returnedData);
        }
      });
    
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题