简体   繁体   English

如何通过AJAX更改变量?

[英]how to change variable via AJAX?

I need advice with AJAX. 我需要AJAX的建议。

I have page where i use to show info in block like: 我有一个页面,我用来显示块中的信息,如:

<div1 container>
   <div2>
      some data
   </div2>
   <div3>
    <?php echo $variable;?>
   </div3>
</div>

and have another page that produce data: 并有另一个页面,产生数据:

function example( $array )
{
   ...some code...
return $answer
}

$variable = example($array);

i am sending AJAX query with some POST data from form and i need after operations get new variable and put it in same new params. 我正在从表单发送带有一些POST数据的AJAX查询,我需要在操作后获取新变量并将其放在相同的新参数中。

Here is my AJAX function: 这是我的AJAX功能:

function sendDataChild(btn) {
var form = $(btn).closest('FORM[name=answer-form]');
var data = form.serialize();
$.ajax({
    type: "POST",
    url: "get.php",
    dataType: "post",
    data: data,
    cache: false,
    success: function (data) {
        form[0].reset();
        //document.location.reload();
    },
    error: function (xhr, str) {
        alert('Возникла ошибка: ' + xhr.responseCode);
    }
});
return false;};

when i clear // and just reload page it works, but it is wrong way cause my page is quite big. 当我清除//只是重新加载页面它有效,但这是错误的方式导致我的页面很大。 Can some body advise how to make it? 有人可以建议如何制作吗?

I think you want the success function to output the data into the div where you want the variable to show: 我想你想要成功函数将数据输出到你想要变量显示的div中:

success: function (data) {
    form[0].reset();
    $("#divId").html(data);
}

Give a id to the div where you want to show the result 为要显示结果的div提供id

<div id="div3">

</div>

And you need to change your ajax 你需要改变你的ajax

 function sendDataChild(btn) {
var form = $(btn).closest('FORM[name=answer-form]');
var data = form.serialize();
$.ajax({
    type: "POST",
    url: "get.php",
    dataType: "post",
    data: data,
    cache: false,
    success: function (data) {
        $('div3').html(data);
    },
    error: function (xhr, str) {
        alert('Возникла ошибка: ' + xhr.responseCode);
    }
});
}

HTML page HTML页面

<div1 container>
   <div2>
      <!--some data-->
   </div2>
   <div3 id="div3">

   </div3>
</div>

PHP page PHP页面

function example( $array )
{
   //...some code...
   echo $answer;
   exit;
}

And your JavaScript 还有你的JavaScript

function sendDataChild(btn) {
var form = $(btn).closest('FORM[name=answer-form]');
var data = form.serialize();
$.ajax({
    type: "POST",
    url: "get.php",
    dataType: "post",
    data: data,
    cache: false,
    success: function (data) {
        $('div3').html(data);
    }
});
}

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

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