1

I have some code written in php to validate a postcode field in my form. The code is meant to check that the field is not empty (mandatory) and matches one of 5 usable postcodes, if not it displays an alert. The problem i am having is that when i leave the field empty and hit the submit button the proper alert is show but if i enter a wrong value and hit submit the form just loads to a blank screen, can anyone spot a mistake in my code? :

 <?php

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

$post = $_POST["post"];
$words = array('2747','2750','2753','2760','2777');
$msgp = "";

if (!empty($post)) {
foreach ($words as $item)
{
    if (strpos($post, $item) !== false)
        return true;
}
$msgp = '<span class="error"><b>Please enter correct postcode</b></span>';
return false;

} else if(empty($post)) {
$msgp = '<span class="error"><b>Please enter postcode</b></span>';
}

}

?>

<form name="eoiform" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi">

<b>Post Code</b>
<br>
<input type="text" id="post" name="post" /><?php echo $msgp; ?>

</form>
4
  • what's that return true;? Commented May 31, 2013 at 10:50
  • 2
    why don't you use in_array? Commented May 31, 2013 at 10:51
  • Why not just use a <select> element with only those 5 values? Commented May 31, 2013 at 10:52
  • i've answered your question. Select is better option but there should always be validation too. Commented May 31, 2013 at 10:58

5 Answers 5

3

return? Return where?

When you return in your main code, it's (nearly) the same as die()'ing.

So when you return, the remaining PHP won't be executed anymore.

I'd consider to set some variable like $success = true/false; instead of returning.

Sign up to request clarification or add additional context in comments.

Comments

2

You return false after $msgp = '<span class="error"><b>Please enter correct postcode</b></span>'; therefor you do not continue to the form below... remove the returns from your code to be able to handle and show an error.

1 Comment

ah i see, i took out the returns and just left the messages and now it works just fine, thank you!
1

You are using return. Are you in a function() {} ? If so, all your variables are in function scope. You could do global $msgp; to make the variable accessible outside of the function.

If not... then you shouldn't use return.

1 Comment

I still give this +1 even though I disagree with the idea of using globals. If you are in a function you could return $msgp or pass it as a parameter by reference.
0

Change php code to

<?php

if (isset($_POST['post'])) {
$post = intval($_POST["post"],0);
$words = array(2747,2750,2756,2760,2777);
$msgp = '<span class="error"><b>'.in_array($post,$words) ? "Please enter postcode" : "Please enter correct postcode" .'</b></span>';
}

There are a lot of things that can be simplified. For example codes as int array and using in_array() function. Return statement should be used to return something from method/function using it in global scope then execution of the current script file is ended. There is no need for it here. If there are more $words values you should consider using simple REGEX /[0-9]{4}/ and preg_match()

Comments

0

You want to display $msgp right? Use echo $msgp;. But you can return to nowhere in your code. (put it in a function).

Try this

<?php        
$post = $_POST["post"];
$words = array('2747','2750','2753','2760','2777');
$msgp = "<span class='error'><b>Please enter correct postcode</b></span>";
function check($post){
  global $words;
  $return=true;
  if (!empty($post)) {
      foreach ($words as $item)
      {
          if (strpos($post, $item) !== false)
              //$return=true;
      }

  } else if(empty($post)) {
      $return=false;
  } 
  return $result;
}

$return=check($post);
if($return === true){
// echo you are right!
}
else {
    echo $msgp;
}

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.