html input addclass,html - Add A Class To An Input Using PHP - Stack Overflow

You can have PHP add a class to your inputs if a field is invalid, but you may need to adjust your approach.

If you want to maintain using a form POST method (as you have it), you'd want to move your validation code to be processed on the same page that renders your view (the easiest way to do this is to have it within the same page). You can still use and include a separate file to pull in a PHP class or other system you want to use for validation if you want to keep your code neat. Here's an example:

// filter_input returns null or false if invalid.

$firstname = filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_STRING);

?>

However, I would recommend a more robust approach which would be to use an AJAX handler for submitting your form. This will give your users a better experience, and it also allows you to neatly have your form processing entirely separate from your view. That solution is a bit beyond the scope of this question, but ultimately you would have your PHP AJAX handler return information as to whether or not the submission was valid or what fields are invalid and have the javascript add your invalid class to the fields in error.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值