Ajax Call from Add/Edit Form

Hi there all. I am attempting to update a pretty old site to 9.0.2 that started probably in C5 5.3 or something. Ive updated successfully but one particular block (so far) is giving me trouble because it uses the depreciated ‘gettools’ thingy in the add/edit form. I have been trying to change this to an AJAX ‘action’ call to the block controller but nothing I am doing is working. Can someone offer an example of how this should look in 9.0.2? I’ve had success doing this from view.php but not in add.php or edit.php.

Thanks in advance.
C

Does using this in view

<?= $view->action('your_method_name'); ?>

and having method in block controller prefixed with action_

public function action_your_method_name($bID)

work for you?

To clarify, what @Parasek suggested should also work in an edit form.

Thanks for the replies.
I had something similar (just yourMethodName instead of your_method_name).
I switched to the latter just in case the function name needed to be in that style and still not working.

Here is what it looks like…

form.php

function getThumbs(){
    var selectedFileSet = $("#form-select-fileset").val();
    //var toolURL = $("#toolURL").val();
	var toolURL = "<?=$view->action('get_the_thumbs'); ?>";
    //if they selected a fileset
    if(selectedFileSet != 'none'){
        $.ajax({
            type: "POST",
            data: {fsID: selectedFileSet, bID: <?php echo $bID?>},
            dataType: 'json',
            url: toolURL,
            success: function(thumbs) {
                $("#items-container").html(thumbs);
                indexItems();
            },
            error: function(){
                $("#items-container").html("Something went wrong...");
            }
        });
    } else{
        //they selected none
        $("#items-container").html("Choose a Fileset");
    }
}
getThumbs();
$("#form-select-fileset").change(function(){
    getThumbs();
});    

controller.php

	public function action_get_the_thumbs($bID){		

		$fsID = $_POST['fsID'];
		$bID = $_POST['bID'];

		$db = Loader::db();
		$existingThumbs = $db->GetAll('SELECT * from btVividThumbGalleryThumb WHERE bID = ? ORDER BY sort', array($bID)); //gives us all the files we've already saved/sorted
		$existingThumbIDs = array();
		foreach($existingThumbs as $thumb){
			$existingThumbIDs[] = $thumb['fID'];
		}

		$fs = FileSet::getByID($fsID);
		$fileList = new FileList();            
		$fileList->filterBySet($fs);
		$fileList->filterByType(FileType::T_IMAGE);
		$fileList->sortByFileSetDisplayOrder();
		$files = $fileList->get(); //gives us all the files in the set

		//we're going to make a new array of thumbs from the files in our fileset.
		$allThumbs = array();
		foreach($files as $file){
			if(in_array($file->getFileID(),$existingThumbIDs)){
				$sort = $db->GetOne('SELECT sort from btVividThumbGalleryThumb WHERE bID = ? and fID = ?', array($bID,$file->getFileID()));
				$thumb = array('fID'=>$file->getFileID(),'sort'=>$sort);
			} else{
				$thumb = array('fID'=>$file->getFileID(),'sort'=>9999);
			}
			$allThumbs[] = $thumb;
		}   
		$allThumbs = thumbSort($allThumbs,'sort',SORT_ASC);
		foreach($allThumbs as $thumb){
			$file = File::getByID($thumb['fID']);
			$html .= "<div class='thumb-item-shell'>";
			$html .= "<img src='".$file->getRecentVersion()->getThumbnailURL('file_manager_listing')."'>";
			$html .= "<div class='thumb-file-name'>".$file->getFileName()."</div>";
			$html .= "<input type='hidden' name='fID[]' value='".$file->getFileID()."'>";
			$html .= "<input type='hidden' name='sort[]' class='item-sort'>";
			$html .= "</div>";
		}

		echo json_encode($html);
		exit;
	}

Still get a 500 Internal Server Error that says ‘Unable to find the specified block’.
Does it matter that this is an unpublished draft? Shouldn’t, right?

As you suspect, it is likely the case that this is an unpublished block. It stems from the add/edit cycle.

Before the add block is saved, the bID or block instance doesn’t actually exist.

A quick and way round it is to add the block before enabling the ajax functionality, then edit again with the ajax functionality enabled.

A more sophisticated way round is use the routing system to set up an entry point and to route the ajax either back to the block type controller or to another class set up especially for the purpose.

Which you choose depends on who the users are and how slick an experience is required.

Either way, consider that during an add you won’t be able to connect to the actual instance of the block. But you will be able to do so during an edit.