AJJ
AJJ

Reputation: 897

Numeric PHP validation?

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>

Upvotes: 1

Views: 194

Answers (5)

Bere
Bere

Reputation: 1747

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;
}

Upvotes: 0

Robert
Robert

Reputation: 20286

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()

Upvotes: 0

nl-x
nl-x

Reputation: 11832

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.

Upvotes: 1

bwoebi
bwoebi

Reputation: 23777

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.

Upvotes: 3

Salketer
Salketer

Reputation: 15711

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.

Upvotes: 2

Related Questions