tags:

views:

46

answers:

3

I have a vertical scrolling website (lots of in-page links). I also have a contact form script I'm working on.

I'm trying to set it up so when someone completes/submits the contact form, it redirects them to #contact_area (on the same page), but calling the header function after is throwing a "Cannot modify header information" error.

Any suggestions on how to redirect after a script is processed from within ?

Thanks!

A: 

You can work around this using output buffering, few examples here

seengee
A: 

I am assuming you are posting the form back to the same right? Maybe submit the contact form to another page for example contactus.php and once its been successful place your header location code into that OR you could use the jquery form plugin (submit() function I have used) which runs in the background and then you could maybe jquery scrollTo() your hashtag.

Go got option 1 and if you have time then play with the jquery version maybe.

PHPology
+3  A: 

A header redirect needs to happen before PHP prints any output. If you want to direct the user to an anchor on the current page you have two options:

  1. Submit the form as normal. Your PHP script processes the data and does this before any output: header("Location: /my_same_page#contact_area"); The page will be reloaded but they'll end up in the right spot.

  2. Submit the form data via AJAX and then scroll to the #contact_area anchor.

The second options is probably the cleanest but the first one should be a lot easier for you to implement.

joeynelson