-1
<?php
  require ('db_connect.php');

  $Name = $_POST["name"]; $Email = $_POST["email"]; $Message = $_POST["message"];

  if( isset($_POST["submit2"]) ) {
    $insertString2 = "INSERT INTO Messages(Name,Email,Message)      
      VALUES('$Name','$Email','$Message')";

    mysql_query($insertString2);
    header("Location:register.php?msg=Successfully Send Message! You will get reply soon...");
  }

?>

This is my code. The MySQL part is working. But this does not redirect to register.php. Why?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Dinistro Dec 17 '14 at 07:07
  • does file exists? verify the path etc, error? – MixedVeg Dec 17 '14 at 07:07
  • Is it even going to `register.php`? – Darren Dec 17 '14 at 07:11

2 Answers2

3

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... 
slevy1
  • 3,797
  • 2
  • 27
  • 33
  • Note that you don't _have_ to specify a full URL in the `Location:` header. The latest [HTTP specification](http://tools.ietf.org/rfcmarkup/7231#section-7.1.2) says "When it has the form of a relative reference ([\[RFC3986\], Section 4.2](http://tools.ietf.org/rfcmarkup/3986#section-4.2)), the final value is computed by resolving it against the effective request URI ([\[RFC3986\], Section 5](http://tools.ietf.org/rfcmarkup/3986#section-5))". – Nisse Engström Dec 17 '14 at 11:11
  • True, but if a browser supports http1.0, such as wget, then the web server even if it ordinarily supports http1.1 might fall back with an http1.0 response in which case my advice is sound for maintaining backwards compatibility with http1.0. (see http://tomcat.apache.org/tomcat-5.5-doc/config/http.html#HTTP/1.1_and_HTTP/1.0_Support) – slevy1 Dec 18 '14 at 00:27
0

"It is important that header() must be called before any actual output is sent"--you may miss this point.

cs95
  • 379,657
  • 97
  • 704
  • 746
Sourav Ghosh
  • 1,964
  • 4
  • 33
  • 43