1

Checked several topics here on stack overflow and the jquery documentation but still getting stuck.

I basically want this to post (with ajax/jquery) to update a div's content.

function loadSubCats(value)
{
    $.post("load_sub_cats.php",
        data: { "catid": value },
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
            function(data) {
        $('#sub_categories').html("Data Loaded: " + data);
    });
}

But im getting no responses what so ever. The page contains the sub_categories div, and this is called onclick for a form event.

<select name='cselect3' onChange='loadSubCats(this.value)' class='e1'>

So basically, it should past the value of the field (cselect3) and load_sub_cats.php should just echo $_POST[catid]; thats it. But im getting zero activity on it so it think its something simple i've screwed up.

6
  • Did you try to inspect the request? Chrome Dev tools or Firebug? What do you get? Commented Jan 10, 2013 at 12:45
  • 1 error, Uncaught ReferenceError: loadSubCats is not defined Commented Jan 10, 2013 at 12:48
  • which means your function "loadSubCats" containing the $.post never gets called. Commented Jan 10, 2013 at 12:51
  • look at the script included/written above loadSubCats function. May be there is some error and so loadSubCats function is not found Commented Jan 10, 2013 at 12:53
  • @MichaelClarke have you tried my answer? I've corrected your code. Commented Jan 10, 2013 at 12:55

4 Answers 4

1

you have a problem with $.post syntax. Here's how it should be:

function loadSubCats(value)
{
    $.post("load_sub_cats.php",{ "catid": value },
            function(data) {
        $('#sub_categories').html("Data Loaded: " + data);
    });
}

as simple as it is.

Sign up to request clarification or add additional context in comments.

2 Comments

That loads the other page, but the how do i use the value it passed? $_GET, $POST both dont contain it
use $_POST["catid"] should get you the data passed by the above function.
1
function loadSubCats(value)
{
$.post("load_sub_cats.php",
    {  catid: value }, 
     function(data) {
     $('#sub_categories').html("Data Loaded: " + data); }
     );
}

You send catid not "catid"

Comments

1

You're mixing the syntax of $.post and $.ajax which are different. If you want to use the object syntax then call $.ajax, because $.post takes its arguments as a simple un-named list.

function loadSubCats(value)
{
   $.ajax({
        url : "load_sub_cats.php",
        data: { "catid": value },
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        success: function(data) {
            $('#sub_categories').html("Data Loaded: " + data);
        }
    });
}

You could also directly attach the change event instead, of inline and do away with the loadSubCats() function:

$(document).ready(function(){
    $('select[name=cselect3]').change(function(){
       $.ajax({
            url : "load_sub_cats.php",
            data: { "catid": $(this).val() },
            type: "POST",
            contentType: "application/x-www-form-urlencoded",
            success: function(data) {
                $('#sub_categories').html("Data Loaded: " + data);
            }
        });
    });
});

2 Comments

Tried this it didnt resolve the problem, still getting the ncaught ReferenceError: loadSubCats is not defined and no output on screen.
Where you are placing the loadSubCats() function? Put it in the global scope, not in a a $(document).ready or any other closure.
-1

two things:

you are using type in post which is not required.

Even after removing it is not working just try to alert it-

function loadSubCats(value)
{
    $.post("load_sub_cats.php",
        data: { "catid": value },
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
            function(data) {
          alert(data)
         $('#sub_categories').html("Data Loaded: " + data);
    });
}

By alert you will come to know - 1) if there is any php error it will alert that, 2) If php is working fine that it will alert with your values.

updating my post as per latest comments: Try to use like below:

$(document).ready(function() {
  $('.e1').change(function() {
        $.post("load_sub_cats.php",
        data: { "catid": value },
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
            function(data) {
          alert(data)
         $('#sub_categories').html("Data Loaded: " + data);
    });  
  })
});

2 Comments

Still receiving the same error and no output. Have checked the file receiving and there are no errors on it.
if you are using $(document).ready() than it will be not same error. And if error is same than how you are checking PHP file output?

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.