0

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? Commented Oct 19, 2012 at 14:37
  • Do you want the submission to be async or sync to the page? Commented Oct 19, 2012 at 14:38

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

@speakingcode Who knows what the OP wants - it's not the most specific question, very open-ended!
1

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

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"
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.
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.
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".
0
<form name="input" action="whatever.php" method="get">
...
<input type="submit" value="Submit">
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.