weixin_33725272 2017-02-08 13:50 采纳率: 0%
浏览 24

jQuery + PHP。 请求/响应

I would search for the solution, but I don't know what exactly do I have to search.

The task is to grab texts with ID's (#ftext_1,..._2,..._3,..._4) in html file and send them to php file. After some manipulation with texts in php file I have to insert them back into their ID's in html file. Here is the code:

var text_1_Replace = $('#ftext_1').text();
var text_2_Replace = $('#ftext_2').text();
var text_3_Replace = $('#ftext_3').text();
var text_4_Replace = $('#ftext_4').text();

$('#ID').on('click', function(){
var text= {
        ftext_1: text_1_Replace,
        ftext_2: text_2_Replace,
        ftext_3: text_3_Replace,
        ftext_4: text_4_Replace
    }
     var targetFile = 'ajax/file.php';

    $.ajax({
        method: 'post',
        url: targetFile ,
        data: JSON.stringify(text),
        contentType: 'application/JSON'
    }).done(function(data) {
       console.log(data);
    });
});

How do I edit .done function to place new texts in their old ID's(#ftext_1,..._2,..._3,..._4)? The variable with texts array is $result.

so the answer is :

}).done(function(data) {
        var text = JSON.parse(data);

        var text1 = text.ftext_1;
        var text2 = text.ftext_2;
        var text3 = text.ftext_3;
        var text4 = text.ftext_4;
        $('#ftext_1').text(text1);
        $('#ftext_2').text(text2);
        $('#ftext_3').text(text3);
        $('#ftext_4').text(text4);

So, the last update for the topic: The real and nice answer is:

.done(function(data) {
        var text = JSON.parse(data);
        $.each(text, function(i, val){
            $("#" + i).text(val);
        });

This code is the solution to my question in this topic. Thank you all, who responded!

  • 写回答

2条回答 默认 最新

  • weixin_33681778 2017-02-08 13:59
    关注

    When you change your received values in php you put them in an array so that you can call it later easily

    PHP

    $values = array("one"=>5,
                    "two"=>"something",
                    "three"=>$something);
    echo json_encode($values);
    

    You need to add

    dataType:'json' 
    

    in Jquery since you're returning json

    JQuery

    $.ajax({
        method: 'post',
        url: targetFile ,
        data: JSON.stringify(text), # or data: {"value1":value,"value2":value2},
        contentType: 'application/JSON',
        dataType:'json',
        success: function(response){
            console.log(response.one); #Will console 5
            console.log(response.two); #Will console "something"
            console.log(response.three); #Will console whatever $something holds in php
      }
      });
    

    You can call it however you want it (response) or (mydata)... And then you just type response.yourdata (that you declared in php)

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。