刷新后输入AJAX提交表单

问题描述:

我找遍了也没有找到如何通过JS / jQuery的/ AJAX。

I searched all over but couldn't find how to refresh specific input field via js/jquery/ajax.

这我投入了更改每一个岗位:

This my input that change on every post :

<input type='hidden' id='nonce' name='nonce' value=''>
<input type='hidden' id='key' name='key' value=''>

我需要后,Ajax表单提交刷新此输入,任何想法?

I need after ajax form submit to refresh this inputs, any idea?

有一个更好的解释: 这个PHP code生成随机哈希键。

A better explanation : This php code generates random hashed keys.

<form action="#">

<?php $n->generateFormFields() ?>

</form>

我把通过AJAX发布此生成的密钥,问题是,当我送了code到阿贾克斯,在服务器端的关键变化,因此后年提交的关键将是错误的,因为之后没有改变Ajax响应,所以我需要刷新此code后,阿贾克斯提交/刷新上面的投​​入。

I send this generated key via ajax POST, the problem is when I send the code to the ajax, the key changes in server side, so after next submit the key will be wrong because it didn't change after the ajax response, so I need to refresh this code after ajax submit/ refresh the inputs above.

修改2:

我使用这个PHP脚本:

I am using this php script :

http://github.com/greatwitenorth/php-nonce

脚本运行PHP的职位,但我使用AJAX的职位,所以我需要以某种方式刷新与阿贾克斯的关键。

The script is working on php POST, but I am using AJAX post so I need to refresh the keys with ajax somehow.

修改3:

形式例如:

<form action="#">

<?php $n->generateFormFields() ?>

</form>

PHP函数上面创建散列键。 这些哈希键我通过AJAX JSON POST发送后,我送他们,我验证密钥是相同的数据库密钥。 - 如果正常继续,如果没有显示错误。 现在的问题是每一个表单提交时间的重要变化。所以它的变化,但在输入的形式,它不会改变,因为Ajax不是刷新页面,所以这将是送那是以前相同的密钥值。

The php function above is creating Hashed keys. These hashed keys I send via ajax json POST, after I send them, I verify that the key is the same as the database key . - if ok continue, if not show error. now the problem is the key changes every time the form submitted. So it changes but in the input on the form, its not changed because ajax is not refreshing the page, so it will be sending the same key value that was before.

由于正在使用AJAX返回值,新的值保存到varibale说new_input,

Since you are using ajax to return the value, save the new value to a varibale say new_input,

然后简单地使用,

$("#target_input_id").val(new_input);

还是在Ajax调用你的.done()函数应该是这样,

Or your .done() function in ajax call should be like,

.done(function ( data ) {
  $("#target_input_id").val(data); //assuming the php file only returns the value for input
});

既然你已经返回多个值, 看这两个环节。

Since you have to return multiple values, look at this two links.

json - jQuery的返回undefined在AJAX调用多个值

How从JQuery的AJAX返回多个值调用?

现在.done()会,

Now .done() would be,

.done(function ( data ) {
      $("#key").val(data.key); 
      $("#nonce").val(data.nonce);
    });