开发者

Jquery Form Output

开发者 https://www.devze.com 2023-01-28 16:46 出处:网络
I have a function which sends specified form data to a php processing page. function get(){ $.post(\'data.php\',{\"name\":$开发者_如何学C(\'#Fname\').val(),\"age\":$(\'#age\').val()},

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

function get(){

                $.post('data.php',{"name":$开发者_如何学C('#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.


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 || '');
           }
        }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号