Help with "simple" Modal box

I have a posting in the PHP section titled “Help with Upload script = “Must agree to terms first…”, in which I explain: The script I’ve installed (successfully) allows the user to drag/drop, or choose a file, to upload. How can a feature be added where the user must agree to the Terms before the dragged/dropped/chosen file can be uploaded? This script uploads the file as soon as it is drag/dropped.

So, I looked into a Modal Box which will alledgedly creates a lightbox over the drag/drop area. So, I followed along with this page: https://paulund.co.uk/how-to-create-a-simple-modal-box-with-jquery,

but my copying of it isn’t working successfully yet, probably because I don’t understand a few things (I’m learning as I go here).

They state about the line of code below: “We want this run on the click event of a link, so we need to create a link and add a class so we can reference it later”.

<a href="#" class="paulund_modal">Click Here</a>

I’m not completely sure how that statement can relate to having someone agree to the Terms first before uploading. I understand the concept that the event is to click, or check a box, which means they agree to the Terms, which will also remove the modal box, making the drag/drop area accessible for uploads, but I need some more guidance, or an example please, of how that would work with this script. I think I’m pretty close, but I’m probably missing some specific function code stating “do somthing here”…

Another question is, do I replace # with the page where this modal box code is (the upload page)?

There is also that type of ‘link’ code on line 86 below. Any enlightenment will be greatly appreciated.

Here’s the js code:

(function($){

	// Defining our jQuery plugin

	$.fn.paulund_modal_box = function(prop){

		// Default parameters

		var options = $.extend({
			height : "250",
			width : "500",
			title:"JQuery Modal Box Demo",
			description: "Example of how to create a modal box.",
			top: "20%",
			left: "30%",
		},prop);

		return this.click(function(e){
			add_block_page();
			add_popup_box();
			add_styles();

			$('.paulund_modal_box').fadeIn();
		});

		 function add_styles(){
			$('.paulund_modal_box').css({
				'position':'absolute',
				'left':options.left,
				'top':options.top,
				'display':'none',
				'height': options.height + 'px',
				'width': options.width + 'px',
				'border':'1px solid #fff',
				'box-shadow': '0px 2px 7px #292929',
				'-moz-box-shadow': '0px 2px 7px #292929',
				'-webkit-box-shadow': '0px 2px 7px #292929',
				'border-radius':'10px',
				'-moz-border-radius':'10px',
				'-webkit-border-radius':'10px',
				'background': '#f2f2f2',
				'z-index':'50',
			});
			$('.paulund_modal_close').css({
				'position':'relative',
				'top':'-25px',
				'left':'20px',
				'float':'right',
				'display':'block',
				'height':'50px',
				'width':'50px',
				'background': 'url(images/close.png) no-repeat',
			});
                        /*Block page overlay*/
			var pageHeight = $(document).height();
			var pageWidth = $(window).width();

			$('.paulund_block_page').css({
				'position':'absolute',
				'top':'0',
				'left':'0',
				'background-color':'rgba(0,0,0,0.6)',
				'height':pageHeight,
				'width':pageWidth,
				'z-index':'10'
			});
			$('.paulund_inner_modal_box').css({
				'background-color':'#fff',
				'height':(options.height - 50) + 'px',
				'width':(options.width - 50) + 'px',
				'padding':'10px',
				'margin':'15px',
				'border-radius':'10px',
				'-moz-border-radius':'10px',
				'-webkit-border-radius':'10px'
			});
		}

		 function add_block_page(){
			var block_page = $('<div class="paulund_block_page"></div>');

			$(block_page).appendTo('body');
		}

		 function add_popup_box(){
			 var pop_up = $('<div class="paulund_modal_box"><a href="/Upload.html" class="paulund_modal_close"></a><div class="paulund_inner_modal_box"><h2>' + options.title + '</h2><p>' + options.description + '</p></div></div>');
			 $(pop_up).appendTo('.paulund_block_page');

			 $('.paulund_modal_close').click(function(){
				$(this).parent().fadeOut().remove();
				$('.paulund_block_page').fadeOut().remove();
			 });
		}

		return this;
	};

})(jQuery);

I’m not sure that script, is going to do what you think it is. All that it is going to do is pop up a modal box when the user clicks a link within the page. Is that what you wanted?

The ‘Click Here’ link is simply the trigger that opens the modal box, it doesn’t actually go to the href.

The second link you mention, in the add_popup_box function, is just another trigger, that one is to close the modal.

What library or script are you using to handle the drag/drop upload process?

For “Click here” you would put, “Read the Terms of Use before uploading.”

For title:“JQuery Modal Box Demo”, replace the text in quotes with “Terms of Use.”

For description: “Example of how to create a modal box.”, replace the text in quotes with your terms of use.

The reason I asked about the library is because most libraries like dropzone provide events that could delay the upload process. For example when a file is dropped the upload could be delayed popping open the modal. Than the upload could be resumed later.

Quick search on google revealed how to easily achieve deferring uploads with dropzone.js.

Thanks for the replies.
Regarding “pop up a modal box when the user clicks a link within the page. Is that what you wanted?”
No, actually I’d like it so that the modal box is blocking the drap/drop area upon page load, and removed when something is selected that means they have read the Terms. Any help with that will be appreciated.

Regarding “replace the text”, I did that, but upon clicking ‘Read the Terms of Use before uploading’ a small box appears for a nano-second, is all that is working for me currently.

Regarding “most libraries like dropzone provide events that could delay the upload process…”
Yes, it is dropzone that I am using, but when I looked at the Stackoverflow link you provided (thanks) and looked over the dropzone.js, I don’t think I could make the integration with my current skill level.
But, "when a file is dropped the upload could be delayed popping open the modal. Than the upload could be resumed later (like after something is clicked to mean they have read the Terms), would be ideal.

Any additional help will be appreciated.

The 3rd comment down explains how to implement the delay.

Gotta fix that! This is an area to figure out. Compare your code with the original. This is unacceptable behavior.

Thanks for the replies. Greatly appreciated.

Regarding “3rd comment down explains how to implement the delay”, I think you mean: /Block page overlay/, (or do you mean in the stackoverflow link, it only has two answers?) So, I added some height and width.

So, here is my current code, still a small box appears for a nano-second upon clicking 'Read the Terms of Use before uploading":

<head>
.....

<script src="/jquery.paulund_modal_box.js"></script>

.....

</head>

<body>

....

<a href="/Upload.html" class="paulund_modal">Read the Terms of Use before uploading</a>

.......

	<script>
	$(document).ready(function(){
		$('.paulund_modal').paulund_modal_box();
	});
</script>
</body>

and the jquery.paulund_modal_box.js file code:

(function($){

	// Defining our jQuery plugin

	$.fn.paulund_modal_box = function(prop){

		// Default parameters

		var options = $.extend({
			height : "250",
			width : "500",
			title:"Terms of Use",
			description: "The content of this Site, and related services are presented "as-is". All warranties of any kind, expressed or implied, including merchantability,
non-infringement of third party rights, and the warranty of fitness for a particular purpose, as to the operation of the Site, and related services,
or the nature or accuracy of the information included in the Site, and related services, are disclaimed.
").<br /><br />
			top: "20%",
			left: "30%",
		},prop);

		return this.click(function(e){
			add_block_page();
			add_popup_box();
			add_styles();

			$('.paulund_modal_box').fadeIn();
		});

		 function add_styles(){
			$('.paulund_modal_box').css({
				'position':'absolute',
				'left':options.left,
				'top':options.top,
				'display':'none',
				'height': options.height + 'px',
				'width': options.width + 'px',
				'border':'1px solid #fff',
				'box-shadow': '0px 2px 7px #292929',
				'-moz-box-shadow': '0px 2px 7px #292929',
				'-webkit-box-shadow': '0px 2px 7px #292929',
				'border-radius':'10px',
				'-moz-border-radius':'10px',
				'-webkit-border-radius':'10px',
				'background': '#f2f2f2',
				'z-index':'50',
			});
			$('.paulund_modal_close').css({
				'position':'relative',
				'top':'-25px',
				'left':'20px',
				'float':'right',
				'display':'block',
				'height':'250px',
				'width':'250px',
				'background': 'url(images/close.png) no-repeat',
			});
                        /*Block page overlay*/
			var pageHeight = $(document).height();
			var pageWidth = $(window).width();

			$('.paulund_block_page').css({
				'position':'absolute',
				'top':'0',
				'left':'0',
				'background-color':'rgba(0,0,0,0)',
				'height':250px,
				'width':250px,
				'z-index':'10'
			});
			$('.paulund_inner_modal_box').css({
				'background-color':'#fff',
				'height':(options.height - 50) + 'px',
				'width':(options.width - 50) + 'px',
				'padding':'10px',
				'margin':'15px',
				'border-radius':'10px',
				'-moz-border-radius':'10px',
				'-webkit-border-radius':'10px'
			});
		}

		 function add_block_page(){
			var block_page = $('<div class="paulund_block_page"></div>');

			$(block_page).appendTo('body');
		}

		 function add_popup_box(){
			 var pop_up = $('<div class="paulund_modal_box"><a href="Upload.html" class="paulund_modal_close"></a><div class="paulund_inner_modal_box"><h2>' + options.title + '</h2><p>' + options.description + '</p></div></div>');
			 $(pop_up).appendTo('.paulund_block_page');

			 $('.paulund_modal_close').click(function(){
				$(this).parent().fadeOut().remove();
				$('.paulund_block_page').fadeOut().remove();
			 });
		}

		return this;
	};

})(jQuery);

Any additional help will be appreciated.

Pretty sure he means #4 (posts are numbered at right).

Thank you.
As you can see I by my code in the #9 posting I followed those instructions, I believe.

… and you still need to focus on this. This is wrong behavior.

Thanks for your message.
Yes, believe me I am focused on it, however, I still don’t know the cause.
Any ideas/suggestions/remedy will be welcomed.

There are much simpler modal plugins to use. I like http://jqueryui.com/dialog/.

Have a look at this fiddle https://jsfiddle.net/cfpyb8d6/6/. Is this what you’re trying to achieve?

Yes, thanks. That is what I’m trying to acheive.
However, I tried to copy both of your examples over with no success.
Any other help will be appreciated.

What is not working for you?

If you are copying https://jsfiddle.net/cfpyb8d6/6/ make sure you look at what’s included in the external resources - right hand column. You need jquery & jquery-ui js and jquery-ui css,

Thanks for the replies. Much appreciated.

I tried what was suggested, without success, so I moved onto jBox (https://stephanwagner.me/jBox), which I have now set up successfully.
When I select 'Click Me" the modal box appears and asks a question, essentially yes or no.

So, if I could get some guidance with how to tweak it so that instead of “click me” (to show the modal box), the modal box will appear upon page load, automatically. So, my first guess is that a change may be needed in jBox.confirm.js in order to get the modal box to show upon page load.
I look forward to any feedback/guidance, much thanks again. Here’s the code:

Html:

<main class="container">
<div class="target-click" data-confirm onclick="new jBox('Notice', {content: 'Yay! You clicked the confirm button', color: 'green', attributes: {y: 'bottom'}})">Click me!</div>
</div>
</main>

js:


jQuery(document).ready(function () {
  
  new jBox.plugin('Confirm', {
    
    
    // Options (https://stephanwagner.me/jBox/options#options-confirm)
    
    confirmButton: 'Submit',  // Text for the submit button
    cancelButton: 'Cancel',   // Text for the cancel button
    confirm: null,            // Function to execute when clicking the submit button. By default jBox will use the onclick or href attribute in that order if found
    cancel: null,             // Function to execute when clicking the cancel button
    closeOnConfirm: true,     // Close jBox when the user clicks the confirm button
    target: window,
    addClass: 'jBox-Modal',
    fixed: true,
    attach: '[data-confirm]',
    getContent: 'data-confirm',
    content: 'Do you really want to do this?',
    minWidth: 360,
    maxWidth: 500,
    blockScroll: true,
    closeOnEsc: true,
    closeOnClick: false,
    closeButton: false,
    overlay: true,
    animation: 'zoomIn',
    preventDefault: true,
    
    
    // Triggered when jBox is attached to the element
    
    _onAttach: function (el)
    {
      // Extract the href or the onclick event if no submit event is passed
      if (!this.options.confirm) {
        var submit = el.attr('onclick') ? el.attr('onclick') : (el.attr('href') ? (el.attr('target') ? 'window.open("' + el.attr('href') + '", "' + el.attr('target') + '");'  : 'window.location.href = "' + el.attr('href') + '";') : '');
        el.prop('onclick', null).data('jBox-Confirm-submit', submit);
      }
    },
    
    
    // Triggered when jBox was created
    
    _onCreated: function ()
    {
      // Add a footer to the jBox container
      this.footer = jQuery('<div class="jBox-Confirm-footer"/>');
      jQuery('<div class="jBox-Confirm-button jBox-Confirm-button-cancel"/>').html(this.options.cancelButton).click(function () { this.options.cancel && this.options.cancel(); this.close(); }.bind(this)).appendTo(this.footer);
      this.submitButton = jQuery('<div class="jBox-Confirm-button jBox-Confirm-button-submit"/>').html(this.options.confirmButton).appendTo(this.footer);
      this.footer.appendTo(this.container);
    },
    
    
    // Triggered when jBox is opened
    
    _onOpen: function ()
    {
      // Set the new action for the submit button
      this.submitButton.off('click.jBox-Confirm' + this.id).on('click.jBox-Confirm' + this.id, function () { this.options.confirm ? this.options.confirm() : eval(this.source.data('jBox-Confirm-submit')); this.options.closeOnConfirm && this.close(); }.bind(this));
    }
    
  });
  
});

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.