-5

How to make an input validation for an 'add button'. and I need to use the $_POST data and function is_numeric and must put up the following message: "Data must be numeric", Also if username section is left blank, a message "Username is required" needs to be shown. the form has username, age and amount. (sorry, I just started learning php, don't really know how to code.. please help!) Many thanks!

This is what i got so far

if (isset($_POST['add'])) {  
echo "age,amount " . $_POST['age and amount must be numeric'];
echo "age " . $_POST['age is required'] ;}

The database using an INSERT statement.

$stmt = $pdo->prepare('INSERT INTO autos
(age, amount) VALUES ( :age, :amount)');
$stmt->execute(array(
':age' => $_POST['age'],
':amount' => $_POST['amount']));

1 Answers1

0

I think you should use jquery/javascript for your purpose. Please try the code below (remember to include jquery library in your code).

function check()
{
var e=false;
if ( ($('#usrn').val().length) ){}
else
{alert ("Username is required"); e=true;}
if (jQuery.isNumeric($('#age').val()))
{}
else
{alert ("Data must be numeric");e=true;}
if (e==false){
alert ("Input Data is Ok");
// do something with the value with ajax/php or jquery
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>

<body>
 <div class="row">  
<div class="form-group">
  <label for="usrn">User Name:</label>
  <input type="text" class="form-control" id="usrn">
   <br>
  <label for="age">Age:</label>
  <input type="text" class="form-control" id="age">
   <br>
  <label for="age">Other:</label>
  <input type="text" class="form-control" id="other">
</div>
    <input type="button" value="Add" id="add" onclick="check();return false;"  />
</div>

</body>

</html>
Mai Do
  • 21
  • 5