0
$('.select_category').change(function(){
        if($(this).is(':checked')){
            var ID = $(this).val();
            $.ajax({
                url:'<?php echo site_url($this->config->item('admin_folder').'/catalog/get_all_option');?>',
                type:'POST',
                data:{category_id:1},
                dataType: 'json',
                success:function(data){

                    $('#attribute_form').html('<?php add_attribute_form("'+data+'");?>');


                }

            });
        }

    });

on callback function success return the data and pass it to add_attribute_form(data) php function but nothing response. what is the correct way to pass js object to php function

3
  • Hum, you can't call a PHP function like that. After the page is already rendered in the browser, you just can't do that. You have to do what your function does using JavaScript/jQuery Commented Aug 29, 2015 at 12:04
  • but it work fine when i pass the custom data like add_attribute_form('yasir') and it show on #attribute_form div But i actually pass the object to the function. Commented Aug 29, 2015 at 12:10
  • 4
    What you need to know is that the instructions inside the PHP tags (<?php ... ?>) are carried out before the page is transferred to the user agent, so you'll never see a PHP tag in the delivered page! and the add_attribute_form() function will also be carried out before hand, no matter you pass the yasir or the '+data+' as the argument for the PHP function!! Commented Aug 29, 2015 at 12:40

1 Answer 1

2

What you will need to do here is, use Ajax to send data to a separate php page passing it some information, then, based on that information, the php page should return data to the Ajax callback function which will add the returned data to the original page.

Here's a simple example (and a working demo here):

In index.html do this:

<script>
 $(document).ready(function(){
    
    $('.select_category').change(function(){
        if($(this).is(':checked')){
            var ID = $(this).val();
            $.ajax({
                url:'somepage.php',
                type:'POST',
                data:{category_id:1},
                dataType: 'json', // this setting means you expect the server to return json formatted data
                                  // this is important because if the data you get back is not valid json, 
                                  // the success callback below will not be called, 
                                  // the error callback will be called instead
                success:function(response){
                    $('#attribute_form').html(response.html); 
                    // if not using json, this would just be $('#attribute_form').html(response); 
                },
                error:function(xhr, status, error){
                   // handel error 
                }
            });
        }
    });
});
</script>

<input type="checkbox" class="select_category"> Check this box
<div id="attribute_form"></div>

Then in somepage.php do the following:

<?php
$category_id = isset($_POST['category_id']) ? $_POST['category_id'] : null;
if($category_id == '1'){
    echo json_encode(array('html'=>'<h1>This text came from the php page</h1>'));
    exit;
    
    // if you are not using dataType:json in your ajax, you can just do:
    // echo '<h1>This text came from the php page</h1>';
    // exit;
}
?>
Sign up to request clarification or add additional context in comments.

Comments

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.