简体   繁体   中英

How to numerically validate a php form ?

I am trying to set up form validation for a project app. I keep getting the error that I am calling to an undefined function, but I'm not what I am doing wrong. From what I have learned and found online, this is the right syntax. Basically I only want the ticket number and spot number values to accept numbers as their input, and an error message to appear prompting the user to input a numeric value. This is my code so far:

<html>
<?php

if(!is_number($_POST['tick_num'])) { 
    echo "please enter a number";
}

$searchBy = $_POST['field'];
$searchFor = $_POST['term'];

$database = 'jy6536';
$db = mysqli_connect('', 'jy6536', '1km4bjRi', $database);

$record = "SELECT * FROM keyfob WHERE $searchBy LIKE '%".$searchFor."%'";
$result = mysqli_query($db, $record);


while($myrow = mysqli_fetch_array($result))
{
  echo "<br><br>First Name: ".$myrow['last_name'];
  echo "<br> Last Name: ".$myrow['first_name'];
  echo "<br> Ticket Number ".$myrow['tick_num'];
  echo "<br> Make: ".$myrow['make'];
  echo "<br> Spot Number: ".$myrow['spot_num'];
}
?>

<br><br><a href="viewdb.php">Return to Table </a>
<br><a href="search.html">Return to Search </a>
</html>

http://php.net/manual/en/function.is-numeric.php

if(!is_numeric($_POST['tick_num'])) { 
    echo "please enter a number";
}

Do you want to allow ticket numbers like "+0123.45e6" and "9.1"? Maybe http://php.net/manual/en/function.ctype-digit.php would be better?

You could use this code instead:

$ticketNumber = $_POST['tick_num'];
if(!ctype_digit((string)$ticketNumber)) { 
    echo "please enter a number";
}

This will only accept integers, ss values like 9.1 or +0123.45e6 won't be accepted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM