I have a php function in which some pdf is being created. I have a button and I want to call that function on button click and then redirecting to a url. What can be the simplest method of doing that?
2
-
Put the button in a <form> and use a $_POST request and call the function on submit... What code have you got?TommyBs– TommyBs2012-10-19 14:37:29 +00:00Commented Oct 19, 2012 at 14:37
-
Do you want the submission to be async or sync to the page?Daniel Noel-Davies– Daniel Noel-Davies2012-10-19 14:38:54 +00:00Commented Oct 19, 2012 at 14:38
Add a comment
|
3 Answers
If you need to call a PHP method, I'd use AJAX. Something like this:
var btn = document.getElementById("button_id");
btn.onclick = function () {
// Make AJAX request
// On success, do this:
window.location.href = "url to redirect to";
};
The code for "Make AJAX request" can be Googled easily, and I will provide it in a minute :)
UPDATE:
function ajaxFunction() {
var ajaxRequest; // The variable that makes Ajax possible!
try {
// Firefox, Chrome, Opera 8.0+, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP.6.0");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP.3.0");
} catch (e) {
throw new Error("This browser does not support XMLHttpRequest.");
}
}
}
}
}
return ajaxRequest;
}
The AJAX code -
var req = ajaxFunction();
req.onreadystatechange = function (response) {
if (response.status == 200 && response.readyState == 4) {
window.location.href = "url to redirect to";
}
}
req.open("POST", "your PHP file's URL", true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send(null); // Or send any `key=value&` pairs as a string
1 Comment
Ian
@speakingcode Who knows what the OP wants - it's not the most specific question, very open-ended!
There are various ways to accomplish this. I would call the PHP function in ajax and redirect based on the functions return value. The following example uses jQuery:
jQuery:
$.ajax({
url: 'createpdf.php',
success: function(data) {
if (data) window.location = 'link/to/new/path';
}
});
PHP:
function create_pdf(){
//Create PDF
//If PDF was created successfully, return true
return true;
}
4 Comments
Ian
data will probably a string of "true" (or "false"), so you probably want to check for it to be == to "true", not just if (data) because that would evaluate to true for data being "true" or "false"Sam Timalsina
It's best practice for functions to return either true or false instead of the string equivalent 'true' or 'false'.That said, yes the condition if(data) would evaluate true if any string was returned.
Ian
I'm pretty sure that's not true. I agree it's "best practice", but when the response comes back for an AJAX request, it will be in one of the following formats - JSON, XML/HTML, text, script, or something related (decided by jQuery in this situation). When the server returns
true, it will be parsed by Javascript as text, and the variable data will be a string, not a boolean value. jQuery won't bother checking to see if it can be converted as a boolean, number, or something else...because technically, the server could return "true123" for whatever reason...it will be a string.Ian
Yeah, I mean technically what AJAX is composed of is an
XMLHttpRequest, and for its response, it has a responseText property. This holds exactly what the server returns, and I'm pretty sure it's always in the format as a string, as that's the only real communication between Javascript & a server. If you don't specify the dataType in the $.ajax call, jQuery attempts to parse the value by the "Content-Type" set in the headers (xml, json, script, html, etc.). If you specify dataType, jQuery only attempts to convert the value to that...and normally, it's "text/html" or "text/plain".