【问题标题】:numeric function and database pdo数值函数和数据库 pdo
【发布时间】:2018-08-19 22:42:13
【问题描述】:

如何对“添加按钮”进行输入验证。我需要使用 $_POST 数据和函数 is_numeric 并且必须显示以下消息:“数据必须是数字”,此外,如果用户名部分留空,则需要显示消息“需要用户名”。表格有用户名、年龄和金额。 (对不起,我刚开始学习php,不太会写代码..请帮忙!)非常感谢!

这是我目前得到的

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

使用 INSERT 语句的数据库。

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

【问题讨论】:

  • 到目前为止你尝试过什么?我们无法为您编码。
  • 不要在 cmets 部分发布代码,用代码编辑您的答案并删除 cmets

标签: php function pdo isnumeric


【解决方案1】:

我认为你应该使用 jquery/javascript 来达到你的目的。请尝试下面的代码(记得在代码中包含 jquery 库)。

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>

【讨论】:

    猜你喜欢
    • 2013-09-07
    • 2013-07-28
    • 2014-12-06
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多