1

I'm trying to validate a registration form using HTML and PHP, and insert the data into an SQL database. Upon registration, I'd like for my user to be directed to their personal dashboard. I can get everything to work except for the redirect upon form submission.

Here is the PHP at the start of the page:

<?php


// define variables and set to empty values
$emailErr = $passErr = $confirmErr = $email = $pass = $confirm = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
  }

  if (empty($_POST["pass"])) {
    $passErr = "You must have a password";
  } else {
    $pass = test_input($_POST["pass"]);
  }

  if (empty($_POST["confirm"])) {
    $confirmErr = "You must confirm your password";
  } else {
    $confirm = test_input($_POST["confirm"]);
  }  

  if ($pass != $confirm) {
    $confirmErr = "Your passwords must match";
  } 

}

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $email = test_input($_POST["email"]);
  $pass = test_input($_POST["pass"]);
    }

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
    }

?>

And my HTML form:

<div class="container">

    <form class="form-signin" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
      <h2 class="form-signin-heading">Register below:</h2>   
      <span class="error"><?php echo $emailErr;?></span>  
      <input type='email' class="form-control" placeholder="enter email..." name='email'/>
      <span class="error"><?php echo $passErr;?></span>
      <input type='password' class="form-control" placeholder="enter password" name='pass'/><span class="error"><?php echo $confirmErr;?></span>
      <input type='password' class="form-control" placeholder="confirm password" name='confirm'/>

      <input type='submit' class="btn btn-success btn-block" name="submit" value="submit">
      <h5>Already have an account? <a href="login.html">Log in</a>.</h5>
    </form>

  </div>

Note the use of

<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> 

on

<form action=""> 

param. Using this function I can't get a redirect.

I've included my SQL entries following the HTML, and the entries are working just fine, but the header(); tag doesn't seem to work. See here:

<?php

$email = $_POST['email'];
$pass = md5($_POST['pass']);

// Database connection
require 'database.php';
$sql = "SELECT email FROM users WHERE email = '".$email."'";

$result = $conn->query($sql);
$row_count = $result->num_rows;

if ($row_count == 1) {
    // user already exists
    header('Location: login.php');
} elseif ($row_count > 1) {
    // just to double check multiple entries aren't input into the DB
    echo 'There are too many records with that name!';
} else {
    // enter user into DB
    $sql = "INSERT INTO users (email, pass)
    VALUES ('".$email."', '".$pass."')";

    if ($conn->query($sql) === TRUE) {
        header('Location: dashboard.php');
        exit();
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

mysqli_close($conn);
}

?>

Any ideas what I'm doing wrong?

4 Answers4

0

If I understand it right, what you can do is redirect back to your page with the link :

<form name="form-lignin" method="post" action="this-php-page.php">

On the same page you can then create a section that will show up only if the form has been submitted and the form will be hidden. The section can be personalised for each user then as well.

You can do this by adding a hidden field to your form.

<input type='hidden' name='submission' value="something"/>

Then you can add this php :

            if ($_POST["submission"]=="something") {

        *Dashboard content?*    

              } else {

        *The actual form...*
              }

Hope this helped :)

  • This gave me a blank page upon valid form submission. Still, no redirect. – ReganMathew Jan 25 '16 at 10:12
  • I don't get it, do you want to redirect on another page or stay on that page with he form validated and data extracted? – Benjamin S Jan 25 '16 at 10:19
  • also if possible I'd like to keep the "" part in the code. Every tutorial I've seen suggests this is the safest when using form validation – ReganMathew Jan 25 '16 at 10:22
  • I want to redirect TO another page. But I need to ensure the data are correct. – ReganMathew Jan 25 '16 at 10:22
  • So just add the link to the other page in "
    " and you can get the value through the POST.
    – Benjamin S Jan 25 '16 at 10:24
  • Using this method, I can't get the form to validate without redirecting to another page. The user will not be alerted to any issues with their information. Is it possible to get the page validated, and then once it has been validated, send the data to the .php file in which I'll enter the info into the database, and then redirect to the dashboard? – ReganMathew Jan 25 '16 at 12:00
  • Well this seems quite complicated but doable with what I posted first. Using the submission "if" and another "if" checking the validity of all the entries. If all are good, then just redirect (with php redirect or JS)... Honestly, the best solution actually is to do the verification in JavaScript as the user types it in. The error messages appear directly and form validation is disabled until everything is filled out correctly. -> if you need help on this, there are templates (using onChange event,..) – Benjamin S Jan 25 '16 at 13:17
  • This seems workable. Strange that there's no simple solution in PHP. I imagine this is a very common task. I'll try this in the morning – ReganMathew Jan 25 '16 at 13:20
0

hi i have seen you code in your code you are using login.php for redirecting from header tag. and in html file you are using login.html file for checking account is exists or not. Please check file extension for your login file. you can use following link for header function details: PHP header(Location: ...): Force URL change in address bar

Community
  • 1
  • 1
chetan
  • 249
  • 3
  • 6
  • While you're correct, this is inconsequential for the problem I'm having. Both the login.html and login.php files exist. Upon form submission and validation I am getting a blank form once again. The user's data is entered into my DB, but the page is not redirected. – ReganMathew Jan 25 '16 at 09:57
  • please try for redirecting @header("Location:dashboard.php") – chetan Jan 25 '16 at 10:04
  • please check this link for your help http://stackoverflow.com/questions/12525251/header-location-not-working-in-my-php-code – chetan Jan 25 '16 at 10:24
0

1.you need ensure the code has run

    if ($conn->query($sql) === TRUE) {
        echo 'is run!';exit;
        header('Location: dashboard.php');
        exit();
    }
and you can use if ($conn->query($sql)) instand if ($conn->query($sql) === TRUE)

2.notice ,if the "exit;" code run ,"mysqli_close($conn);" will useless。

suibber
  • 267
  • 1
  • 6
0

You first need to put all your php code above the html so that the html output will be sent to the browser after execution of the validation script.

Then

1 Capture form values

2 Validate form values

3 If Every thing is ok. Then sanitize the input and insert it into the database.

4 if insert successful, Redirect it to another page, Using header('location:http://example.com');

Thats it

codersrb
  • 140
  • 1
  • 12
  • could you perhaps write out the code so I can see how I would implement this change? Or use my code and show me the code i would use (and where) – ReganMathew Jan 25 '16 at 12:03