token in JQuery script

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
riverman
User
Posts: 158
Location: Stockholm/Sweden

token in JQuery script

Post by riverman »

Hi,
I have a form with parts in javascript. I want to use the token protection in my PHPMaker project and not disable "Check token for form post".

I read that I should add: token: EW_TOKEN,

Javascript is not exactly my best area...It does not work for me, probably because I did something wrong...

<script>        
	$('#upload_btn').on('click', function(){
		var fd = $.FileDialog({
			id: 'upload_form',
			token: EW_TOKEN,
			action: './index.php'
		});
		
		fd.on('files.bs.filedialog', function(ev) {
			$.each(ev.files, function(index, obj){
				$('#upload_form').append(
						"<input type='hidden' name='names[]' value='" + obj.name + "'>" + 
						"<input type='hidden' name='contents[]' value='" + obj.content + "'>"
				);
			});
		});
	});
			
</script>

I


mobhar
User
Posts: 11703

Post by mobhar »

Assume your form is a part of your "Custom File", and it is a .php file, then change:
token: EW_TOKEN,

to:
token: "<?php echo CurrentPage()->Token; ?>",


riverman
User
Posts: 158
Location: Stockholm/Sweden

Post by riverman »

Hi,

I had misunderstand how the code was working! Thanks Mobhar for pointing me in the right direction!

Here is the final solution:

	$('#upload_btn').on('click', function(){
		var fd = $.FileDialog({
			id: 'upload_form',
			action: './index.php'
		});
		
		fd.on('files.bs.filedialog', function(ev) {
			$.each(ev.files, function(index, obj){
				$('#upload_form').append(
						"<input type='hidden' name='names[]' value='" + obj.name + "'>" + 
						"<input type='hidden' name='contents[]' value='" + obj.content + "'>" +
						"<input type='hidden' name='token' value='<?php echo CurrentPage()->Token; ?>'>"
				);
			});
		});
	});

Post Reply