Question:

PHP validation not working...

by  |  earlier

0 LIKES UnLike

Hi,

This was originally working ok 'ish but i've since rewritten it to include it within functions to make the code easier to understand.

Below is the basic code which if fails redirects to the relevant error page, yet if validation passes then in theory nothing should happen. The thing is even if validation is passed it's still redirecting to an error page, i've tried looking online and still can't find an answer, any ideas?

Thanks

<?php

$target_path = "images/uploads/";

$username = $_POST['username'];

$email = $_POST['email'];

$password = $_POST['password'];

$quote = $_POST['quote'];

formvalidate();

function formvalidate(){

// Is username blank?

if(trim($username) == ''){

header('Location: error.php?code=1');

return false;}

// Is email blank?

else if(trim($email) == ''){

header('Location: error.php?code=1');

return false;}

// Is password blank?

else if(trim($password) == ''){

header('Location: error.php?code=1');

return false;}

// Is there a quote?

else if(trim($quote) == ''){

header('Location: error.php?code=1');

return false;}

return true;

}

?>

 Tags:

   Report

4 ANSWERS


  1. I have never coded PHP so am not the best person to try to answer!

    However, could it be anything to do with the fact that in your test eg

    f(trim($username) == &#039;&#039;){

    header(&#039;Location: error.php?code=1&#039;);

    return false;}

    you have an open double quotes in line 1 but no closing double quotes?

    Alternatively, are you sure the if statement is correct?  In BASIC, if you want to check that a string is empty, one way is:

    IF username$=&quot;&quot; THEN PRINT &quot;User name is empty&quot;

    NB in the above code the empty string is denoted by the TWO double quotation marks.

    This is probably rubbish but hope it might help!!


  2. I would try something more like:

    if (! formvalidate())

    {

      header(&quot;Location: error.php?code=1&quot;);

      exit();

    }

    and then remove the header lines inside the function.

    To further clean it up, you could change the function to look like:

    function formvalidate() {

      if ( !trim($username) || !trim($email) || !trim($password) || !trim($quote) ) {

        return false;

      }

      else {

        return true;

      }

    }

  3. I don&#039;t see you do a redirection in that code. You just validate and then ignore the answer.

    There is a better way to do your tests using the isempty function.

    if (isempty($email))

    {

  4. I can&#039;t remember if you can do forward declaration of functions with PHP, i.e., you&#039;re calling your validation function BEFORE it has been declared. Plus, why are you redirecting the browser to another web page from within your function?

    What about this?

    I can&#039;t try it as I have no PHP or webserver installed on this machine.

    &lt;?php

    $target_path = &quot;images/uploads/&quot;;

    $username = $_POST[&#039;username&#039;];

    $email = $_POST[&#039;email&#039;];

    $password = $_POST[&#039;password&#039;];

    $quote = $_POST[&#039;quote&#039;];

    function formvalidate()

    {

        if ( (trim($username) == &#039;&#039;) || (trim($email) == &#039;&#039;) ||

            (trim($password) == &#039;&#039;) || (trim($quote) == &#039;&#039;) )

            return false;

            return true;

    }

    //If the formvalidate function returns FALSE redirect

    if ( !formvalidate() )

    {

        header(&#039;Location: error.php?code=1&#039;);

    };

    Or, what about,

    function formvalidate()

    {

        return !(empty($username) &amp;&amp; empty($email) &amp;&amp;

            (empty($password) &amp;&amp; empty($quote) )

    }

    //If the formvalidate function returns FALSE redirect

    if ( !formvalidate() )

    {

        header(&#039;Location: error.php?code=1&#039;);

    };

    Also, do any redirection before anything HTML is sent to the page or the header function won&#039;t do the redirection.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.