PHP File Download Using Hidden IFrame Without Saving
===

I apologize in advance for the confusing question wording, but I couldn't figure out how to put this.

I essentially have a string in a database that I intend to make available for user download. How would I go about doing this? 

I was attempting to use ajax, but I wasn't sure how to go about it. 

The following jquery code is executed when the download link is pressed

   	$.ajax({

		url: 'index.php/script/downloadFile',
		type: 'post',
		data: {name: $(this).text()}
		
	});


The relevant PHP code looks like: 

    public function downloadScript(){

        $name = $_POST['name'];

        $filename = $name . ".txt";
        $string = //String that comes from database using name to create query
   	    $filename = $name . ".txt";

		header('Content-type: text/plain');
		header('Content-Disposition: attachment; filename="' . $filename . '"');
		header("Content-Length: " . strlen($string));
		header("Connection: close");

        echo $string;
    }

Essentially, I want the string to be there for a user to download as a text file. I didn't know if AJAX was the right way to do this or if there was a better way to do the same task. 

Any help is appreciated!