Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

How 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
Steven Parker
216,164 Points

Normally, 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).

How would the server redirect you after it takes you to the page you submitted it to?

Steven Parker
Steven Parker
216,164 Points

The 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.

Would it be possible to make a success callback for the form inside the submit event?

Steven Parker
Steven Parker
216,164 Points

If 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.

Hmm, a sticky situation. Well is it possible to redirect the page with php after the form data has been processed?

Steven Parker
Steven Parker
216,164 Points

Absolutely. That's the "normal way" I first mentioned.

Oh, sorry for making you go on that hypothetical train ride then lol. So how can we do it with php?

I 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
Steven Parker
216,164 Points

PHP 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..

Would 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
Steven Parker
216,164 Points

You 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.

I 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