0

I am having a problem with ajax not posting data as expected, i am using the Codeigniter calendar library and have added classes to each Table Row and Table Data tag so that when you click on any table cell the below code will fire. not sure what is going wrong

here is the code:

<script type="text/javascript">

$('.calendar .day').click(function(){
  get_day_num();
});

function get_day_num()
{
  //this alert shows 'undefined' ??
  alert($(this).find('.day').html());
  //values being passed to sendValue are probably 'undefined' - why?        
  sendValue($(this).find('.day_num').html(),$(this).find('.content').html())
}

function sendValue(day_num,day_data)
{
  $.ajax({
  url: window.location,
  type: 'POST',
  data: {
    day: day_num,
    data: day_data
  },
  complete: function(msg)
  {
    location:reload();
  }
});
}
</script>

2 Answers 2

1

Assuming the script tag is in the head of your page, you need to wrap your code in a document ready handler.

<script type="text/javascript">
    $(function() {
        // your code here...
    });
</script>

Also, this in your get_day_num function will be the window, not the element which was clicked on. You need to pass the element in to the function, or alternatively use the function reference as the handler. Try this:

$('.calendar .day').click(get_day_num);
Sign up to request clarification or add additional context in comments.

5 Comments

shoot, i missed that!
my jQuery code is in the body of the page. could that be why its not working?
no, its actually just before the footer tags - the </body> is just after the footer tags (ive gotten this from a nettuts tutorial on codeigniter and all has worked except for this bit of jQuery - which ive been struggling with all day)
found an error - location:reload() should have been location.reload() still didnt fix anything though - my real problem is that its getting 'undefined' values
@user2295029 see my update, I noticed you were using this in the wrong scope in one of your functions.
0
function sendValue(day_num,day_data)
{
  $.ajax({
  url: window.location,
  type: 'POST',
  data: {
    day: day_num,
    data: day_data
  },
  complete: function(msg)// here use success not complete in complete it comes every time whether out put come or not
  {
    location:reload();
  }
});
}

reference ajax

1 Comment

in console see in response what is come and see is this any error in your url file and in url why you use window.location i think this is causing problem please see in your console i think it will help

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.