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
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?
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
////////////////////////////////////////////
<?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('price'); ?>
" />
" />
Please Select
<?php
foreach($cats as $id=>$cat){
?>
id;?>"><?php echo $cat->name;?>
<?php
}
?>
Please Select
Yes
No
</*input type="button" id="save-product" name="save-product" title="Save Product" value="Save Product" />*/
<?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
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
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.