D
D
DeniSidorenko2020-05-18 14:09:13
opencart
DeniSidorenko, 2020-05-18 14:09:13

How to add a file upload for the form in contacts, Opencart?

Good afternoon, there is a form in contact, how to add a field with uploading files (images)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lazuren, 2020-05-18
@DeniSidorenko

I don’t know what version of OpenCart you have, but in the third version you can do this.
in a file

catalog/view/theme/default/template/information/contact.twig

add this code in the right place
<div class="form-group{% if option.required %} required {% endif %}">
            <label class="control-label">{{ option.name }}</label>
            <button type="button" id="button-upload{{ option.product_option_id }}" data-loading-text="{{ text_loading }}" class="btn btn-default btn-block"><i class="fa fa-upload"></i> {{ button_upload }}</button>
            <input type="hidden" name="option[{{ option.product_option_id }}]" value="" id="input-option{{ option.product_option_id }}" />
          </div>

js at the end of the file in a tag<script></script>
$('button[id^=\'button-upload\']').on('click', function() {
    var node = this;

    $('#form-upload').remove();

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

    $('#form-upload input[name=\'file\']').trigger('click');

    if (typeof timer != 'undefined') {
      clearInterval(timer);
    }

    timer = setInterval(function() {
      if ($('#form-upload input[name=\'file\']').val() != '') {
        clearInterval(timer);

        $.ajax({
          url: 'index.php?route=tool/upload',
          type: 'post',
          dataType: 'json',
          data: new FormData($('#form-upload')[0]),
          cache: false,
          contentType: false,
          processData: false,
          beforeSend: function() {
            $(node).button('loading');
          },
          complete: function() {
            $(node).button('reset');
          },
          success: function(json) {
            $('.text-danger').remove();

            if (json['error']) {
              $(node).parent().find('input').after('<div class="text-danger">' + json['error'] + '</div>');
            }

            if (json['success']) {
              alert(json['success']);

              $(node).parent().find('input').val(json['code']);
            }
          },
          error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
          }
        });
      }
    }, 500);
  });

All uploaded files can be found in System->Maintenance->External uploads
Important: When using this method, uploaded files will be anonymized and the file will not be attached to the message from the form, but will be uploaded immediately.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question