You may put a header call in an if-conditional preceded by other code, as long as you follow what the Manual recommends:
You can use output buffering ... by calling ob_start() and ob_end_flush() in your
script, or setting the output_buffering configuration directive on in your php.ini
or server configuration files.
If you wish to alter your PHP.INI file, you may turn output buffering on, as follows:
output_buffering = On
With output buffering turned on, this code structure should work:
Try this for your header call:
// setting variables here, then:
if (condition) {
// other code here, then:
$location = "http://yourdomain.whatever/register.php";
$encoded = urlencode("Successfully Send Message! You will get reply soon...");
header("Location: $location?msg=$encoded");
exit;
}
Note: when you use header() if you have any concern with maintaining backwards compatibility with http1.0, then you should provide a full url, including the protocol whether that is 'http', 'https' or something else. Also your query string contains characters that need to be urlencoded. After passing the value to be encoded to urlencode(), the string looks like this:
Successfully+Send+Message%21+You+will+get+reply+soon...
Part of the joy of using PHP is that it does nice things like automatically decoding the encoded url. So all you need do to display a message to the user is write something similar to the following code at register.php:
<?php echo htmlentities($_GET['msg']);
if you wish to decode the $_GET variable with JavaScript, just be aware that PHP and JavaScript do not urlencode spaces the same way, so you need to manually decode the $_GET variable's value yourself rather than fully relying on JavaScript's decodeURIComponent(). The following JavaScript will urldecode the value of "msg", including converting any '+' characters into spaces:
var str=location.search.substring(5);
str = decodeURIComponent( str );
str=str.replace(/\+/g, ' ');
alert(str); //Successfully Send Message! You will get reply soon...