Jump to content

need help in $.ajax


FURQAN

Recommended Posts

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

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.