I want to alert the user that the username is already taken and put them back on the new username page. How do I call a javascript function in php: I wrote somthing like this in my php script echo "<script language=javascript>alert('Username already taken.')</script>"; header ("Location: www.test.com"); It doesn't work. How do I fix this or is there another way I can do the same thing. ~Imozeb
You can't call javascript in PHP since PHP is a server side language and JavaScript is a client side language. What you need to do is set the error either using sessions or using a GET var, and passing them to the page you want to. Then on that page check if either of those are set, and echo out the javascript on that page if they are Personally I think javascript alerts like this are overkill when you can just use a simple div with bold colours to display an error. The only time I would even consider using it would be if you were doing this via AJAX
You can't output stuff and redirect to a new page at the same time. One or the other. For the sake of providing a decent interface, I'd recommend re-displaying the registration page with a clear notification of the problem in a red box or something similar, right next to the place where the user enters the username. That puts the problem in context and makes it easier for them to deal with. And javascript alerts are supremely annoying.
The reason it does not work is because before the javascript code is prepared to display and process in your browser in order for the message to display, your page will already have been redirect to another page due to header function. Try a stack of both alert and postponed redirect in javascript if you insist on using alert function. If not, I would say others' suggestion about displaying a clear error message in some kind of div right on registration page would the be best solution. Regards.