简体   繁体   中英

Can I have PHP redirect to an in-page link when I process a form?

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

您可以使用输出缓冲解决此问题, 这里有一些示例

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM