Jump to content

ajax form data insertion via controller to db


sg_sg

Recommended Posts

 

I need help on ajax request from custom page.

I want to insert form data into sql table , but when I am trying to store sample variable with Tools::getValue('variable_1') the value stored as empty.

 

please help me out to how to pass form data from ajax to front controller and how to get values in php and pass to insert command.

//js.file
$("#form-submit").click(function() {

        var fd = new FormData();
        var file_data = $('input[type="file"]')[0].files; // for multiple files
        for(var i = 0;i<file_data.length;i++){
            fd.append("file_"+i, file_data[i]);
        }
        var other_data = $('#cgvideouploadform').serializeArray();
        $.each(other_data,function(key,input){
            fd.append(input.name,input.value);
        });
        //var formData = new FormData($("#cgvideouploadform")[0]); 
        alert(fd.get('cg_user-video-title'));
        //Custom data
       
        $.ajax({
        url : postURL,
        type : 'POST',
        cache : false,
        processData: false,
        contentType: false,
        data : {
            'variable_1' : 'var1',
        },
        success : function (result) {
            alert('AJAX call was successful!'+result);
                    //alert('Data from the server' + response);
                },
                error: function() {        
                    alert('There was some error performing the AJAX call!');
                }
        });
    });
    
    
    
    //in php file
    <?php

require_once('./classes/controller/Controller.php');

class customersinsertformModuleFrontController extends ModuleFrontController{
    public function initContent()
    {
        $this->ajax = true;
        parent::initContent();

    }
    public function displayAjaxUploadForm(){
       
       
       $db = Db::getInstance();

     /*WORKING
       $url='test';

        $query = "INSERT INTO `" . _DB_PREFIX_ . "cgv` (title,id_product,online,url) VALUES ('test','0','0','{$url}')";*/
        /*NOT WORKING*/
        //$var2 = filter_var(Tools::getValue('variable_1'),FILTER_SANITIZE_STRING);  
        //$url=$_POST['variable_1'];
        //$var1 = Tools::getValue('variable_1');

        /*End-Not working*/

        $var1 = Tools::getValue('variable_1');
        $query = "INSERT INTO `" . _DB_PREFIX_ . "cgv` (title,id_product,online,url) VALUES ('test','0','0','{$var1}')";
        $db->Execute($query);
        
        /*$db = Db::getInstance();
        $query = "INSERT INTO `" . _DB_PREFIX_ . "cgv` (title,id_product,usermail,online,url) VALUES ('title','0','usr','0','test')";
        $db->Execute($query);
        $this->notification();*/
        echo json_encode($query);
    }
}
 

 

Thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...