All,
I have a PHP5 application written with Zend Framework and MVC. On my home page, I want to incorporate the functionality to download a dynamically generated pdf file. The way this is done is:
User clicks "download file" link.
On Click, an AJAX call occurs to a PHP controller, which takes the form data, generates the pdf and returns it as a string.
My javascript function now has the pdf string.
How can I display the user an "Open/Save" dialog to download the pdf file from javascript?
I have the following code:
<script type="text/Javascript">
$('#dlcontent').click(function(e) {
$.ajax({
type: 'POST',
url: "/downloads/dlpolicies",
data: $("#frmMyContent").serialize(),
cache: false,
dataType: "html",
success: function(html_input){
alert(html_input); // This has the pdf file in a string.
//ToDo: Open/Save pdf file dialog using this string..
}
});
});
</script>
Thanks
I would try to keep this simple. Just sumbit the form with a target="_blank" and then have PHP force the file download.
HTML CODE
<script type="text/Javascript">
$('#dlcontent').click(function(e) {
e.preventDefault();
$("#frmMyContent").submit();
});
</script>
<form id="formMyContent" action="/downloads/dlpolicies" target="_blank" ... >
...
</form>
Then on your server side, you need to tell PHP to send the response as a "download".
PHP Code
$this->getResponse()
->setHeader('Content-Disposition', 'attachment; filename=result.pdf')
->setHeader('Content-type', 'application/pdf');
Got this from here:
Zend Framework how to set headers
The simplest way of doing it is open a new window with URL to pdf string.
Add an element to your page where you want the link to go. It can be a span or a div or a cell in a table or whatever you want. Give it a unique ID. Then use jQuery to set the html of that element. I refer to it as somepageelement here.
$('#dlcontent').click(function(e) {
$.ajax({
type: 'POST',
url: "/downloads/dlpolicies",
data: $("#frmMyContent").serialize(),
cache: false,
dataType: "html",
success: function(html_input){
$('#somepageelement').html('Click here to download the pdf');
}
});
});
Related
Hey guys I was wondering if it's possible to get the content within the tags I have using PHP. I'm using the Ace editor and I have a save button. I'm trying to save the content to a file using PHP, can someone tell me how I can get the content using PHP please?
May you can save it via AJAX,
send the editor content and desired filename (and path also) using POST method
example
function saveCode () {
$.ajax({
url: 'handler.php',
type: 'POST',
dataType: 'html',
data: {code: editor.getValue(), filename: './test.php'},
})
.done(function(result) {
console.log("success");
});
}
then on the handler.php
<?php
if(isset($_POST['code']))
{
file_put_contents($_POST['filename'], $_POST['code']);
echo 'success';
}
?>
I have a post which has 3 extra custom text field and it has some content.now i have link on front and to popup that 3 fields content.When page load they are in display none mode but when i click link it opens in fancybox popup.Now i want to generate pdf of each file using tcpdf or any other easy way i am using tcpdf for my purpose but i get all 3 fields in single pdf so i use ajax when i click link it takes anchor tag value and pass to the pdf parameter via ajax but nothing seems to work here is my code for ajax call in single.php
<script type="text/javascript">
jQuery(function($){
$('nav a.cboxElement').on('click', function(){
alert('hi');
var nv = $(this).text().toLowerCase().replace(/\s+/g, "_");
alert(nv);
$.ajax({
type: "POST",
url: '<?php echo get_template_directory_uri(); ?>/popupdatatopdf.php',
data: {valueMin : nv}
}).done(function(result) {
alert(result);
console.log(result);
});
});
});
</script>
Below code if for popupdatatopdf.php i can see hello printed and can can see hello123 in pdf but the data which i got via ajax request not printing in pdf i checked in console and i can see value of request there but it does not get prited i pdf
echo "hello";
$pdfname="uu";
$mlm=$_REQUEST['valueMin'];
$nn="hell0123";
$html3.="<div style='font-size:23px;'>".$mlm.$nn."</div>";
$html3.="<div style='font-size:23px;'>ppc</div>";
I am trying to refresh a div with jquery load(); but the load displays the correct information but duplicates entire parts of the page that arent in the div
$.ajax({
type: 'POST',
url: $(this).attr('action'),
cache: false,
data: $("#uses_form").serializeArray(),
success: function(data)
{
$('#uses_form_div').load('#uses_form_div');
}
});
return false; });
i think you are having a misunderstanding..
if you want to load an external url into the div block
$('#uses_form_div').load("./a.file");
will do it. see the api docs.
Or if you are trying to load the ajax response of the $.ajax call into the div, it should be
$.ajax({
type: 'POST',
url: $(this).attr('action'),
cache: false,
data: $("#uses_form").serializeArray(),
success: function(data)
{
$('#uses_form_div').html(data); // see here
}
});
UPDATED according to comments below:
if the case of an included page to be refreshed. I have two ways to recommend.
make the included file as a separate url and load it initially. so instead of include you will be loading it via jquery as the page loads by a jquery load call. and when you want to refresh it you can do $('#uses_form_div').load("./a.file");
you can put it as include it self and when you need to update, make an ajx request get the data back. Here you have 2 choice. You can build the dom at server and give html as ajax response and simply $("#uses_form_div").html(data) or get the response as json
and build your dom at client side and load it via same $("#uses_form_div").html(data).
I also had the same problem but finally found the answer
<script type="text/javascript">
function recp() {
setInterval(function()
{
$("#result").load(location.href+ ' #my');
});
}
</script>
<div id="result">
<div id="my"><?php echo date('a:i:s'); ?></div>
</div>
I am using an iFrame in my application that lets user to upload a CSV file which has information about the new countries to be added into database. What I want is as soon as the CSV file is processed into the database the existing select box that shows current countries from the database should get updated with newly added entries of CSV file.
I am the making ajax call like
$(document).ready(function(){
jQuery.ajax({
type: "POST",
url: "get_all_countries.php",
data: '',
cache: false,
success: function(response)
{
$("#all_countries_select_box").html("<select name='all_countries' id='all_countries' MULTIPLE size='8' style='min-width:250px;'>"+response+"</select>");
}
});
});
My only problem is the id 'all_countries_select_box' is in another PHP file called 'manage_countries.php'. Is there a way to change content of that file from the file that is in iFrame (upload.php). If not, what could be the best possible solution
If you want to access the parent window from inside an iframe you should use window.parent
success: function(response)
{
var parentJQuery = window.parent.jQuery;
parentJQuery("#all_countries_select_box").html("<select name='all_countries' id='all_countries' MULTIPLE size='8' style='min-width:250px;'>"+response+"</select>");
}
if the file you wantr to modify instead is inside an iframe you should do
success: function(response)
{
$('#idoftheiframe').contains().find("#all_countries_select_box").html("<select name='all_countries' id='all_countries' MULTIPLE size='8' style='min-width:250px;'>"+response+"</select>");
}
Use jquery + php. Doing custom AJAX captcha. i.e user clicks on image, it automatically updates.
<script src="/js/jquery.js"></script>
<script>
$(document).ready(function(){
$.post('/captcha.php', {}, function(resp){
$("div").html(resp);
});
});
</script>
<div></div>
in PHP header already sent, so if it includes into <img src="/captcha.php" /> it prints captcha in jpeg. The problem seem to be is header that need to be sent. So, how can i do this? The header is sent in PHP. It doesnt work in js.
If you want to change an image in an HTML document, then change the src attribute of the image (or replace the image element with a new one). Don't use XMLHttpRequest at all.
Add header function in Your captcha.php file:
header('Content-Type: image/jpeg');
If you are loading the source from a different script then you can simply send the src of the file through a string value. In that way your captcha.php code becomes something like this
$source="/path/to/the/file.jpg";
header("Content-type:text/plain");
print $source;
When you receive it you can do the following to change the source
$("#changeCaptchaButton").click(function() {
$.ajax({
url: "captcha.php",
cache: false,
success: function(data) {
alert("changing source of image");
var source=data;
$("#captchaImg").attr("src", source);
}
});
});
If you are doing the change in the current script then don't use the ajax but use the latter jQuery method