我正在使用codeigniter。在我的视图文件中我有一个表单,我使用bootstrap验证器检查字段验证.Bootstrap验证器验证表单字段正确。但问题是我正在使用codeigniter。函数写在控制器。表单提交后,它应该重定向在控制器中写入的功能。当我在成功验证后点击提交按钮时我遇到问题它没有执行任何操作。它没有重定向控制器,其中新功能是写的,如何我成功验证后重定向页面。我使用的是不使用ajax的JavaScript。
以下是我的View文件代码:
<form class="" data-toggle="validator" role="form" id="sms_form" method="Post" action="<?php echo site_url('SMS/sendIndividualMsg/'.$row->pro_id)?>">
<div class="col-md-12">
<div class="form-group">
<label for ="message"><strong>Message</strong></label>
<textarea class="form-control" id="comment_body" name="message" placeholder=" Your Message"></textarea>
</div>
<div>
<?php echo form_submit(['name' => 'submit' ,'class' => 'btn btn-default' , 'value' => 'Send Message']); ?>
</div>
</div>
</form>
这是我的bootstrap验证器代码:
<script>
$('#sms_form').bootstrapValidator({
// To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
message:{
validators: {
regexp: {
regexp: /^[a-zA-Z0-9_\.\s]+$/,
message: 'The message can only consist of alphabetical, number, dot and underscore'
},
notEmpty: {
message: 'Please supply your message'
}
}
}
}
});
</script>
这是我的控制器代码。这是我需要重定向的功能:
public function sendIndividualMsg($pro_id)
{
}
答案 0 :(得分:1)
使用此..
<script> $('#sms_form').bootstrapValidator({ // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { message:{ validators: { regexp: { regexp: /^[a-zA-Z0-9_\.\s]+$/, message: 'The message can only consist of alphabetical, number, dot and underscore' }, notEmpty: { message: 'Please supply your message' } } } } .on('success.field.fv', function(e, data) { $( "form" ).submit(); }); }); </script>
答案 1 :(得分:0)
这不是一个有趣的玩法。
长话短说。
你需要拥有你的JS加载你的jquery.js文件(这是你的Bootstrap js文件的所有部分)。你当前的JS工作。
你也需要在bootstrapValidator.js之后,如果你不需要,你需要把你的j包在...
$(document).ready(function () {
// JS Code here if this is rendered before bootstrapValidator.min.js
})
这里的技巧是在Browsers Developer工具中观察控制台消息......
只是说,它不起作用,不看控制台消息就没有帮助。
如果您对我所处理的内容一无所知,我会在稍后澄清。
更新:这是我用来调查您的问题的测试代码。
测试视图 - views / form_validator_view.php
<!doctype html>
<html lang="en">
<head>
<title>Hello, world!</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
</head>
<body>
<h1>Hello, world!</h1>
<div class="container">
<form class="" data-toggle="validator" role="form" id="sms_form" method="Post" action="<?php echo site_url('sms/sendIndividualMsg/' . 1) ?>">
<div class="form-group">
<label for="message"><strong>Message</strong></label>
<div class="col-md-12">
<textarea class="form-control" id="message" name="message" placeholder=" Your Message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-xs-9 col-xs-offset-3">
<button type="submit" class="btn btn-primary" name="signup" value="Sign up">Submit</button>
</div>
</div>
</form>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
<script src="//oss.maxcdn.com/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.min.js"></script>
<script>
$(document).ready(function () {
// JS Code here if this is rendered before bootstrapValidator.min.js
})
// $(document).ready(function () {
$('#sms_form').bootstrapValidator({
// To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
message: {
validators: {
regexp: {
regexp: /^[a-zA-Z0-9_\.\s]+$/,
message: 'The message can only consist of alphabetical, number, dot and underscore'
},
notEmpty: {
message: 'Please supply your message'
}
}
}
}
});
// })
</script>
</body>
</html>
测试控制器 - controllers / Sms.php
<?php
class Sms extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
}
public function index() {
$this->load->view('form_validator_view');
}
public function sendIndividualMsg($id) {
echo "The ID is $id";
var_dump($_POST);
}
}