TCPDF generated file does not open on ajax call - php

Everything work fine if I use this:
form name="nnewOBR" id="inewOBR" method="POST" action="targetsample_pdf.php" target="_blank"
The data from the form will be saved in the database and the PDF will be opened. But when I use ajax, it will only execute the insertion of data in the database but it will not open the PDF. My ajax statement is as follows:
$(function() {
$("#inewOBR").submit(function() {
var datos = $('#inewOBR').serialize();
$.ajax({
type: "POST",
url: "targetsample_pdf.php",
data: datos,
success: function(data) {
alert("Successful");
}
});
return false;
});
});
I would really appreciate your help. Thank you very much.

Related

AJAX Load PHP Response

How can I load data from my PHP response via ajax into a panel?
My PHP outputs correctly and I can see a table in the response, but I can;t get it to build the data on my webpage.
Here is my jquery/ajax so far. It passed the value to PHP correctly and PHP builds the table via its echo, but what am I missing for AJAX to display the table?
PHP:
<?php
foreach ($lines as $value) {
echo "<input name='data[]' value='$value'><br/>";
}
?>
JQUERY:
$(function () {
$('#rotator').change(function (e) {
var rotator = $("#rotator").val();
$.ajax({
type: "POST",
url: "tmp/JFM/National/national.php",
data: {
rotator: rotator
},
success: function (result) {
$('#panel').load(result);
}
})
return false;
});
});
The answer to this was two fold.
I was attempting to append to my main div, which apparently can't happen. I created a new empty div and was able to load the results there.
Beyond that, the comments to change .load(results) to .html(results) were needed.
The correct jquery code is below.
$(function () {
$('#rotator').change(function (e) {
var rotator = $("#rotator").val();
$.ajax({
type: "POST",
url: "tmp/JFM/National/national.php",
data: {
rotator: rotator
},
success: function (result) {
console.log(result);
$('#test').html(result);
}
})
return false;
});
});
move your function from:
$.ajax({...,success: function(){...}});
to
$.ajax({..}).done(function(){...});
if it doesn't work, try to add async:false into the ajax object...
$.ajax({...,async:false}).done(function(){...});
Hope it helps... =}

File downloading using AJAX not working

I am creating simple file downloading script using AJAX in PHP. My script is not working. Means it displaying the content of the pdf/doc file below download link after clicking on it. Below image will illustrate the problem.
Below is my code
AJAX and HTML:
$(function() {
$(".download_link").click(function() {
var test = $("#content").val();
var dataString = 'content='+ test;
$.ajax({
type: "POST",
url: "download_file.php",
data: dataString,
cache: false,
success: function(html){
$("#display").after(html);
document.getElementById('content').value='';
}
});
return true;
});
});
<a href="#" class="download_link" id="d_link">
PHP Script: (download_file.php)
<?php
ob_start();
$file = 'file.doc';
header("location:".$file);
?>
You are using $("#display").after(html); thats why its displaying the content of the file. You can download the file by following code.
$(function() {
$(".download_link").click(function() {
var test = $("#content").val();
var dataString = 'content='+ test;
$.ajax({
type: "POST",
url: "download_file.php",
data: dataString,
cache: false,
success: function(html){
window.location = 'your_file.pdf';
}
});
return true;
});
});
I think your approach is incorrect.
With your script you are trying to append file.doc contents into DOM object -> browser do not know on AJAX request how to manage document's encoding, it will not work.
For downloading documents I would not use AJAX, I think would be fine if you just open a new window with the document's url and let the borwser to manage the content's response:
window.open("download_file.php");
If you just want to display documents contents in the same page, I would use an iframe as follows:
<form action="download_file.php" method="GET" target="docIframe">
<input type="submit" value="Download"/>
</form>
<iframe name="docIframe" width="600px" height="500px"/>
You need to set header content type to tell the browser how it renders the content for PDF or DOC.
<?php
//Set header for pdf
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='file_to_download.pdf'");
readfile("original.pdf");
?>

ajax PHP progress bar

I have a code that gets some data and send it to a php page that makes the process and finally shows the result of this process without reload the page:
So, I hace this code that calls process.php file.
$(document).ready(function(){
$("#btEnviar").click(function (){
var datos = $("#formulario").serialize();
$.ajax({
type: "POST",
url: "process.php",
data: datos,
contentType: "application/x-www-form-urlencoded",
beforeSend: function() {
$("#status").html("Enviando....");
},
dataType: "html",
success: function(datos){
if(datos == 1){
$("#status").html("Script procesado satisfactoriamente");
}else if(datos == 0){
$("#status").html("Error al procesar script");
}
}
});
});
});
It is possible to make a progress bar of process.php ?
Thanks in advance for your help.
Have you looked at http://php.net/manual/en/session.upload-progress.php
Note, it doesn't work in fastCGI mode
You may use some ready-made solution like Reload Progress Bar. However, check this post first File upload progress bar with jQuery. I hope it helps.

Get only part of the message and reload only one div

this is an ajax method that inserts the data into a db and should supposedly display the new content.
<script type = "text/javascript">
$(document).ready(function() {
$('#submit').live('click', function(eve) {
eve.preventDefault() ;
var form_data = {
title: $('#title').val()
};
$.ajax({
url: "http://localhost/ci/index.php/chat/comment",
type: 'POST',
data: form_data,
success: function(msg) {
alert(msg);
}
});
});
});
</script>
However in my /chat/comment, i am loading the view again, i.e, user submits a comment, load the view again and the comment should be there. My response from server is the view's HTML. However the view comes with all the divs and there are many of them. I need to retrieve only part of the div, say, #commentspace from the ajax on success.
Look at the jQuery $.load() function?
Example
Inside "firstpage.html"
$('#content').load('secondpage.html #content');

Forms/PHP/Ajax load?

I'm currently learning PHP. I've made a simple script # http://hash.techho.me, only thing is, I want the form to submit then load the results via AJAX, without the user leaving the page. Possible?
post the form using ajax
$.ajax({
url:'yoururl',
data:$("form").serialize(),
type:'POST',
success:function(data){
alert("success");
},
error:function(jxhr){
alert(jxhr.responseText);
}
});
jQuery.ajax() – jQuery API
Posting to the same page should do the trick. No need to use ajax for that
> <?php
>
> //do stuff with $_POST
> ?>
>
> <html> <body> <form method="post">
>
> <?php echo $result ?>
>
> </form>
> </body>
Fike
use ajax for this, lets suppose try this one for your practice
var string = $("#string").val();
var dataString = 'string=' + string ;
if(string==''){
alert('enter any string');
}
else{
$.ajax({
type: "POST",
url: "path of php file",
data: dataString,
suceess: function(){
//do something
},
error: function(){
//do something
}
});
}
You can use jQuery or Prototype JS libraries to make an easy AJAX call. Example using jQuery would be:
$.ajax({
url:'hashed.php',
data:$("form").serialize(),
type:'POST',
success: function(data){
$('hashmd5').html(data.md5);
$('hashsha1').html(data.sha1);
},
error: function(jxhr){
alert(jxhr.responseText);
}
});
Don't use the same id value in HTML, never ever. They must be unique to correct perform JavaScript functions on elements.
yes it is possible. Write a javascript function that would trigger on submit, disable the submit button so user couldn't press it again, and finally request the server via ajax. on successful response update the content. Something like following in Jquery
$('.form-submit').click(function(event)) {
event.preventDefault();
if(form is valid and not empty) {
$.ajax({
type: "POST",
url: "path to script that will handle insetion",
data: "data from form", //like ({username : $('#username').val()}),
suceess: function(data){
//update the content or what. data is the response got from server. you can also do like this to show feedback etc...
$('.feedback').html("Data has been saved successfully");
},
error: function(){
$('.feedback').html("Data couldn't be saved");
}
});
}
}

Categories