0

i still can't get the jquery variable.it keeps on giving me an error

$(document).ready(function()
{
     $("#months").change(function(event)
    {
          $.ajax
          ({
            type: 'post',
             url: "monthly_CRD.php",
             data:
            {
                 "m": $(this).val()
             },
             success: function(msg)
             {
                 alert(msg);
             },
             error: function(msg) 
            {
                 alert("An error happened: " +msg);
            }   
       });
    });

    <div>
<select id="months">
    <option value='00'>Month...</option>
            <option value='01'>Jan</option>
            <option value='02'>Feb</option>
            <option value='03'>Mar</option>
            <option value='04'>Apr</option>
   </select>
   </div>

   <?php
     if (isset($_POST['m'])) 
      {
         $m = $_POST['m'];
         echo $m;
      }
   ?>

the error is An error happened: [objectXMLHttpRequest]

this is the jQuery

  $(document).ready(function()
{
    $("select").change(function(event)
    {
        var view=$(this).val();
        switch(view)
        {
            case "daily":
            {
                $("#graph").load('../crd_reports/daily_CRD.php');
                $("#top10").empty();
                break;
            }
            case "weekly": 
            {
                $("#graph").load('../crd_reports/weekly_CRD.php');
                $("#top10").load('../crd_reports/top10_weekly.php');
                break;
            }
            case "monthly":
            {
                $("#graph").load('../crd_reports/monthly_CRD.php');
                $("#top10").load('../crd_reports/top10_monthly.php');
                break;
            }
            case "yearly":
            {
                $("#graph").load('../crd_reports/yearly_CRD.php');
                $("#top10").load('../crd_reports/top10_yearly.php');
                break;
            }
            default: 
            {
            }
        }

    }); 
});
11
  • can you alert and see $(this).val() Commented Aug 9, 2011 at 5:59
  • What error is it giving? Commented Aug 9, 2011 at 5:59
  • and to see the error. instead of doing an alert write console.log and check your console in firfox or chrome. console.log(msg) Commented Aug 9, 2011 at 6:01
  • 1
    @shobe089 I suggest you read the API for the ajax() "error" callback - api.jquery.com/jQuery.ajax Commented Aug 9, 2011 at 6:04
  • 1
    @shobe089 Here's a hint; argument #1 to the error callback is not an error message. Also, there are three arguments for you to use, not just one Commented Aug 9, 2011 at 6:13

3 Answers 3

2

You saved the value with

var m = $(this).val();

...so you should put this into your data:

data: {
  "m": m
}
Sign up to request clarification or add additional context in comments.

2 Comments

@shobe089 - what happens if you put it back?
if i removed the variable and use this the alert function for error would give me An error happened: [objectXMLHttpRequest]
0

"this" change context inside the ajax function to represent the "ajax()" function instead of the selected month, so it won't have val() any more:

data:
{
    "m": $(this).val() // this is like -> "m": $.(ajax()).val()
},

retrive the "m" variable saved in the outer context instead:

data:
{
    "m": m
},

It should do the trick!

Comments

0

The Code you've given is in a .html or '.php' file? what ever, please check the url of this page. if you url is like something like file:///E:/wamp/www/test/post.html (example) then it will not work, you have to use http://localhost/test/post.html i.e a server url. I think this will fix your problem

4 Comments

well it's like this.the file is a php file and is called through jquery.
@shobe089 i want to know that the code of jquery reside in .html or .php file. if you change the url http://localhost/test/post.html or http://localhost/test/post.php it will work, i hope
there i already posted the jquery.i edited the post above.thank you for the help.
i'm calling a php file named reports_CRD.php then from there the viewer could change which view he or she wants without loading the whole page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.