简体   繁体   English

在Ajax调用PHP之后更新值

[英]update value after Ajax call PHP

I want to update TextBox value on basis of selectChange 我想基于selectChange更新TextBox值

HTML: HTML:

<select name="abc" id="def">
    <option value="1">1st Record</option>
    <option value="2">2nd Record</option>
    <option value="3">3rd Record</option>
</select>
<input value="" name="final_value" id="final_value" />

JS: JS:

$("#selectboxid").change(function() {
  $.ajax({ url: "test.php", data:"value="+$("#def").val() , success: function(){
     $("#final_value").val(data);
  }});
});

PHP: PHP:

<?php
    function abc($data){
      // Database operations
      echo json_encode($response);
    }

    abc($_GET);
?>

With this approach value is updating very nicely But i have below points: 通过这种方法,值可以很好地更新,但是我有以下几点:

  1. above approach prints values and one can see in Network Tab. 上述方法会打印值,并且可以在“网络”选项卡中看到。 Is it a nice way to do it? 这是一个好方法吗?
  2. I also tried using return $response in PHP function but then response is null and console.log(data) in ajax shows null array. 我也尝试在PHP函数中使用return $response ,但是响应为null,ajax中的console.log(data)显示为空数组。

So, how to update Input value after Ajax without echoing . 因此,如何在Ajax之后更新输入值而不echoing I do not want that user shall see any type of response even in Network Tab. 我不希望该用户即使在“网络”选项卡中也不会看到任何类型的响应。

I have seen various websites which does the same and not showing any values in Network tab. 我见过各种网站,它们的功能相同,并且在“网络”选项卡中未显示任何值。 ** Is there any different approach to do this? **是否有其他方法可以做到这一点?

You can alternatively try like this 您也可以这样尝试

All HTTP requests can possible to watch on their own network. 所有HTTP请求都可以在自己的网络上进行监视。 You can restrict this 你可以限制这个

$(document).ready(function(){
    var params = {};
    params['value'] = $("#def").val();
    $.post('test.php', params, function(response){
        console.log(response);
        alert(response);
    });
});

It should be like this 应该是这样

$("#selectboxid").change(function() {
  $.ajax({ 
     url: "test.php", 
     data:{
      "value": $("def").val() 
     }, 
     success: function(){
       $("#final_value").val(data);
     }
  });
});

I've not tested this, but immediatly I've spotted a couple of things: 我尚未对此进行测试,但立即发现了两件事:

$("#selectboxid").change(function() {
    // .val is a function
  $.ajax({ url: "test.php", data:"value"+$("def").val() , success: function(data)    { //Your success callback needs to take an argument
     //data will be either a string or a JSON object, so depending on what you want from it, you'll probably not be able to just pass it to the val() function
     $("#final_value").val(data);
  }});
});

Not 100% complete I'm sure, but hopefully will give you a good starting point 我确定还没有100%完成,但希望能给您一个好的起点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM