FURQAN Posted January 19, 2011 Share Posted January 19, 2011 hi there i am having problem with $.ajax i have a php page that is echoing up a variable the code is as follows the named variable.php <?php $furqan="Here i am"; echo $furqan; ?> i have another page from where i am to retreive the variable that named value.php the code is as follows <html> <head> <script src="jquery.js" type="text/javascript"></script> <script type="text/javascript"> function displaymessage() { $.ajax ({ type: "POST", url: "variable.php", dataType: "json", data: data, cache: false, success: function(furqan) { //alert(data); $('#here').html(furqan); } }); } </script> </head> <body> <form> <input type="button" value="Click me!" onclick="displaymessage()" /> </form> <div id="here"></div> </body> </html> Thats not working for me i want to get a variable from php page and want to show it in the <div> named here after doing inspect element from google chrome in the scripts its saying Uncaught ReferenceError: data is not defined can anybody help me out with this problem.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/224939-need-help-in-ajax/ Share on other sites More sharing options...
trq Posted January 19, 2011 Share Posted January 19, 2011 A few things. Firstly, you passing the data parameter a variable called data, this is undefined and you don't need it. Secondly, your telling the $.ajax() function that it is to receive the dataType of json from this request, then you return it plain text. You should stick with the json dataType, but fix your php accordingly. (this will make things more flexible in the long run). Lastly, jQuery provides mechanisms that mean you don't need to mix Javscript in with your markup, use them. So, with those things in mind, your code should not look like. <?php echo json_encode(array('foo' => 'Here i am')); ?> <html> <head></head> <body> <form> <input type="button" id="clicker" value="Click me!" /> </form> <div id="here"></div> </body> <script src="jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $('#clicker).click(function() { $.ajax({ type: "POST", url: "variable.php", dataType: "json", cache: false, success: function(json) { $('#here').html(json.foo); } }); }); }); </script> </html> Quote Link to comment https://forums.phpfreaks.com/topic/224939-need-help-in-ajax/#findComment-1161819 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.