1

I have created a custom form on the frontend, and want to save data in the database as well display in the admin.

My Form:

<form id="career-form" name="career-form" action="#" method="post">
    <div class="rendered-form">
        <div class="form-group field-name">
            <label for="name" class="fb-text-label">Name<span class="fb-required">*</span></label>
            <input type="text" placeholder="enter your name" class="form-control" name="name" id="name" required="required" aria-required="true">
        </div>
        <div class="form-group field-email">
            <label for="mail" class="fb-text-label">E-mail<span class="fb-required">*</span></label>
            <input type="email" placeholder="enter your Email"id="mail" name="email" class="form-control" required="required" aria-required="true"/>
        </div>
        <div class="form-group field-mobile">
            <label for="mobile" class="fb-text-label">Mobile Number<span class="fb-required">*</span></label>
            <input type="text" placeholder="Enter your Mobile Number" class="form-control" name="mobile" maxlength="10" id="mobile" required="required" aria-required="true">
        </div>
        <div class="form-group field-applied">
            <label for="applied" class="fb-text-label">Applied For<span class="fb-required">*</span></label>
            <input type="text" class="form-control" name="applied" id="applied" required="required" aria-required="true">
        </div>
        <div class="form-group field-intro">
            <label for="intro" class="fb-textarea-label">Short Introduction</label>
            <textarea type="textarea" class="form-control" name="intro" rows="5" id="intro"></textarea>
        </div>
        <div class="form-group field-resume">
            <label for="resume" class="fb-file-label">Upload Resume<span class="fb-required">*</span></label>
            <input type="file" class="form-control" name="resume" id="resume" required="required" aria-required="true">
        </div>
        <div class="form-group field-apply">
            <?php /* ?><button type="button" class="btn btn-success" name="submit" id="apply-success">Button</button><?php */ ?>
            <input type="button" class="btn btn-success" value="submit comment" id="apply-success"/>
        </div>
        <span id="formLoader">&nbsp;</span>
        <span id="successmsg">&nbsp;</span>
    </div>
</form>

Controller Code:

<?php 
namespace Allin\Video\Controller\Index;
use Magento\Framework\Controller\ResultFactory; 
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;

class Career extends \Magento\Framework\App\Action\Action
{

 public function execute()
    {
    $i=0;
    $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    $post = $this->getRequest()->getPost();
    if (!$post) {
            $this->_redirect('*/*/');
            return;
        }

         if(1)
        {

            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $fileSystem = $objectManager->create('\Magento\Framework\Filesystem');
            $mediaPath=$fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath();
            $media = $mediaPath . 'test-doc/';
            $file_name = rand() . $_FILES['resume']['name'];
            $file_size = $_FILES['resume']['size'];
            $file_tmp = $_FILES['resume']['tmp_name'];
            $file_type = $_FILES['resume']['type'];
            $file_path = $media . $file_name;

//echo $file_name; die;
        if (move_uploaded_file($file_tmp, $media . $file_name)) {
            echo $file_name;
        } else {
            echo "File was not uploaded";
        }   



            //echo 'testtttttttttttttt';
            $to = 'allin.vinodkumar@gmail.com';
            $subject = 'Career Form';
            $name = $post['name'];
            //echo $name; die;
            $email = $post['email'];
            $phone = $post['mobile'];
            $applied = $post['applied'];
            $intro = $post['intro'];
            //$resume = $_FILES['resume']['name'];


            $message = 'Name: ' . $name . "\r\n" . "Email: " . $email . "\r\n" . "Phone: " . $phone . "\r\n". 'Post: ' . $applied. "\r\n" . 'Intro: ' . $intro;
            //echo $message; die;
            $headers = 'From:'. $email;
//echo $message; die;
            mail($to, $subject, $message, $headers); 


            $model = $objectManager->create('Allin\Resume\Model\Jobresume');
            $model->setData('name', $post['name']);
            $model->setData('email', $post['email']);
            $model->setData('phone', $post['mobile']);
            $model->setData('post', $post['applied']);
            $model->setData('intro', $post['intro']);
            $model->setData('format', $post['type']);
            $model->save();

             echo 'Thanks for applying the Job. We will contact you asap.';

        }

    }
}

Ajax Code to call Controller:

<script>
//========================Comment Form======================================//
require(['jquery'], function() {
    jQuery(document).ready(function() {     
    jQuery( "#apply-success" ).click(function() {
        if(1){
        var data = jQuery('#career-form').serialize();
        //alert(data);
        jQuery.ajax({
            url:"<?php echo $this->getUrl().'video/index/career';?>",
            type:'POST',
            data: data,
            beforeSend:function()
            {
                 jQuery("#formLoader").html("<img src='http://www.example.com/pub/media/zipcode/loading.gif'/>");
                 jQuery("#formLoader").show();
            },
                success: function(response)
            {   
                jQuery("#formLoader").hide();
                jQuery("#successmsg").html(response);
                jQuery("#successmsg").show().delay(5000).fadeOut();
                jQuery("#career-form")[0].reset();


            }
        });
        }else{ return false; }
        });

    });
});
</script>

I am unable to get image in the controller and save in the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.