![](/img/trans.png)
[英]How to upload the image using ajax with jquery validation in CodeIgniter?
[英]Using codeigniter's validation on jquery ajax post
我正在嘗試使用codeigniter的驗證使用jquery,ajax和json驗證表單,這是我嘗試過的:
這是我的控制器:
function cadastrar(){
$this->load->library('form_validation');
$this->form_validation->set_rules('nome', 'Nome', 'required|min_length[3]|max_length[15]');
$this->form_validation->set_rules('sobrenome', 'Sobrenome', 'required|min_length[3]|max_length[15]');
$this->form_validation->set_rules('senha', 'Senha', 'required|matches[confirmar]');
$this->form_validation->set_rules('confirmar', 'Confirmação de senha', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() == FALSE)
{
echo '{"mensagem": "Erro", "nome_erro": "' . form_error('nome') . '", "sobrenome_erro": "' . form_error('sobrenome') . '", "email_erro" : "' . form_error('email') . '", "senha_erro" : "' . form_error('senha') . '", "confirmar_erro" : "' . form_error('confirmar') . '" }';
}
else
{
echo '{"mensagem": "Registro com sucesso !", "nome_erro": "", "sobrenome_erro": "", "email_erro" : "", "senha_erro" : "", "confirmar_erro" : "" }';
}
}
這是我的jquery函數:
$(document).ready(function(){
$('.cadastrar_enviar').click(function(){
var nome = $('.cadastro_nome').val();
var sobrenome = $('.cadastro_sobrenome').val();
var email = $('.cadastro_email').val();
var senha = $('.cadastro_senha').val();
var confirmar = $('.cadastro_confirmar').val();
var mensagem = "";
$("#loading").show();
$.post('../index.php/usuario/cadastrar', {
"nome" : nome,
"sobrenome" : sobrenome,
"email" : email,
"senha" : senha,
"confirmar" : confirmar
}, function(data){
$('#loading').hide(500);
$('span#nome_erro').html(data.nome_erro);
$('span#sobrenome_erro').html(data.sobrenome_erro);
$('span#email_erro').html(data.email_erro);
$('span#senha_erro').html(data.senha_erro);
$('span#confirmar_erro').html(data.confirmar_erro);
alert(data.confirmar_erro);
$('#mensagem_oi').html(data.mensagem).show(500);
}, "json");
});
});
我沒有得到任何錯誤,form_error()沒有返回任何東西,任何人都可以給我一個提示嗎? 謝謝
jquery期望控制器返回一個json頁面,但控制器正在響應一個普通的html頁面。 您應該在標題中指定輸出格式,在Codeigniter中代碼如下所示:
$this->output
->set_content_type('application/json')
->set_output(json_encode(array("mensagem"=> "Registro com sucesso !", "nome_erro"=> "", "sobrenome_erro"=> "", "email_erro" =< "", "senha_erro" => "", "confirmar_erro" => "")));
查看輸出類: http : //codeigniter.com/user_guide/libraries/output.html
這樣你應該看到一些東西,但我不知道驗證是否有效。
CI驗證觸發表單提交。
嘗試提交表格。
$('.cadastrar_enviar').submit(function(){
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.