duanla1996 2013-01-15 19:35
浏览 44
已采纳

PHP表单验证无法正常工作

I have a registration script on my page, and it's handled like this:

<?php
include "inc/config.php";
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $email = $_POST['email'];
        $pass = $_POST['pass'];
        $conditions = $_POST['conditions'];
        if($firstname==""||$lastname==""||$email==""||$pass==""||$conditions==""){
        echo "
        <p>Tiedoissa oli puutteita :(</p>
            <ul>";
            if($firstname == ""){
                echo "<li>Etunimi puuttuu.</li>";
            }
            if($lastname == ""){
                echo "<li>Sukunimi puuttuu. </li>";
            }
            if($email == ""){
                echo "<li>Sähköposti puuttuu. </li>";
            }
            if($pass == ""){
                echo "<li>Salasana puuttuu. </li>";
            }
            if($conditions == ""){
                echo "<li>Luitko ehdot? </li>";
            }
        echo "</ul>";
        return false;
        }
        $rq = "INSERT INTO Users (Firstname,Lastname,Email,Password) VALUES ($firstname,$lastname,$email,$pass)";
        if(!mysqli_query($dblink,$rq)){
            echo "Rekisteröityminen epäonnistui tuntemattomasta syystä! :(";
        }
        else{
            echo "Rekisteröinti onnistui!<br>
            Käyttäjätunnuksesi on <strong>$email</strong> ja salasanasi on <strong>$pass</strong>.<br>
            Voit nyt kirjautua sisään. 
            ";
        }


?>

I'm having troubles finding the right method of comparing the form value, as I've tried ==,=== and NULL on comparison. But I'm always getting all of the error messages, or none of them. What I'm doing wrong?

Here's the html of the form:

<form class="form-horizontal span6" method="post" id="registrationform">
          <div class="control-group">
            <label class="control-label">Etunimi</label>
            <div class="controls">
              <input type="text" name="firstname">
            </div>
          </div>
          <div class="control-group">
            <label class="control-label">Sukunimi</label>
            <div class="controls">
              <input type="text" name="lastname">
            </div>
          </div>
          <div class="control-group">
            <label class="control-label">Sähköposti</label>
            <div class="controls">
              <input type="text" name="email">
            </div>
          </div>
          <div class="control-group">
            <label class="control-label">Salasana</label>
            <div class="controls">
              <input type="password" name="pass">
            </div>
          </div>
          <div class="control-group">
            <div class="controls">
              <label class="checkbox">
                <input type="checkbox" name="newsletter"> Tilaan uutiskirjeen
              </label>
              <label class="checkbox">
                <input type="checkbox" name="conditions"> Hyväksyn ehdot
              </label>
              <button type="submit" class="btn btn-success" id="registerbtn">Rekisteröidy</button>
            </div>
          </div>
        </form>
  • 写回答

2条回答 默认 最新

  • dtr32787 2013-01-15 19:37
    关注

    Since your code is not in a function hence return false; is not working as you expect, and code continues to execute below. Your comparisons may already be fine. Change that return false; to die();

    Ok so here is probably where the problem lies

    if($firstname=="" || $lastname=="" || $email=="" || $pass=="" || $conditions==""){
    

    You are checking for that and that check is working even when you dont submit the form and it appears like everything has an error. So you need to make that comparison only when the form is submitted and not while normal load. So wrap your php code around some isset like

    if(isset($_POST["firstname"]) && isset($_POST["lastname"]))
    {
     // all your php code
    }
    

    Other than that, your code is working fine. It compares like you expect it to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?