Need help with Ajax call + ajax call with post method in admin module

edited July 2015 in Modules
Hi,
I am trying ajax call with post method in fuel advanced module (Admin panel) something like

var request = $.ajax({
url: "<?php echo base_url().PRODUCT_MODULE_FOLDER.'/ajax/';?>saveProduct",
method: "POST",
//data: $("#frm_product").serialize(),
data: {s:'ss'},
dataType: "json"
});

request.done(function( msg ) {
alert(msg );
});

request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: ");
});

My routes

$my_models = array('products');
$my_controllers = array('product','scrap','ajax');
$route[FUEL_ROUTE.PRODUCT_MODULE_FOLDER.'/(:any)'] = PRODUCT_MODULE_FOLDER.'/$1';
foreach($my_models as $c)
{
$route[FUEL_ROUTE.PRODUCT_MODULE_FOLDER.'/'.$c] = FUEL_FOLDER.'/module';
$route[FUEL_ROUTE.PRODUCT_MODULE_FOLDER.'/'.$c.'/(.*)'] = FUEL_FOLDER.'/module/$1';
}
foreach($my_controllers as $c)
{
$route[FUEL_ROUTE.PRODUCT_MODULE_FOLDER.'/'.$c] = PRODUCT_MODULE_FOLDER.'/'.$c;
$route[FUEL_ROUTE.PRODUCT_MODULE_FOLDER.'/'.$c.'/(.*)'] = PRODUCT_MODULE_FOLDER.'/'.$c.'/$1';
}

1. Here ajax call is not working on module's any controller .
2. But when I create a controller ajax which extends fuel_base_controller it works fine.
3. Still when I try this with point #2 it does not accept POST
4. Also when I use GET method and because the data is too large it throws an error - "The requested URL's length exceeds the capacity limit for this server."

I am stuck here, in my several project in fuel cms, very badly and I need solution for this, please help me with this issue. Thanks in advance.

Comments

  • edited 3:14PM
    I'm sorry but I'm not quite sure I understand your issue and need some additional information. A few questions:

    1. Is this concerning the front end or back end?
    2. What is the URL you are trying to AJAX too (e.g. /products/ajax/saveProduct)
    3. Where is this AJAX code being setup and is it loading correctly in the page?
    4. Do you have the proper routes in place in the advanced module's fuel/modules/products/config/products_routes.php file?
  • edited 3:14PM
    Hi,

    Here my answers

    1. Is this concerning the front end or back end?
    Back end

    2. What is the URL you are trying to AJAX too (e.g. /products/ajax/saveProduct)
    Under product module there is controller called ajax and saveProduct is method. /products/ajax/saveProduct

    3. Where is this AJAX code being setup and is it loading correctly in the page?
    It is being loaded in my custom view file.

    4. Do you have the proper routes in place in the advanced module's fuel/modules/products/config/products_routes.php file?
    Yes I have mentioned routes as well...

    $my_models = array('products');
    $my_controllers = array('product','scrap','ajax');
    $route[FUEL_ROUTE.PRODUCT_MODULE_FOLDER.'/(:any)'] = PRODUCT_MODULE_FOLDER.'/$1';
    foreach($my_models as $c)
    {
    $route[FUEL_ROUTE.PRODUCT_MODULE_FOLDER.'/'.$c] = FUEL_FOLDER.'/module';
    $route[FUEL_ROUTE.PRODUCT_MODULE_FOLDER.'/'.$c.'/(.*)'] = FUEL_FOLDER.'/module/$1';
    }
    foreach($my_controllers as $c)
    {
    $route[FUEL_ROUTE.PRODUCT_MODULE_FOLDER.'/'.$c] = PRODUCT_MODULE_FOLDER.'/'.$c;
    $route[FUEL_ROUTE.PRODUCT_MODULE_FOLDER.'/'.$c.'/(.*)'] = PRODUCT_MODULE_FOLDER.'/'.$c.'/$1';
    }

    Thanks
  • edited 3:14PM
    For point #3, you say that that it does not accept any POST data. What is meant by that (e.g. is it not going to the fuel/modules/products/controllers/ajax.php saveProduct method?) Are you seen an error and if so, what is the error?
  • edited 3:14PM
    Here is my code which is a view file in module

    ////////////////////////////////////////////


    <?php //if(isset($_POST['search_url'])){

    if(isset($_POST['search_url'])){
    $murl = $_POST['search_url'];
    }elseif(isset($_POST['product_url'])){
    $murl = $_POST['product_url'];
    }else{
    $murl = '';
    }

    ?>



     



    " placeholder="URL...">


     






     

    <?php echo form_error('product_name'); ?>
    Price
    <?php echo form_error('price'); ?>
    " />
    " />
    Category

    Please Select
    <?php
    foreach($cats as $id=>$cat){
    ?>
    id;?>"><?php echo $cat->name;?>
    <?php
    }
    ?>

    <?php echo form_error('category_id'); ?>

    Description
    Is Discount
    Please Select
    Yes
    No

    </*input type="button" id="save-product" name="save-product" title="Save Product" value="Save Product" />*/


     

    image" id="main-image-hopp" />

    <?php //} ?>

     



    <?= (isset($result)) ? $result : '';?>






    .error-field{
    color:#FF0000
    }

    .prod-selector{
    height:30px;
    border:30px dashed #FF0000;
    z-index:100px;
    }

    .prod-selected{
    background-color:#99FF99;
    }

    .prod-deselected{
    background-color:none;
    }


    var store_host = '<?php echo trim($store_host);?>';

    function validateProduct(){
    $("#fuel_notification").find('.ico').html('');
    var request = $.ajax({
    url: "<?php echo base_url()."fuel/".PRODUCT_MODULE_FOLDER.'/';?>validate",
    method: "POST",
    //data: $("#frm_product").serialize(),
    data: {name:$("#product_name").val()},
    dataType: "json"
    });

    request.done(function( msg ) {

    if(msg.status=='fail'){
    $("#save-product").prop('disabled',true);
    $("#fuel_notification").find('.ico').html('Product already exist.');
    }else{
    $("#save-product").prop('disabled',false);

    }

    });

    request.fail(function( jqXHR, textStatus ) {
    $("#save-product").hide();
    alert( "Request failed: ");
    });
    }

    });


    ///////////////////////////////////////////////
    YOU can see here jquery ajax is being used and it is making a post request but in controller's method it does not come as $_POST but $_GET. I think now it make sense.

    Thanks
  • edited 3:14PM
    How is the validateProduct function being called so that it takes over the submission of the form?
  • edited 3:14PM
    1. I have created a advanced module and there one controller, which has a validate method. This validate whether product already there or not.
    2. Now for this I created one view file which has a form and javascript ajax call is embeded there.
    3. I am not submitting form through submit button, in form there is button. I have defined a click event using jquery to post form's field's data to my controller's function.
    4. The problem is I can not make a ajax call from back-end files with "POST" method.

    Here is the code :

    var request = $.ajax({
    url: "<?php echo base_url()."fuel/".PRODUCT_MODULE_FOLDER.'/';?>validate",
    method: "POST",
    //data: $("#frm_product").serialize(),
    data: {name:$("#product_name").val()},
    dataType: "json"
    });

    request.done(function( msg ) {

    if(msg.status=='fail'){
    $("#save-product").prop('disabled',true);
    $("#fuel_notification").find('.ico').html('Product already exist.');
    }else{
    $("#save-product").prop('disabled',false);

    }

    });

    request.fail(function( jqXHR, textStatus ) {
    $("#save-product").hide();
    alert( "Request failed: ");
    });
    }
    When I do print_r($_POST) in controller's function it does not print anything but print_r($_GET), it prints.

    Simply putting my question now - how can I make a ajax call with POST method?


    Thanks
  • edited 3:14PM
    I see the javascript function in the code, but I don't see what in the code triggers that function call so that it does take over the submission of the form.

    Are you able to browse to the AJAX url normally? FUEL shouldn't be preventing you from submitting a form post via AJAX... it's just a normal CI controller handling it so I would make sure that that works. I'd perhaps test the form part outside of FUEL just to create a simple HTML file that sits at the root of your FUEL installation alongside your index.php file and put in the AJAX and HTML for submitting the form to your URL and see if that even works.
Sign In or Register to comment.