View Full Version : javascript function in redirection possible?

03-03-2009, 08:07 PM
Is this possible?

header("Location: Javascript: addToDb();");

03-03-2009, 08:21 PM
Not to the best of my knowledge.
Header forces a redirection header (in this instance). You can't put javascript into it because JS is a part of the output body (even though its within the 'head' section of HTML, its not the same as a header).
So... no I don't believe you can use any type of JS function within the header. I'd be pretty leary of using it even if it would work, but you could try outputting the HTML page before sending the header and use output buffering. I still don't think it will work, but only one way to find out! Post you're results.

03-03-2009, 08:21 PM
You can call a JavaScript function from the href attribute of an anchor tag using the technique you're asking about.

<a href="javascript:addToDb();">Add to database</a>

However, what you are trying will not work with PHP. PHP is executed on the server, while JavaScript is executed on the client. In other words, PHP has returned an HTML document to the client before any JavaScript is executed. You cannot put calls to JavaScript functions in PHP functions, and I expect that your header function is throwing an error.

That being said, there may be a way to accomplish what you're trying. Some more details may be helpful.

03-04-2009, 01:59 AM
You can do this with an onload in your html body tag:

<body onload="addToDb()">