Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery POST Json return

Tags:

json

jquery

post

i have a jquery script, which posts my form. here it is:

$(document).ready(function(){
$("form#submit").submit(function() {


var an      = $('#an').attr('value');
var betreff = $('#betreff').attr('value');
var text    = $('#text').attr('value');

    $.ajax({
        type: "POST",
        url: "newmsg.php",
        data: "an="+ an +"& betreff="+ betreff +"&text="+ text,
        success: function(){

            $('#window').html(name);                

        }
    });
return false;
});
});

my newmsg.php file

<?php if($_POST['an']=="john") { echo json_encode(array("name"=>"hi john")); } ?>

my problem is, that my php file will not return the name, so my div #window does not post the message

hope you guys understand...

thank you very much

like image 540
njaknjak Avatar asked Mar 19 '11 02:03

njaknjak


2 Answers

Try

success: function(data){
    var json = $.parseJSON(data);
    $('#window').html(json.name);                
}
like image 180
mattsven Avatar answered Nov 08 '22 18:11

mattsven


this is prob the part you have wrong

$('#an').attr('value');

if id=an is a input it should be done this way

$('#an').val(); 

if id=an is a container it should be done this way

$('#an').html(); 

you'll want to change the rest of these too

var betreff = $('#betreff').attr('value');
var text    = $('#text').attr('value');
like image 26
Traveling_Monk Avatar answered Nov 08 '22 16:11

Traveling_Monk