-1

After the form's data is sent to MySQL I want to redirect the user to another page. (This will be the payment system page, but for now I do not have the URL for the payment page, so I am trying to redirect to index.php) Unfortunately, the redirect is not working. What am I doing wrong here?

The form's data is being received by mysql database. I checked it.

PHP File:

<?php
//Only process the form if $_POST isn't empty
if (!empty( $_POST) ) {
    // Connect to MySQL
    $conn = mysqli_connect('localhost', 'root', 'root', 'lpw_');

    // Check our connection
    if (!$conn) echo 'Could not connect DB';

    // Insert our data
    $sql = "INSERT INTO play ( mobilenumber, mntimes, landlinenumber, lntimes, 
            otherphonenumber, opntimes, firstname, lastname, age, city, country, email ) 
            VALUES ( '{$conn->real_escape_string($_POST['mobilenumber'])}',
                '{$conn->real_escape_string($_POST['mntimes'])}',
                '{$conn->real_escape_string($_POST['landlinenumber'])}',
                '{$conn->real_escape_string($_POST['lntimes'])}',
                '{$conn->real_escape_string($_POST['otherphonenumber'])}',
                '{$conn->real_escape_string($_POST['opntimes'])}',
                '{$conn->real_escape_string($_POST['firstname'])}', 
                '{$conn->real_escape_string($_POST['lastname'])}',
                '{$conn->real_escape_string($_POST['age'])}',
                '{$conn->real_escape_string($_POST['city'])}',
                '{$conn->real_escape_string($_POST['country'])}',
                '{$conn->real_escape_string($_POST['email'])}' )";
    $insert = mysqli_query($conn, $sql);

    // Print response from MySQL
    if ($insert) {
        header("Location: index.php");
    } else {
        die("Error: {$conn->mysql_errno} : {$conn->error}");
    }
    // Close DB connection
    $conn->close();
}

HTML Page:

<form action="" method="post">
  <div>
        <label for="mobilenumber">Mobile Number *</label>
    <div>
       <input name="mobilenumber" type="text" id="mobilenumber" placeholder="Mobile Number">
    </div>
  </div>

  ...

  //the same kind of code, only diferent values.

  ...

  <input type="submit" name="Submit" id="Submit " value="Next step"/>
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Sauer
  • 3
  • 2

2 Answers2

0

Try this code:

header('location:http://localhost/yourprojectname/index.php');
  • 4
    while the full URL is technically required, its unlikely to be the real issue here. yet to see a browser that did not handle a relative url correctly –  Nov 20 '15 at 22:48
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) out of the code really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Rizier123 Nov 20 '15 at 22:49
  • 1
    Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Nov 20 '15 at 22:53
  • @Bas van der Hoeven I tried but did not work. – Sauer Nov 21 '15 at 22:41
0

Call exit();after header("Location: index.php");, otherwise the script execution will not get terminated. Setting header alone is not enough to redirect.

if ($insert) {
    header("Location: index.php");
    exit();
}else{
   die("Error: {$conn->mysql_errno} : {$conn->error}");
}
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • That was going to be my response but there's not enough information in the question to resolve this. You can have output after `header()` and it will still work. It's usually because of output before `header()` that causes problems. – Paul Carlton Nov 20 '15 at 22:57
  • @DataHerder Yes, that might be the issue. – Rajdeep Paul Nov 20 '15 at 23:04
  • @RajdeepPaul @DataHerder I tried before using `code` exit(); `code` like above, but if I put this piece of code, when I click in submit the page change to a blank (white) page. I can't see any error, only a white page. Should appear any error in this page? How can I setup the config to show the errors? Thanks – Sauer Nov 21 '15 at 22:40
  • @Sauer, blank (white) page? Make sure your *display error* is *on* and *error reporting* is set to *E_ALL* in *php.ini* file. You can even explicitly set these parameters in your php script like this, `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);`. And also [read this answer](http://stackoverflow.com/a/8028987/5517143) to know about probable header redirection issues. – Rajdeep Paul Nov 22 '15 at 00:17
  • @RajdeepPaul I got setup the display error.... and I got this error.... `Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/lotto/rascunho_play.php:2) in /Applications/MAMP/htdocs/lotto/rascunho_play.php on line 40` – Sauer Nov 22 '15 at 18:39
  • @RajdeepPaul It is working now... Thanks a lot... the problem was that I left 1 space line in the begining of the code... actually it was the first line... I just put the ` – Sauer Nov 22 '15 at 18:59
  • @Sauer Happy to hear. Please accept an answer to close this question. :) – Rajdeep Paul Nov 22 '15 at 19:13