简体   繁体   English

对PHP的AJAX调用接收“先前”发布的数据

[英]AJAX call to PHP receives the “previous” posted data

So I post data from a form using the jQuery form extension. 因此,我使用jQuery表单扩展名发布了表单中的数据。 This code is executed when the user presses ["Save"] on the HTML page. 当用户在HTML页面上按下["Save"]时,将执行此代码。

$('#myform').ajaxForm({
    async: false,
    cache: false,
    target: $(this).find('.save-response'),
    beforeSubmit: function(arr,$form){
        json_ = JSON.stringify (vars.table_settings[aux.getTableId()].by_ix); //, null, 4);
        $form.find('.user-settings').val(json_);
        $form.find('.save-response').stop(true,true).hide().show();
        alert (json_);
    },
    success: function(){
        $(this).fadeOut(15000);
    },

As you see, I use the alert() function to check what is actually being delivered in the AJAX call, to the destination PHP file. 如您所见,我使用alert()函数检查AJAX调用中实际传递给目标PHP文件的内容。 What I see is the following: 我看到的是以下内容:

[
...
{"id":"colpartdesc","vis":"vis","width":500,"ix":3,"desc":"Part Desc."},
...
]

On the PHP file, I read the $_POST value, and feed it back to the calling page (to the .save-response element in this example) as a response. 在PHP文件上,我读取了$_POST值,并将其作为响应返回到调用页面(在此示例中为.save-response元素)。 What I get as a reply is this: 我得到的答复是:

[
...
{"id":"colpartdesc","vis":"vis","width":270,"ix":3,"desc":"Part Desc."},
...
]

As you see, the "width" value is different. 如您所见, "width"值不同。 In fact, a value of 270 is the value that I sent the last time I pressed the ["Save"] button. 实际上,值270是我上次按下["Save"]按钮时发送的值。 If I press ["Save"] again, the server will receive the 500 value sent in this call. 如果再次按下["Save"] ,服务器将收到此呼叫中发送的500值。 But it may be that the value of 500 is wrong by that time. 但是到那时500的值可能是错误的。

If I press the ["Save"] button twice each time before changing the input, then the functionality works as expected. 如果在更改输入之前每次按两次["Save"]按钮,则该功能将按预期工作。

As you see, I've disabled the cache in the Ajax call, so I don't think this is the problem. 如您所见,我已经禁用了Ajax调用中的cache ,所以我认为这不是问题。

Does anyone have an idea why I am experiencing this behaviour? 有谁知道我为什么要经历这种行为?

Aside: the original HTML page is generated using Smarty templating engine. 此外:原始的HTML页面是使用Smarty模板引擎生成的。 However, I have switched off caching in the Smarty engine too, so again, I don't think this is significant. 但是,我也已经关闭了Smarty引擎中的缓存,因此,我再次认为这并不重要。 Also, the PHP file processing the Ajax call does not use a Smarty template. 另外,处理Ajax调用的PHP文件也不使用Smarty模板。

The answer to the problem is that I don't know how the jQuery Form plugin works. 问题的答案是我不知道jQuery Form插件的工作方式。

jQuery Form does not submit the values in the fields of the form, but rather the value in the array. jQuery Form不提交表单字段中的值,而是提交数组中的值。 Therefore, updating the values in the form itself withing the beforeSubmit function (as done in the following line) is incorrect: 因此,使用beforeSubmit函数更新表单本身中的值(如以下行所示)是不正确的:

$form.find('.user-settings').val(json_);

What I should be doing instead is to update the form-data array, like this: 我应该做的是更新表单数据数组,如下所示:

$('#myform').ajaxForm({
    $(this).ajaxForm({
        target: $(this).find('.save-response'),
        beforeSubmit: function(formData,$form){
            json_ = JSON.stringify (vars.table_settings[aux.getTableId()].by_ix); //, null, 4);
            formData[1].value = json_;
            $form.find('.save-response').stop(true,true).hide().show();
        },
        success: function(){
            $(this).fadeOut(15000);
        },
    });

The answer to >>this<< post is what put me on the right tracks. >> this <<帖子的答案使我处于正确的轨道。

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

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