Hello, I want to be able to echo a redirect url, based on a few variables. The content type of this php script is set as Header("content-type: application/x-javascript"); here is my code:

$row = mysql_fetch_row($result);	
echo "window.location = \"http://myurl.com/test/".$row[0]."/index.php?name=".$row[0]."\";";

Right now the url just outputs the url as text, but it doesn't go there. I am calling this script from a form, it is called when the user posts the submit button.

Yeah I have to first get the url from database, which is based on the current page.

Hi,
Buddy is right,

You should always remember to
enclose your javascript code inside <script language=javascript></script>
block, in order to let browser know that this is a javascript code ant a sentence.

Hi,
Buddy is right,

You should always remember to
enclose your javascript code inside <script language=javascript></script>
block, in order to let browser know that this is a javascript code ant a sentence.

No that's incorrect. If you look at the original post, my header is:

Header("content-type: application/x-javascript");

I figured out what I was doing wrong though, my database wasn't being altered correctly when I did my update functions I got it working now though, thanks guys!

No that's incorrect. If you look at the original post, my header is:

Header("content-type: application/x-javascript");

I figured out what I was doing wrong though, my database wasn't being altered correctly when I did my update functions I got it working now though, thanks guys!

JavaScript isn't executed if you just download the file. That is, if you add the Mime-Type that tells the browser is javascript, and send it the content, it will not execute it.
It is only executed inside HTML, that is, when the browser finds an embedded script in HTML, it will assume it must be executed.

So you need a HTML file, with embedded <script> tag.

:)

You can also just use the PHP header() function as mentioned.

$row = mysql_fetch_row($result);	
header("http://myurl.com/test/".$row[0]."/index.php?name=".$row[0]."/";

Javascript will execute the code. Just try putting an alert, where the header type is
Header("content-type: application/x-javascript"); you will see :-)

Like I said before a header WOULD NOT WORK since I have to pull information from a database first. You MUST put a header at the top of the page.

Javascript will execute the code. Just try putting an alert, where the header type is
Header("content-type: application/x-javascript"); you will see :-)

That is interesting...
I've created an example here: http://de-test1.appjet.net/
But it doesn't execute for the browsers I tested.

It does not seem to me that a JS resource should be executed on its own, but meant to be executed in the context of another application, such as a HTML Document on a browser, XUL document in Firefox, as Actionscript in Flash etc. etc.

Like I said before a header WOULD NOT WORK since I have to pull information from a database first. You MUST put a header at the top of the page.

The HTTP Response Header just needs to go before the HTTP Response body, it does not matter where in the PHP script it is issued...
You can use output buffering if you need. http://www.php.net/ob_start

eg: for a redirect:

// at the top of your page start the HTTP buffer
ob_start();

// heres your HTML
echo '<p>I am a paragraph</p>';

// then you decide to redirect, clear the buffer
ob_end_clean();
// issue the change in location. 
header('Location: http://example.com/');
This article has been dead for over six months. Start a new discussion instead.