简体   繁体   中英

Printing echo before header(), php

I've meet an issue with php header().

I have an index.php page with a contact form in it. After the form is submitted, its action goes to the same index.php page, where a handler.php script is required. It does the following steps, if the POST has been received:

  1. Recaptcha check:
  2. If recaptcha lets you through:

2.1) echo "/js script tag which alerts smth like 'captcha is verified!'/";

2.2) require one more new_order.php script file, which sends a new order to my CRM and then redirects you through header(location ...); to the abovementioned index.php page.

  1. If recaptcha doesn't let you through:

3.1) echo "/js script tag which alerts smth like 'captcha is not verified!'/";

If 3. happens - the page shows me a captcha-fail js-alert and automatically goes to the index.php, without calls to header();

If 2. happens - a new CRM order is successfully created (because new_order.php does the job), header(); gets me to the index.php, BUT 2.1 echo doesn't alert a message. It shows up, when I comment the line with 2.2) require(new_order.php).

I need this redirect to avoid sending 'Repeat the form send'in my browser. When redirect is done, you refresh the page and it won't say 'Submit the form another time?'

I know it's something about not including some info before header, but I can't grasp the main sense.

Thank you!

Pass some additional params in each of your cases that acts like a 'flag'. Accordingly, set or dont set the custom headers() and process it conditionally.

不可能在标题前打印 echo。

I resolved the issue:

I set a cookie in the end of my new_order.php script by setcookie(blah);.

I add an if-check in the beginning of the handler.php.

When I redirected to the mentioned index.php, the required handler checks, whether my cookie is set.

If it is - it means a have made an order, thus I receive my echo "\\script\\alert('Thank you!');\\script\\";

If it is not set - I never called to the new_order.php!

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