Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Jackson Monk
4,527 PointsHow to redirect user away from page form data was sent to?
When sending a form in HTML, the action attribute is where the form data is sent to, but it is also automatically where the user is redirected to when the form is submitted. However, I don't want the user viewing a php page where I process the form data, so how can I redirect them to another page while still maintaining the destination of the form data?
1 Answer

Steven Parker
227,140 PointsNormally, it's the server's job to determine what is sent back to the browser when a form is received. This would include redirection.
But if you can't take care of this the normal way for some reason, you could use AJAX to submit your form instead of the normal mechanism, and then direct the browser wherever you like by changing the location property of the window (more specifically, location.href).
Jackson Monk
4,527 PointsJackson Monk
4,527 PointsHow would the server redirect you after it takes you to the page you submitted it to?
Steven Parker
227,140 PointsSteven Parker
227,140 PointsThe server might send back an HTTP header with a 303 ("see other") code and the new location. This would be done instead of sending back another response.
See this MDN page on HTTP Redirection for more details.
Jackson Monk
4,527 PointsJackson Monk
4,527 PointsWould it be possible to make a success callback for the form inside the submit event?
Steven Parker
227,140 PointsSteven Parker
227,140 PointsIf I understand what you mean, you're thinking of handling the submit event in JavaScript, and redirecting the browser from there? If so, that's certainly OK if you want to do that instead of actually submitting the form, but otherwise I'm not sure you can guarantee the order of events and switching the page might interfere with the submit.
Jackson Monk
4,527 PointsJackson Monk
4,527 PointsHmm, a sticky situation. Well is it possible to redirect the page with php after the form data has been processed?
Steven Parker
227,140 PointsSteven Parker
227,140 PointsAbsolutely. That's the "normal way" I first mentioned.
Jackson Monk
4,527 PointsJackson Monk
4,527 PointsOh, sorry for making you go on that hypothetical train ride then lol. So how can we do it with php?
Steven Parker
227,140 PointsSteven Parker
227,140 PointsYou'll find that covered in the Build a Basic PHP Website course, in the video on Redirecting After a Form Submission.
Jackson Monk
4,527 PointsJackson Monk
4,527 PointsI don't think the header() redirect is working. The browser just directs me to the php page I'm sending the information to and it just displays all the code I've written.
Steven Parker
227,140 PointsSteven Parker
227,140 PointsPHP code should never be sent to the browser. It sounds like either a syntax error is preventing the PHP from being recognized, or the server isn't configured correctly to use PHP..
Jackson Monk
4,527 PointsJackson Monk
4,527 PointsWould you be able to send me a working, simple example of a php page interacting with an html page? I feel like the reason its not working is because I'm not linking it to the page properly
Steven Parker
227,140 PointsSteven Parker
227,140 PointsYou should find working code examples in the course I mentioned. Have you taken it already?
There are many possible causes in both code and configuration. To facilitate a complete analysis, make a snapshot of your workspace and post the link to it here.
Jackson Monk
4,527 PointsJackson Monk
4,527 PointsI have watched the video you mentioned, but I think I need to learn a little more about php before I'm ready to do this. Thank you so much for all your help