1

I want my form to insert the data into an SQL database using PHP which is a separate file while, going to another webpage once the form has been submitted.

<form action="http://localhost:8888/phpv1/studentadded.php" method="post">
<input type="submit" name="submit" value="Send">

Currently I use the following code which works in the sense that it invokes the PHP and causes it to submit the data into the SQL database. However it goes to a blank webpage (the page of the PHP) instead of going to an alternate webpage. What code should I add so that when submitted it goes to an alternate web page? Thanks :)

Here is my full php script:

<?php

if(isset($_POST['submit'])){

$data_missing = array();    

if(empty($_POST['email_banned'])){

    // Adds name to array
    $data_missing[] = 'Email';

} else {

    // Trim white space from the name and store the name
    $email_banned = trim($_POST['email_banned']);

}

if(empty($_POST['notes'])){

    // Adds name to array
    $data_missing[] = 'Notes';

} else {

    // Trim white space from the name and store the name
    $notes = trim($_POST['notes']);

}

if(empty($data_missing)){

    require_once('mysqli_connect.php');

    $query = "INSERT INTO banned_emails (id, email_banned, created_on, notes) VALUES ( NULL, ?, NOW(), ?)";

    $stmt = mysqli_prepare($dbc, $query);


    //i Interger
    //d Doubles         
    //s Everything Else


    mysqli_stmt_bind_param($stmt, "ss", $email_banned, $notes);

    mysqli_stmt_execute($stmt);

    $affected_rows = mysqli_stmt_affected_rows($stmt);

    if($affected_rows == 1){

        echo 'Student Entered';

        header("Location: http://localhost:8888/phpv1/test2.php");

        mysqli_stmt_close($stmt);

        mysqli_close($dbc);

    } else {

        echo 'Error Occurred<br />';
        echo mysqli_error();

        mysqli_stmt_close($stmt);

        mysqli_close($dbc);

    }

} else {

    echo 'You need to enter the following data<br />';

    foreach($data_missing as $missing){

        echo "$missing<br />";

    }

}

}

?>

Have I placed the header in the right place? (I will obviously change the content of the header)

5
  • In php page after the sql code you can try to use a Header location (Google about it) Commented Dec 11, 2014 at 16:10
  • Hmmmm, i guess you want to redirect to another page, for this you can use header('Location: another_page.php'); Commented Dec 11, 2014 at 16:11
  • You probably don't have an "if success/echo this" statement. Commented Dec 11, 2014 at 16:12
  • You're going to need to post your related code. This is anybody's guess at this point. Commented Dec 11, 2014 at 16:19
  • I have updated with the full code, have I placed the header in the right place? Commented Dec 11, 2014 at 19:12

2 Answers 2

3

You should edit studentadded.php so it redirects to the desired destination after processing the data.

header("Location: http://example.com/");
Sign up to request clarification or add additional context in comments.

2 Comments

Let's just hope OP isn't outputting before header (wink). That's why I left a comment under OP's question to show us the related code; but this would work.
I have edited the post, have I placed the header in the right place? Thanks :)
0

I would do it something like this:

if(isset($_POST['submit'])) {

  //Your insert into the DB etc 

  header("location: http://www.google.com");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.