Samuel Meddows
Samuel Meddows

Reputation: 36712

Jquery Form Output

I have a function which sends specified form data to a php processing page.

function get(){

                $.post('data.php',{"name":$('#Fname').val(),"age":$('#age').val()},

                    function(output){
                $('#Rage').hide().html(output).fadeIn(1000);
                });
            }

After posting the script takes all the outputs from the php page (ie: echo's) and puts them all the in #Rage div. $('#Rage').hide().html(output).fadeIn(1000);

I am wondering is it possible to have two different outputs for each of the inputs.

By this i mean an echo response for "name":$('#Fname').val() goes to #Rname and an echo response for "age":$('#age').val() goes to #Rage.

I hope i have explained myself well enough.

Cheers guys.

Upvotes: 0

Views: 220

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163238

You could have your PHP script return some JSON with those keys, and in the callback function, assign the values to their respective elements.

Say you had the PHP script return this:

header('Content-type: application/json');

$return_obj = array('age' => 'someAge', 'name' => 'someName', 'success' => true);

echo json_encode($return_obj); //{"age": "someAge", "name": "someName", "success":true}

You could do this on the client side:

$.ajax({
        url:'data.php',
        type:'POST', 
        data: {"name":$('#Fname').val(),"age":$('#age').val()},
        dataType: 'json',
        success:function(json) {
           if(json.success) {
              $('#age').val(json.age || '');
              $('#Fname').val(json.name || '');
           }
        }
});

Upvotes: 2

Related Questions