2

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=''>

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

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

<form action="#">

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

</form>

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.

edit 2 :

I am using this php script :

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

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

edit 3:

The form ex:

<form action="#">

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

</form>

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.

8
  • after success of ajax just write following code. jQuery('#nonce').val(''); jQuery('#key').val(''); Commented Aug 4, 2013 at 6:07
  • not good it delete the value not refreshing it like normal POST when refresh the page , i need to refresh only the inputs Commented Aug 4, 2013 at 6:08
  • it make blank value for input Commented Aug 4, 2013 at 6:11
  • yes i tryed not good , i need just refresh , this inputs hold KEYS that comes from DATABASE and i need just refresh the input after form submit.. any ideas? Commented Aug 4, 2013 at 6:13
  • You need to be more specific. What does it mean to "refresh inputs"? If you want to reset your form, something like Datta said is what you are looking for. Otherwise you need to change your question to clarify your point. Commented Aug 4, 2013 at 6:16

2 Answers 2

1

.html file

<!DOCTYPE html>

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
    $("#check").click(function(){
        $("#keyvalue").text($("#key").val());
    });
    $("#submit").click(function(){
    var text = $("#text").val();
    var key = $("#key").val();
        $.ajax({
            url: 'trial.php',
            data: {text: text, key:key},
            type: 'POST',
            dataType: 'json',
            success: function(data) {
                if(data.status == "fail"){
                    $("#status").html(data.message);
                }else{
                    $("#status").html(data.message);
                    $("#key").val(data.key);
                    $("#keyvalue").text('');
                }
            }
        });
        return false;
    });
 });
</script>
</head>
<body>
    <form method="post" action="trial.php" onsubmit="return send_form();">
        <input type="text" name="text" id="text"/>
        <input type="hidden" id="key" name="key" value="xsaigoehf7118191"/>
        <button id="submit">Send data and get new key</button>
    </form>
    <br><br>
    <div id="status"></div>
    <br><br>
    <button id="check">What's current value of key?</button> --------> <span id="keyvalue"></span>

    <div id="response"></div>
</body>

</html>

.php

<?php

//You get the form contents here.

$key = isset($_POST['key']) ? $_POST['key'] : "error";
$text = isset($_POST['text']) ? $_POST['text'] : "empty";

//Check them if it matches with DB's entry, if doesn't you set $key = "error";

if($key=="error"){
    $status = "fail";
    $message = "There was some error processing your form.";
    exit;
} else{

    //You generate another random key.
    $random ='';
    for ($i = 0; $i < 10; $i++) {
        $random .= chr(mt_rand(33, 126));
    }

    //Now here in steps save it to your DB. So that when next form is submitted you can match it.
    //And send back response to html file, where ajax will refresh the key.
    $status = "success";
    $message = "
    Your form was processed succesfully<br>
    The text you sent was ".$text.", and the key you sent was ".$key.".
    The new key sent to you can be seen by pressing the button below, has value, ".$random."<br><br>
    ";
    }

    echo json_encode(array("status" => $status, "message" => $message, "key" => $random));

?>

Hope this helps you.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks you very much for you help , so its allmost what i have but in your php when u check the key i have php function that do it and just return msg text and not generate new key i will show u what i mean in the next answer , again thanks you so much.
Okay. Welcome. But a suggestion please work on your english. ;)
Editing the question for new question is a wrong idea. You should have started a new one. Please add a new question, and give me the link here. Make this question same as before. I would surely help on the newer one.
stackoverflow.com/questions/18053078/… here i make new one please help me i stuck very hard i am newbie ajax
Yes I will see that link after my class.
0

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

then simply use,

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

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 return undefined for multiple values in ajax call

How to return multiple values from JQuery AJAX call?

Now .done() would be,

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

27 Comments

its not good , look i will explain more better : i have form login , in my form login i have php function that generate random keys and i send them via ajax post and in the server side file i verfiy if it correct code. now the code change every post on server side , so i need to refresh it also after ajax response so i need to refresh the specific inputs - just refresh not take new values and not clear just make it refreshed..
Still not clear what you are looking for, this is how you refresh/change input values. Look here. jsfiddle.net/TSc8E/48
thanks you for your help , it close to what i need but still not what i need , look i have login form . in my login form i have hidden input that generate random hash keys with php function .i send the key with ajax POST to sever side php file and verfiy if the key is correct . so the problem is every time i press submit the key changes on the server side on not on the input , so its send old key instead of the new one , so i need to trigger the ajax to refresh this input to the the serverside key , only when i make refresh to the page it show the new key but ajax dont refresh
Are you refreshing key on server side or client side first?
the keys generated on php server side function inside the form input and auto refreshred by server side after been used , i mean if this key : 123 , verfiyed and already been used , it will be changed to 132 on serverside - Database . - but it wont change on the input since ajax post not refreshring the page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.