Jump to content

ajax form data insertion via controller to db


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.

$("#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();
        //var formData = new FormData($("#cgvideouploadform")[0]); 
        //Custom data
        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


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

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


        $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);  
        //$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 = Db::getInstance();
        $query = "INSERT INTO `" . _DB_PREFIX_ . "cgv` (title,id_product,usermail,online,url) VALUES ('title','0','usr','0','test')";
        echo json_encode($query);



Share this post

Link to post
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...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More